• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

how to call sevlet from corejava program?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about creating an object of servlet and calling it's any method (doGet(), doPost() or service()) explicitly???
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic