• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Sample code of Applet call servlet

 
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
 
Bartender
Posts: 9612
16
Mac OS X Linux 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 ]
 
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;
}
 
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
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic