Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to pass parameters from a serlet to an applet

 
Sim Raina
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic