Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Calling a java servelet help?


Message #1 by "Paul Dermody" <paul.dermody@m...> on Tue, 1 May 2001 14:52:00
Chanoch Wiggers wrote:
> 
> have a look at the servlet specs but basically you would need to call
> init(), then doGet() or doPost() (which ideally you would call via the
> service() method? I think) the only problem is you probably have to provide
> a servlet context. You would also need to have the servlet.jar package
> available on the classpath, which incidentally should give you a servlet
> context implementation to instantiate (I think). The specs are the best
> place to start. Depending on where the important code is and to what extent
> it depends on the container - i.e. does it require persistence, cookies,
> server variables,  etc, you may be able to just call init() followed by
> doGet() and its probably worth trying that first

No. None of what you describe is necessary. To call a servlet a standalone
java client doesn't need servlet.jar, doesn't need to call the servlet's
init() method, or service() or doGet() or doPost().

What is the most common standalone client that calls a servlet? A Browser, of
course.

To call a servlet, a standalone client just needs to perform the same steps as
a browser:

- Open a connection to the servlet url
- send an HTTP formatted request (most commonly HTTP GET or POST)
- read the response from the server

There are several examples available showing how to do this. Your first stop
should be the Java Tutorial, here's the link to the networking tutorial which
shows how to use the java.net classes to connect to a url:
http://java.sun.com/docs/books/tutorial/networking/index.html

Download the code samples at http://www.servletguru.com/ and look for the
classes DataStreamEcho, ObjectStreamEcho, TestDataStream, and
TestObjectStream.

The examples at http://www.servlets.com/jservlet2/examples/ch10/index.html
show how an applet communicates with a servlet. The only difference between a
standalone client and an applet is the applet runs inside a browser.

The jguru faq at http://www.jguru.com/faq/home.jsp?topic=Servlets also has
some entries on this subject.

> -----Original Message-----
> From: Paul Dermody [mailto:paul.dermody@m...]
> 
> Has anyone tried calling(invoking) a servlet from a standalone java client.
> What are the steps involved
> Any responces would be helpful

  Return to Index