aspose file tools*
The moose likes Servlets and the fly likes how to pass parameters from a serlet to an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to pass parameters from a serlet to an applet" Watch "how to pass parameters from a serlet to an applet" New topic
Author

how to pass parameters from a serlet to an applet

Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
hi everybody how u doing ?
i am making this applet and the values for the applet comes as a parameter from the servlet .I am done with my servlet and my applet the only thing that is missing now is i don know how to communicate the servlet with the applet. so any help is appreciated
ciao


if u can't impress people with ur intelligence , confuse them with ur bullshit
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
If all you need is to pass the applet initialization parameters, just write them into the applet tag param elements in the html page.

If you need to do it dynamically while the applet runs - this has been discussed a number of times here - search for servlet applet communcation.
Bill
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Here is some code which i used for applet and Servlet comminication.
Hope this might help u.
/**
* This method causes the applet to interact with the servlet. Basically, the
* method writes the message to the servlet, then reads the response and sticks
* it in the "response" field.
**/
private void interactWithServlet() {
try {
// Create an object we can use to communicate with the servlet
String location = "http://pdc:7001/SP/AttributeServlet";
URL servletURL = new URL(location);
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
// Write the message to the servlet
PrintStream out = new PrintStream(servletConnection.getOutputStream());
out.println(topTextArea.getText());
out.close();
// Now read in the response
InputStream in = servletConnection.getInputStream();
StringBuffer response = new StringBuffer();
int chr;
while ((chr=in.read())!=-1) {
response.append((char) chr);
}
in.close();
// responseField.setText(response.toString());
} catch (IOException e) {
e.printStackTrace();
// responseField.setText("An error occurred: " + e.toString());
}
// Read in the message from the servlet
if(param_Request.getReader()!=null){
StringBuffer msgBuf = new StringBuffer();
BufferedReader fromApplet = param_Request.getReader();
String line;
while ((line=fromApplet.readLine())!=null) {
if (msgBuf.length()>0) msgBuf.append('\n');
msgBuf.append(line);
cat.debug("Applet servlet==================msgBuf:"+msgBuf);
}
fromApplet.close();
// Write the message back to the applet
param_Response.setContentType("text/plain");
PrintWriter toApplet = param_Response.getWriter();
toApplet.println("I (the servlet) received a message \"" + msgBuf.toString() + "\"");
toApplet.println("at " + (new Date()).toString());
toApplet.println("and sent this text in response. Hope you enjoy it!");
toApplet.close();
}
 
wood burning stoves
 
subject: how to pass parameters from a serlet to an applet