nsuresh
asked on
applet-servlet comunication
can i pass directly objects across applet-servlet comm.
i see the httpRequest's getParamaters only strings , so is only strings
are possible ?
similarly , i need to send an object from the servlet to applet and i dont
want to deocde it to a string ..
tia
i see the httpRequest's getParamaters only strings , so is only strings
are possible ?
similarly , i need to send an object from the servlet to applet and i dont
want to deocde it to a string ..
tia
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Refer to these sites too
1. http://www.j-nine.com/pubs/applet2servlet/index.htm
Lists articles, books, exampless (lot many) that will answer
all of your questions.
2. You need to serialize your object before you pass ot from
applet to servlet and vice versa. Refer to this site for
more on this :
http://java.sun.com/docs/books/tutorial/essential/io/providing.html
3. refer to this example for code details
// read serialized objects at the client
// connect to the servlet
URL studentDBservlet = new URL( servletLocation );
URLConnection servletConnection = studentDBservlet.openConne ction();
// Don't used a cached version of URL connection.
servletConnection.setUseCa ches (false);
servletConnection.setDefau ltUseCache s(false);
// Read the input from the servlet.
//
// The servlet will return a serialized vector containing
// student entries.
//
inputFromServlet = new ObjectInputStream(servletC onnection. getInputSt ream());
studentVector = (Vector) inputFromServlet.readObjec t();
--- server side here ---
// Listing 4
//
// Servlet server-side code to send a serialized
// vector of student objects to an applet.
//
//
public void sendStudentList(HttpServle tResponse response, Vector studentVector)
{
ObjectOutputStream outputToApplet;
try
{
outputToApplet = new ObjectOutputStream(respons e.getOutpu tStream()) ;
System.out.println("Sendin g student vector to applet...");
outputToApplet.writeObject (studentVe ctor);
outputToApplet.flush();
outputToApplet.close();
System.out.println("Data transmission complete.");
}
catch (IOException e)
{
e.printStackTrace();
}
}
----- server side to read serialized object ----
// Listing 3
//
// Servlet server-side code to read a serialized
// student object from an applet.
//
// The servlet code handles a POST method
//
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ObjectInputStream inputFromApplet = null;
Student aStudent = null;
PrintWriter out = null;
BufferedReader inTest = null;
try
{
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(request. getInputSt ream());
// read the serialized student data from applet
aStudent = (Student) inputFromApplet.readObject ();
inputFromApplet.close();
// continue the process for registering the student object
}
catch(Exception e)
{
// handle exception
}
}
--- cliend side code to send serialized object to server ---
// Listing 2
//
// Applet client-side code to send a student object
// to a servlet in a serialized fashion.
//
// A POST method is sent to the servlet.
//
URL studentDBservlet = new URL( webServerStr );
URLConnection servletConnection = studentDBservlet.openConne ction();
// inform the connection that we will send output and accept input
servletConnection.setDoInp ut(true);
servletConnection.setDoOut put(true);
// Don't use a cached version of URL connection.
servletConnection.setUseCa ches (false);
servletConnection.setDefau ltUseCache s (false);
// Specify the content type that we will send binary data
servletConnection.setReque stProperty ("Content-Type", "application/octet-stream" );
// send the student object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servlet Connection .getOutput Stream());
// serialize the object
outputToServlet.writeObjec t(theStude nt);
outputToServlet.flush();
outputToServlet.close();
--------------------
good luck :))
wishes,
raghuRani
1. http://www.j-nine.com/pubs/applet2servlet/index.htm
Lists articles, books, exampless (lot many) that will answer
all of your questions.
2. You need to serialize your object before you pass ot from
applet to servlet and vice versa. Refer to this site for
http://java.sun.com/docs/books/tutorial/essential/io/providing.html
3. refer to this example for code details
// read serialized objects at the client
// connect to the servlet
URL studentDBservlet = new URL( servletLocation );
URLConnection servletConnection = studentDBservlet.openConne
// Don't used a cached version of URL connection.
servletConnection.setUseCa
servletConnection.setDefau
// Read the input from the servlet.
//
// The servlet will return a serialized vector containing
// student entries.
//
inputFromServlet = new ObjectInputStream(servletC
studentVector = (Vector) inputFromServlet.readObjec
--- server side here ---
// Listing 4
//
// Servlet server-side code to send a serialized
// vector of student objects to an applet.
//
//
public void sendStudentList(HttpServle
{
ObjectOutputStream outputToApplet;
try
{
outputToApplet = new ObjectOutputStream(respons
System.out.println("Sendin
outputToApplet.writeObject
outputToApplet.flush();
outputToApplet.close();
System.out.println("Data transmission complete.");
}
catch (IOException e)
{
e.printStackTrace();
}
}
----- server side to read serialized object ----
// Listing 3
//
// Servlet server-side code to read a serialized
// student object from an applet.
//
// The servlet code handles a POST method
//
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ObjectInputStream inputFromApplet = null;
Student aStudent = null;
PrintWriter out = null;
BufferedReader inTest = null;
try
{
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(request.
// read the serialized student data from applet
aStudent = (Student) inputFromApplet.readObject
inputFromApplet.close();
// continue the process for registering the student object
}
catch(Exception e)
{
// handle exception
}
}
--- cliend side code to send serialized object to server ---
// Listing 2
//
// Applet client-side code to send a student object
// to a servlet in a serialized fashion.
//
// A POST method is sent to the servlet.
//
URL studentDBservlet = new URL( webServerStr );
URLConnection servletConnection = studentDBservlet.openConne
// inform the connection that we will send output and accept input
servletConnection.setDoInp
servletConnection.setDoOut
// Don't use a cached version of URL connection.
servletConnection.setUseCa
servletConnection.setDefau
// Specify the content type that we will send binary data
servletConnection.setReque
// send the student object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servlet
// serialize the object
outputToServlet.writeObjec
outputToServlet.flush();
outputToServlet.close();
--------------------
good luck :))
wishes,
raghuRani
Here is one way of passing object directly across applet-servlet .
At the server side : -
public class A extends HttpServlet{
public Date getDate()
{
return new Date();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException , IOException
{
if("object".equals(req.get Parameter( "format")) )
{
ObjectOutputStream out = new ObjectOutputStream(res.get OutputStre am());
out.writeObject(getDate()) ;
}
else
{
PrintWriter out = res.getWriter();
out.println(getDate().toSt ring());
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException , IOException
{
doGet(req,res);
}
}
At the applet side;
private String getDateUsingHttpObject()
{
try
{
URL url = new URL(getCodeBase(),"/servle t/DaytimeS ervlet");
HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("format","object ");
InputStream in = msg.sendGetMessage(props);
ObjectInputStream res = new ObjectInputStream(in);
Object obj = res.readObject();
Date date = (Date)obj;
return date.toString();
}
catch(Exception ev1)
{
ev1.printStackTrace();
return null;
}
}
best Luck,
Yogesh
At the server side : -
public class A extends HttpServlet{
public Date getDate()
{
return new Date();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException , IOException
{
if("object".equals(req.get
{
ObjectOutputStream out = new ObjectOutputStream(res.get
out.writeObject(getDate())
}
else
{
PrintWriter out = res.getWriter();
out.println(getDate().toSt
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException , IOException
{
doGet(req,res);
}
}
At the applet side;
private String getDateUsingHttpObject()
{
try
{
URL url = new URL(getCodeBase(),"/servle
HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("format","object
InputStream in = msg.sendGetMessage(props);
ObjectInputStream res = new ObjectInputStream(in);
Object obj = res.readObject();
Date date = (Date)obj;
return date.toString();
}
catch(Exception ev1)
{
ev1.printStackTrace();
return null;
}
}
best Luck,
Yogesh
ASKER
thanks a lot
Suresh
Suresh
and send them to the destination.
the reverse is trivial ;)