Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sample code of Applet call servlet

 
Rawat Vijay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone get me sample code of applet which is calling servlet
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at java.net.URLConnection. Create a java.net.URL instance with the URL of the servlet, create an instance of URLConnection and use the various methods of URLConnection to interact with the servlet (i.e. doInput() to POST data or getContent to download). NOTE: An unsigned applet can only establish connections with the same server it was loaded from.
[ February 06, 2004: Message edited by: Joe Ess ]
 
jahanzeb fahim
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public Vector PopDat()
{
Vector vf=new Vector();
try
{
URL studentDBservlet = new URL( "http://jahanzeb:8080/servlet/QuerySer?Qname="+ap.getParameter("Per"));
URLConnection servletConnection = studentDBservlet.openConnection();
BufferedReader inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
String str;
while (null != ((str = inFromServlet.readLine())))
{
vf.add(str);
System.out.println(str);
}


}catch(Exception e)
{
System.out.println(e.toString());
}

return vf;
}
 
Purvi Pandya
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !!!
You can do this also
----------------
(Code in Applet)
URL helloServletURL = new URL( getCodeBase().toString()+ "HelloServlet" );
URLConnection urlConnection = helloServletURL.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);

ObjectOutputStream objOut = new ObjectOutputStream (urlConnection.getOutputStream());
objOut.writeUTF(Id); // pass parameter to servlet
objOut.flush();

ObjectInputStream objIn = new ObjectInputStream(urlConnection.getInputStream());
String state = objIn.readUTF(); // read String from Servlet
----------------
(Code in Servlet)
ObjectInputStream dataInput = new ObjectInputStream(request.getInputStream());
String id = dataInput.readUTF();
ObjectOutputStream dataOutput = new ObjectOutputStream(response.getOutputStream());
dataOutput.writeUTF(status);
dataOutput.flush();
dataInput.close();
dataOutput.close();
--------------------
Purvi Pandya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic