File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Can we call a Servlet from a Java Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we call a Servlet from a Java Class" Watch "Can we call a Servlet from a Java Class" New topic
Author

Can we call a Servlet from a Java Class

Chaitanya Varanasi
Greenhorn

Joined: Dec 09, 2004
Posts: 26
Hi All,
First of all HAPPY NEW YEAR. I have a simple doubt. Just when I was reading about Servlets...a silly doubt dropped out of my head.....My Doubt is Is it Possible to call the Servlet from a Plain Old Java Class

I would be happy if you could tell me this and if Yes , how? if not Why not?

Thanks in Advance
Chaitanya V
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
That is possible. In the simplest case, you can use the java.net.URL class, and retrieve a page with the getContent method. If you need more control over the connection, have a look at the URLConnection and HttpUrlConnection classes, or possibly even a library like Jakarta Commons HttpClient.
Chaitanya Varanasi
Greenhorn

Joined: Dec 09, 2004
Posts: 26
Thank you,So Kind of You, Ulf Dittmer

Chaitanya V
kiranreddy reddy
Greenhorn

Joined: Jan 02, 2006
Posts: 11
As previously posted its not possible,

one reason I that comes to my mind, is a servlet has to follow its lifecycle, first init method, then doGet or doPost method shud be called, but neither of them can be called with out the HttpSevletRequest and HttpServletResponse objects, which can not be created by POJOs

Thats one thing I feel its not possible, but I am not if this is the right way to think bout that
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61762
    
  67

It depends what is meant by "call". If you mean "call " as in normal method invocation, then that makes no sense. Can you just call a servlet method like doPost? Yes. But who is going to set up the context and the request and response objects? Makes no sense.

If you mean can you invoke a servlet running in a servlet container somewhere on the web and retrieve its response? The yes. The URL and URLConnection classes can be used to do that.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
i think there is some jUnit which is used to test struts action classes.
they create the servlet requestand response so this is definately possible.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61762
    
  67

Originally posted by Sandip Chaudhuri:
i think there is some jUnit which is used to test struts action classes.
they create the servlet requestand response so this is definately possible.


Well of course it's possible -- otherwise it wouldn't be possible to write servlet containers (or testers). But unless you are writing a servlet container or testbed, what's the point?
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Originally posted by kiranreddy reddy:
As previously posted its not possible,

one reason I that comes to my mind, is a servlet has to follow its lifecycle, first init method, then doGet or doPost method shud be called, but neither of them can be called with out the HttpSevletRequest and HttpServletResponse objects, which can not be created by POJOs

Thats one thing I feel its not possible, but I am not if this is the right way to think bout that



We can call a Servlet from an java file . just like from

Applet .we can use URL & URLConnection class .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we call a Servlet from a Java Class