This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
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

Win a copy of The Software Craftsman this week in the Agile forum!
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: 12913
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.
Have you tried LearnNowOnline?
subject: Invoking a Servlet