aspose file tools*
The moose likes Servlets and the fly likes Calling servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling servlets" Watch "Calling servlets" New topic
Author

Calling servlets

Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
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
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
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

Joined: Dec 17, 2002
Posts: 52
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'?
Palanikumar Thangapandian
Greenhorn

Joined: Feb 15, 2004
Posts: 1
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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


Java Resources at www.wbrogden.com
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
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
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling servlets
 
Similar Threads
Upload files through HTTP call through client
Convertion from Resource bundle to Class Loader
Is there any difference between servlet request object and servletActionContext request object
Does it make any need to have static variables inside a Servlet
compilation error