Click to See Complete Forum and Search --> : Applet Servlet Communication


Monika
April 29th, 2000, 12:37 AM
How to call a servlet from an applet and vice versa?

bharath
April 29th, 2000, 01:38 AM
hai Monica,
place all ur applet related files inside ur publichtml dir inside ur web server and servlet related files inside ur servlets directory and register the servlet inside the web server. the registrasion depends on the server u use.

Then direcly call the applet from the browser using the ip and the applet name
eg: http://localhost:8080/sample.html
which ill refer to the applet in the publichtml dir and the applet ill call the servlet in the servlet directory as need the code u need to include in the applet is as given below :
URL url = new URL(getCodeBase(),"/servlet/SampleServlet");
HttpMessage msg = new HttpMessage(url);
InputStream in = msg.sendGetMessage();
DataInputStream result = new DataInputStream(new BufferedInputStream(in));
String date = result.readLine();

hope u got it.

If u wanted further det. mail me at: bharath@vsofti.stph.net

bye
bharath



S.V Bharath Reddy
Software Engineer,
Hyderabad.

nirmit
April 29th, 2000, 10:58 AM
Hi

It is very simple. Write following code in your applet.

URL urlobj=new URL("http://servername:8080/servlet/servletname");

URLConnection urlcobj=urlobj.openConnection();

InputStream in=urlcobj.getInputStream();

//now you can customize the string and read from it. it will read what ever servlet writes.

Now if you want to pass some parameters to the servlet do,

append the parameters at the end of url string like this,

http://localhost:8080/servlet/myserv?name=nirmit

this will send a parameter named "name" and valued "nirmit" to the servlet.

i the servlet you can fetch the parameter with string name=req.getparameter"name";

That's all.

mail me at nirmit@yours.com for further details.
Good luck.