Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

simple Applet-Servlet communication (code provided)

733 views
Skip to first unread message

valyuha

unread,
Mar 15, 2001, 11:42:01 AM3/15/01
to
Hi,
I am trying to write a very simple communication between an
applet and a servlet, based on an example I've seen on the Sun
site. However, it is not working properly. I am running a JSDK2.1
server, and I think the problem is in my applet, because I dont
think I give it a proper URL to the servlet, although it seemed
to me that I tried all the variations. The applet and servlet
classes are in the same directory, if that helps. Anyway, if you
guys could look at my code and tell me whats wrong, I would
greatly appreciate it.

import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.*;
public class TestApplet extends Applet
{
private String line;

public void init () {
try {
URL url = new
URL("http://localhost:8080/webpages/Web-inf/servlets/TestServlet?
name=TestApplet");
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
line = in.readLine();
in.close();
}
catch (Exception e){e.printStackTrace();}
}

public void paint (Graphics g) {
g.drawString (line, 20, 40);
g.drawString("This is just applet drawing!", 20, 80);
}
}// testapplet

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class TestServlet extends HttpServlet {

public void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException,
IOException
{
res.setContentType ("text/plain");
PrintWriter out = res.getWriter();
out.println ("Hello " + req.getParameter("name") + ", servlet
talking!");
out.close();
}
}// testservlet


Thanks,
Val.

HjDcW68

unread,
Mar 15, 2001, 12:55:34 PM3/15/01
to
Hi,

Are you getting any output when you run?

The code works except that there should not be a space in
"TestServlet? name=TestApplet".

valyuha

unread,
Mar 15, 2001, 1:16:15 PM3/15/01
to
I'm getting an output :
Hello null, servlet talking!

But, thats just from the servlet, if it was the applet it would
add another line afterwards.
Val.

P.S. That space is fixed in the program, just copied it wrong.


valyuha

unread,
Mar 15, 2001, 12:03:04 PM3/15/01
to
Oh, just one more little thing, and it probably isnt changing
anything, but the servlet gets called from the html page when I
click on the submit button.

HjDcW68

unread,
Mar 15, 2001, 2:08:21 PM3/15/01
to
You should get null if you still have that space after the
question mark. I tried out these code myself and they work fine.

g.balakri...@gmail.com

unread,
Nov 28, 2013, 6:17:51 AM11/28/13
to
0 new messages