Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Servlet to Applet Communication problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("http://127.0.0.1:8080/onlinetest/servlet/UserLogin";);
URLConnection servletConnection = userloginservlet.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
servletConnection.setRequestProperty("Content-Type","application/octet-stream");
UserPassword Up= new UserPassword(UName.getText(),Passwd.getText());
ObjectOutputStream OutputToHost= new ObjectOutputStream(servletConnection.getOutputStream());
OutputToHost.writeObject(Up);
OutputToHost.flush();
OutputToHost.close();


coding of servlet in doPost Method:
-----------------------------------

out = new PrintWriter(res.getWriter());
response.setContentType("text/html");
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?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that the doPost method is invoked?
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");


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

inputFromApplet = new ObjectInputStream(req.getInputStream());
Up=(UserPassword)inputFromApplet.readObject();
inputFromApplet.close();
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 ");
querybuff.append("Password='");
querybuff.append(Up.Password.trim()+"'");
String query=querybuff.toString();
System.out.println(query);


ResultSet rset= stmt.executeQuery(query);
res.setContentType("text/html");
if(rset!=null)
out.println("<html><body>Login and Password are correct</body></html>");
else
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>"); }
finally{

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

}

}
 
POlapragada Kiran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I am sending object from Applet to Servlet, I thought It calls doPost method.
 
POlapragada Kiran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic