• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass parameters from a serlet to an applet  RSS feed

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
Author and all-around good cowpoke
Posts: 13078
  • 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.
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();
// Write the message to the servlet
PrintStream out = new PrintStream(servletConnection.getOutputStream());
// Now read in the response
InputStream in = servletConnection.getInputStream();
StringBuffer response = new StringBuffer();
int chr;
while ((chr=in.read())!=-1) {
response.append((char) chr);
// responseField.setText(response.toString());
} catch (IOException e) {
// responseField.setText("An error occurred: " + e.toString());
// Read in the message from the servlet
StringBuffer msgBuf = new StringBuffer();
BufferedReader fromApplet = param_Request.getReader();
String line;
while ((line=fromApplet.readLine())!=null) {
if (msgBuf.length()>0) msgBuf.append('\n');
cat.debug("Applet servlet==================msgBuf:"+msgBuf);
// Write the message back to the applet
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!");
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!