Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we call a Servlet from a Java Class

 
Chaitanya Varanasi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,So Kind of You, Ulf Dittmer

Chaitanya V
 
kiranreddy reddy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandip Chaudhuri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic