• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling servlets

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
There was an interview question that I came across. Hope you guys can help me. The question is : Can Servlets be called from a stand-alone Java Application as other Java objects?
Many thanks in advance,
- Bhaskar
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi BashKar,
Yes. You may call a servlet from a standalone application.How ?
Here is an example i'm doing to call a servlet from a swing application :

HTTPConnection con = new HTTPConnection(new URL(Global.getBundleValue("DomainAddress")));
con.setAllowUserInteraction(false);
HTTPResponse rsp = con.Post("/eProtisWeb/fop", form_data);
if (rsp.getStatusCode() >= 300) {
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
{
.....
}
 
Bhaskar Reddy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Engin for a quick reply..
But I still didnt get how the Servlet is instantiated(??).
Another question : Can we run the servlet in command line using 'java MyServlet'?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
whenever you invoke a servlet from a standalone application , its similar to a request made from the browser . so think in that way , how the servlet container would handle a request from the browser.

Cheers
Palani kumar T
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I still didnt get how the Servlet is instantiated(??).
Another question : Can we run the servlet in command line using 'java MyServlet'


Servlets must run inside a "servlet container" that provides the vital services of creating a servlet instance, receiving a request, holding session data, etc. etc etc.
That said - with thoughtful architecture you can put the main functional parts of your servlet application into classes that can be run from the command line with the aid of a little interface code.
I'll give you an example - the parameters in a request can be obtained as a Map with getParameterMap(). This Map has entries keyed by the parameter name and with parameter value(s) in a String[]
If you write your processing code in a method that can take a Map like this, and write output to a PrintWriter, then the same code that runs in the servlet can be run outside the servlet environment.
Bill
 
Bhaskar Reddy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William for your answer!
I now u'stand that we CAN create something similar to the servlets by capturing the content of request object as a Map, work on the name/value pairs in a method and send the response to a PrintWriter object.
But what about the initialization of the servlet? Should we call the default no argument constructor (which means the init() method would be just like any other method)? And what about the ServletContext and ServletConfig objects? :roll:
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But what about the initialization of the servlet?


That is exactly the point - you should NOT try to initialize a servlet and run it outside a servlet container - you will just get all tangled up due to the absence of the services that the container normally provides.
Instead, test your functions separately - make a command line driven test program to instantiate your utility class and feed it test data.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic