1,ServletInvokerApplet base class for the calling applet.
- By David Tayouri
- November 17, 2000
ENTERPRISE JAVA
Invoke Servlet Methods From Java Apps
David Tayouri
Listing 1. ServletInvokerApplet base class for the calling applet.
/** Extend this applet to have the ability of
servlet method invocation.
The servlet to be invoked usually will
extend InvocableServlet
*/
public abstract class ServletInvokerApplet
extends java.applet.Applet
{
protected URL servletUrl;
//=========================================
/** Opens an output connection to the
servlet and passes the method name
and its parameters (as a vector).
The return value is read from the input
stream and returned as an object.
*/
protected Object invokeServletMethod(
String methodName,
Vector params)
{
Object returnValue = null;
try {
// Connect
URLConnection conn =
servletUrl.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty(
"Content-Type",
"application/octet-stream");
// Open output stream, send method
// name and its params
// and close the stream
ObjectOutputStream out =
new ObjectOutputStream(
conn.getOutputStream());
out.writeObject(methodName);
out.writeObject(params);
out.flush();
out.close();
// Open input stream, read the
// data and close the stream
ObjectInputStream in =
new ObjectInputStream(
conn.getInputStream());
returnValue = in.readObject();
in.close();
} catch (Exception e) {
System.out.println(e);
}
return returnValue;
}
}