File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes can Servlet be Executed at command prompt ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "can Servlet be Executed at command prompt ?" Watch "can Servlet be Executed at command prompt ?" New topic

can Servlet be Executed at command prompt ?

Shantanu Puranik
Ranch Hand

Joined: Jun 10, 2004
Posts: 67
Is it possible to execute servlet at command prompt?
This question was asked in the interview to my friend.

As far as my knowldge goes, one can create an instance of servlet. but, calling service method requires Request & Response Objects too.
is there any way to create those?

what could be the purpose of doing so?


--- Shantanu
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Servlet are based on request/response mechanism and run inside of a web container.Servlet is not a standalone program.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63869

Depends what you mean by "execute"...

For example, you can "hit" a servlet with wget from the command line.

[Asking smart questions] [About Bear] [Books by Bear]
Shantanu Puranik
Ranch Hand

Joined: Jun 10, 2004
Posts: 67
Hello Bear ,
are you saying that you can call doGet method but no response is generated?

** you can "hit" a servlet with wget from the command line **
will you plz. elaborate this for me?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Think of servlets as an extension or component of an application server.
They don't work on thier own. You couldn't just call them with the 'java' command at a dos or shell program.

The program wget is a non-gui web client that can be run from the command line or from within a shell script. It's main use is for synching up mirror sites.

So, while you can't run a servlet as a standalone java application from the command line, you can use a command line browser to hit a servlet if it's running in a container.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Although it may not be what you were looking for, don't forget that a Servlet is still a Java class. You can put a "main" method in it and call any methods that you want, as long as you can give it the right parameters.

I sometimes use this approach if I need to prform ad-hoc tests of some of the code in a Servlet. It's just a shame how the creators of the Servlet API made it so difficult to "mock" the Request and Response objects.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If you find yourself with methods in a servlet class that need to be tested from a command line, you should take that as a indication that it might be a good idea to refactor the class. If you can create one or more "helper" classes that can be tested outside the servlet environment, it speeds up development and testing.

You can emulate the user data that comes in a ServletRequest with a Map - see the ServletRequest getParameterMap() method. You can use a PrintWriter or OutputStream to get the response.

I agree. Here's the link:
subject: can Servlet be Executed at command prompt ?
It's not a secret anymore!