Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking Servlets Outside a Webapp

 
Jason Morris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the "Hammering a nail with a screwdriver dept."

Is it possible to invoke a servlet without invoking a servlet container (like Tomcat)?

-JM
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
Do you mean you want the servlet to run by itself? You could pass it a mock request and response. But then it would function as a regular class and not a servlet. What are you trying to accomplish?
 
Jason Morris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

>> Do you mean you want the servlet to run by itself?
>> You could pass it a mock request and response.

Hi Jeanne,
I'm actually cross-posting for a fellow developer from the Portland Java Users Group. Essentially, he is doing what you describe -- trying to treat a servlet as a class, but not as you would expect when using a mock req/res approach -- usally (my understanding) is that it is for testing.

He actually wants the servlet to do useful work (processing a some data or something via an Ant script -- it gets convoluted to my way of thinking.. ) anyway.. I was just wondering if there was any practical examples of this being done -- and hence my hammering a nail with a screwdriver analogy.

Cheers,
Jason
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets depend on the servlet container for a LOT of services, so faking the servlet container environment would be a waste of time.
Instead, move all the functionality into a "helper" class that can run in both the servlet container and normal application environment. Incidently that is a great way to test and debug during servlet development.
For example, the request getParameterMap() method returns all of the request parameter values in String[] keyed by parameter name. You can easily create an identical map as input to a method in a non-servlet environment.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic