File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Invoking a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invoking a Servlet" Watch "Invoking a Servlet" New topic

Invoking a Servlet

Samuel Lugo

Joined: Feb 28, 2005
Posts: 24

Is it possible to invoke a servlet from another Java class that is not a servlet? If so, what would be the best way to go about doing that?

Thanks in advance for the assistance.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

It depends if you want it to run as a servlet or just another class. If you want it to run as a servl;et, you'll want to put it in a container and call it via a url connection. Otherwise, just create a fake implementation of the request and response interfaces and go for it!
Samuel Lugo

Joined: Feb 28, 2005
Posts: 24
So using the URL and URLConnection classes would work?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Note that if you open a URLConnection with a URL that starts "http://" what you get is actually a HttpURLConnection (in the package). There is where you find methods such as setRequestMethod() which are probably going to be useful when talking to your servlet.
I agree. Here's the link:
subject: Invoking a Servlet
It's not a secret anymore!