| Author |
servlet cannot read incoming data
|
Gauri Deshmukh
Greenhorn
Joined: Dec 25, 2004
Posts: 12
|
|
HI, I am trying to send a string from an applet to a servlet. I can see that the applet is sending the data to the servlet (prints stmts) . However the servlet doen't receive that data. I put in print stmts after the servlet reads the object but nothing gets printed in stdout in logs. I don tthink the servlet even gets called to being with...I have put my code here ...can someone please help me ......I have been stuck at this problem for quite a while. Applet code: public class tryui extends JApplet { public void init() { String userID="gauri"; String servletStr = "http://localhost:8080/App/servlet/senddata"; String ID; try { URL url = new URL(servletStr); URLConnection servconn = url.openConnection(); System.out.println("the opencon was done"); servconn.setDoInput(true); servconn.setDoOutput(true); System.out.println("doip/doop"); servconn.setUseCaches(false); servconn.setDefaultUseCaches(false); System.out.println("caches ok"); servconn.setRequestProperty("Content-Type","application/x-java-serialized-object"); servconn.connect(); System.out.println("connect done"); ObjectOutputStream out = new ObjectOutputStream(servconn.getOutputStream()); out.writeObject(userID); out.flush(); System.out.println("string flushed"); out.close(); } catch(Exception e) { System.out.println("exception in sending data" +e); } } } Servlet code: public class senddata extends HttpServlet{ String userID =null; public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try{ InputStream in=req.getInputStream(); ObjectInputStream ois= new ObjectInputStream(in); userID=ois.readObject().toString(); System.out.println("the userid received is: "+userID); in.close(); } catch(Exception e) { System.out.println("error in reading string userid" +e); } } } Any pointers would be helpful...this is a very critical part of my project ...and I am new to java. Thanks, G.  [ January 04, 2005: Message edited by: Gauri Deshmukh ]
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Gauri, This is pretty good sized task for someone new to Java. This chapter of "Core Servlets and Java Server Pages" shows exactly how to do what you're try in to do. http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter17.pdf You might also want to step back a bit and learn a little more about servlets. What you've got so far in your servlet code is not thread safe (userId is an instance variable) and will give you some unexpected surprises once you get the applet code working. Good luck -Ben
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12271
|
|
Your URLConnection is creating a GET request, not a POST. Therefore your doPost is not called. Look at the setRequestMethod method in the HttpURLConnection class in the java.net package. Since the servletStr starts with "http://" the connection you get is a HttpURLConnection and you can set the method. Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: servlet cannot read incoming data
|
|
|