pls tell me how to send data from an applet to a servlet.am giving my code below but its not working.
thnks in advance.
Aplet code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.net.*;
import java.util.*;
public class apptoserv extends Applet implements ActionListener
{
TextField tf;
Button b;
public void init()
{
tf=new TextField(20);
add(tf);
b=new Button("send");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e)
{
try{
String s=tf.getText();
System.out.println(s);
URL hp=new URL("http://localhost:8080/servlet/servtoapp");
URLConnection hpcon=hp.openConnection();
System.out.println("Connection estd");
hpcon.setDoOutput(true);
hpcon.setUseCaches(false);
hpcon.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream os=new ObjectOutputStream(hpcon.getOutputStream());
os.writeObject(s);
System.out.println("data sent");
os.close();
}catch(Exception e1){e1.printStackTrace();}
}
}
Servlet code:
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servtoapp extends HttpServlet
{
String s="";
public void init() throws ServletException
{
super.init();
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
try{
ObjectInputStream os=new ObjectInputStream(req.getInputStream());
s=(String)os.readObject();
System.out.println(s);
}catch(Exception e){}
}
}
From Anindya
ghoshanindya@yahoo.com
-
Applet Servlet communication (4 messages)
- Posted by: Anindya Ghosh
- Posted on: August 20 2000 04:40 EDT
Threaded Messages (4)
- Applet Servlet communication by Hariprasad Vijayaraghavan on August 20 2000 18:15 EDT
- Applet Servlet communication by Andy Nguyen on August 23 2000 13:51 EDT
- Applet Servlet communication by Hari Koduru on October 01 2000 02:29 EDT
- Re: Applet Servlet communication by J Dev on September 09 2007 03:18 EDT
-
Applet Servlet communication[ Go to top ]
- Posted by: Hariprasad Vijayaraghavan
- Posted on: August 20 2000 18:15 EDT
- in response to Anindya Ghosh
If you are passing large chunk of data from Applet to Servlet or from Servlet to Applet NEVER use Object stream instead WRAP the Object Stream around Buffered stream. This will reduce the number of Writes to the stream and significantly improve the performance. If you sending very few objects then you are OKAY with this Approach.
1. I am not sure why you are using Content Type of Application/octet..., Instead use "java-internal/<class name of the Object you are passing". So if you change this then you should be OKAY.
2. Here is the code to send large chunk of data,
InputStream writeMessage(String input) throws IOException
{
ObjectOutputStream otStream;
URL url;
Vector newVect;
String urlInp = "http://10.9.42.55:7001/" + input;
url = new URL(urlInp);
con2 = url.openConnection();
con2.setDoOutput(true);
con2.setUseCaches(false);
// assign newVect variable
con2.setRequestProperty("Content-Type","java-internal/" + newVect.getClass().getName());
BufferedOutputStream bout = new BufferedOutputStream(con2.getOutputStream(), 1024);
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(newVect);
out.flush();
out.close();
}
on the Servlet side you need to use a combination of Buffered / Object stream as used above to read the data.
Hariprasad
Cognizant Tech Soln
hariprasad at mailcity dot com
-
Applet Servlet communication[ Go to top ]
- Posted by: Andy Nguyen
- Posted on: August 23 2000 13:51 EDT
- in response to Anindya Ghosh
When I was doing this with WebLogic, I ran into similar problems. What I found was that the applet didn't actually send the request until I grabbed the input stream from the url connection.
Try adding hpcon.getInputStream() after you close your output stream in the applet. Also, you may want to have your servlet send back a simple response. I think if you just set the content type on the response, it'll at least send back and HTTP response with no body. So also add res.setContentType("text/html") to your servlet's doGet method.
Hope this helps,
Andy Nguyen -
Applet Servlet communication[ Go to top ]
- Posted by: Hari Koduru
- Posted on: October 01 2000 02:29 EDT
- in response to Andy Nguyen
Andy,
You are so damn correct.
Thanks man.
- a different Hari -
Re: Applet Servlet communication[ Go to top ]
- Posted by: J Dev
- Posted on: September 09 2007 03:18 EDT
- in response to Anindya Ghosh
Here is the code which you can use to communication between applet and servlet/JSP following code can be used at applet side : /* *URL to servlet */ URL serverURL = new URL(servletPath); URLConnection connection = serverURL.openConnection(); /* * Connection will be used for both input and output */ connection.setDoInput(true); connection.setDoOutput(true); /* * Disable caching */ connection.setUseCaches(false); /* *connection will be used for transfaring serialized java objects. */ connection.setRequestProperty("Content-Type", "application/octet-stream"); ObjectOutputStream outputStream = new ObjectOutputStream(connection.getOutputStream()); outputStream.writeObject(request); outputStream.flush(); outputStream.close(); ObjectInputStream inputStream = new ObjectInputStream(connection.getInputStream()); response = (Map)inputStream.readObject(); inputStream.close(); Here response is the response Map returned from servlet. following code can be used in your servlet to send serializable objects to applet. write this code in doXXX method of servlet. ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream()); Map responseMap = new HashMap(); /* put watever objects you wants to return to applet. This objects must be serializable. responseMap.put("a", "a"); */ out.writeObject(responseMap); out.flush(); out.close(); response.setStatus(HttpServletResponse.SC_NO_CONTENT); SS http://www.jyog.com