• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can Servlet be Executed at command prompt ?

 
Shantanu Puranik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what you mean by "execute"...

For example, you can "hit" a servlet with wget from the command line.
 
Shantanu Puranik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic