• 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

can Servlet be Executed at command prompt ?

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic