Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to call sevlet from corejava program?  RSS feed

 
Shobana Sukumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a servlet called fileservlet inside web-inf in mywebapp.and i need to call this servlet from a core java program which is inside mywebapp.can any one help with the sample code and the steps to run this?(bcoz we usually do this only with jsp calling servlets)
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to forward the request to that servlet by using forward() method, from any other servlet/Filter or any regular java class having mendatory objects in order to forward the request.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about creating an object of servlet and calling it's any method (doGet(), doPost() or service()) explicitly???
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What about creating an object of servlet and calling it's any method (doGet(), doPost() or service()) explicitly???


No, you can do it only as Adeel said.
While it may theorotically possible (after all a Servlet too is a java class), you should understand that it's lifecyle ought to be managed by a container. If you instantiate the Servlet object directly, you are by passing the init() method, which may potentially load data from init-params and which is required in processing of the request. This is only one of several pitfalls. The correct way to do it is call forward() and let the container invoke appropriate methods on the Servlet.

Ram.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should be able to forward the request to that servlet by using forward() method, from any other servlet/Filter or any regular java class having mendatory objects in order to forward the request.


I appreciate if you can you explain with a piece of code.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I appreciate if you can you explain with a piece of code.


The keyword here is having mendatory objects in order to forward the request. What does it take to forward a request - a RequestDispatcher object, right ? And how do you get hold of a RequestDispatcher object - either from the request object or the ServletContext object.

So if you are somewhere in a method that has access to either the request or the context object and that code participates in a http request..get the drift ?

cheers,
ram.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about creating an object of servlet and calling it's any method (doGet(), doPost() or service()) explicitly???


To add to what ramprasad madathil said before:

You probably would have to fake ServletRequest and ServletResponse objects, and it would not a good design to begin with.

It would be better to remove the business logic from the servlet, so that it can be its own object, which you can then instantiate and use from whichever code you need to. Who knows, maybe one day you want to incorporate it into a desktop app, a web service or an EJB?

If you are set to call it as a servlet, and you can't use forward or include for some reason, you can always use HttpUrlConnection to make a network connection to it.
[ November 25, 2005: Message edited by: Ulf Dittmer ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shobana Sukumar:
i have a servlet called fileservlet inside web-inf in mywebapp.and i need to call this servlet from a core java program which is inside mywebapp.can any one help with the sample code and the steps to run this?(bcoz we usually do this only with jsp calling servlets)


can use HTTPUrlConncection class from java.net packege .

cheers!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!