Meaningless Drivel is fun!
The moose likes Servlets and the fly likes Servlet to Applet Communication problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to Applet Communication problem" Watch "Servlet to Applet Communication problem" New topic

Servlet to Applet Communication problem

POlapragada Kiran

Joined: Nov 09, 2004
Posts: 4
Hello evrybody,

I have a problem regarding Servlet to Applet communication.

Stpes are as follows:-

1. sent object from applet to servlet through ObjectStream
2. servlet handling doPost()
2. I am trying to send HTML response to the client from the servlet

Problem is I am not getting any type of output..I am not getting even errors.

I am using TOMCAT 4.0

Coding of the Applet is as follows:-

URL userloginservlet = new URL("");
URLConnection servletConnection = userloginservlet.openConnection();
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
UserPassword Up= new UserPassword(UName.getText(),Passwd.getText());
ObjectOutputStream OutputToHost= new ObjectOutputStream(servletConnection.getOutputStream());

coding of servlet in doPost Method:

out = new PrintWriter(res.getWriter());
out.println("<html><body>Login and Password are correct</body></html>");

NOTE:- I didn't get any eroors. And interesting thing is when I used HTML interface , It worked fine. But when I am using Applet as a front end it is not working.

Please Help me!!!

The key is whether can we get the objects from Applet and send HTML code to the client browser? Then how it can be done?
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
Are you sure that the doPost method is invoked?
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Is your servlet expecting Objects in it's InputStream. If you are sending Java objects in the body of your POST message, then your servlet has to open an ObjectInputStream in the doPost method and read the object from the InputStream. POST from an HTML form doesnt send an Java Object. It sends the form fields as name value pairs. So, I dont understand what you mean when you say that the servlet works through HTML and not Applet

here is some sample code for HTTP Tunnelling
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Oh!!another thing!!! Try opening an InputStream in your applet code. I'm not sure but I think the OutputStream that you get from servletConnection will not send the request until you open an InputStream
POlapragada Kiran

Joined: Nov 09, 2004
Posts: 4
Sorry Guys...Following is the full versuion of my Servlet :
rotected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
ObjectInputStream inputFromApplet =null;
PrintWriter out = null;
Statement stmt=null;
Connection con=null;
UserPassword Up=null;

System.out.println("IT HAS BEEN CALLED");

System.out.println("We are now in try block");

inputFromApplet = new ObjectInputStream(req.getInputStream());
System.out.println("taken input");

out = new PrintWriter(res.getWriter());
con = getConnection();
stmt= con.createStatement();
StringBuffer querybuff=new StringBuffer();
querybuff.append("select Login from LogPass where Login='");
querybuff.append(Up.Login.trim()+"' AND ");
String query=querybuff.toString();

ResultSet rset= stmt.executeQuery(query);
out.println("<html><body>Login and Password are correct</body></html>");
out.println("<html><body>Login and Password are NOT correct</body></html>");
}catch(SQLException e)
{ out.println("<html><body> SQLEXeption: " + e.getMessage() + "<br> " + e.getNextException() + "<br> " +e.getErrorCode() + "<br>" + e.getSQLState()+"<br> </body></html>"); }
catch(Exception ie)
{ out.println("<html><body> Exception: " + ie.toString() +"</body></html>"); }

if(stmt!=null) stmt.close();
if(con!=null) con.close();
}catch(SQLException se) { System.out.println(se.getMessage());}


POlapragada Kiran

Joined: Nov 09, 2004
Posts: 4
Since I am sending object from Applet to Servlet, I thought It calls doPost method.
POlapragada Kiran

Joined: Nov 09, 2004
Posts: 4
I mean that I designed a HTML interface and sent login and password to servlet and there I just connected to databse and compared the login and password with the databse entrys after that I sent html response.It is working. But when I am using Applet and using ObjectInputStreams, It is not working.
I agree. Here's the link:
subject: Servlet to Applet Communication problem
jQuery in Action, 3rd edition