8

I have a servlet. I'm new in java. But i need to run the servlet. It has two methods:

public void doGet (HttpServletRequest request,
                     HttpServletResponse response) {...}

and

public void doPost HttpServletRequest request,
                     HttpServletResponse response) {...}

What steps i need to do to run the servlet? (I have tomcat 7 installed, eclipse SE with tomcat plugin, netBeans)

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47
  • What I feel is, the question you have asked needs to be something like how client-server application works, that ways Servlet will come in picture and you will come to know the complete scenario of client side and server side response. this link gives you complete flow as beginner http://en.wikipedia.org/wiki/Java_Servlet. – Jayesh Sep 26 '12 at 07:54
  • Also very good explanation what to do is here: http://stackoverflow.com/questions/206924/how-do-you-develop-java-servlets-using-eclipse – Dmytro Plekhotkin Sep 26 '12 at 08:13

7 Answers7

8
  1. Create a dynamic web project
  2. Create a new class extending HttpServlet and override the method doGet and doPost, write your business logic in there
  3. Configure web.xml, something like :

      <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>test.helloworld</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/helloworld</url-pattern>
      </servlet-mapping>
    
  4. Deploy your web project in the tomcat

  5. Type localhost:8080/mywebapp/helloworld.do in the browser's address bar, mywebapp is your project name

If you are lucky, you will see the result.

Foredoomed
  • 2,219
  • 2
  • 21
  • 39
  • is there a way to do this without tomcat? There used to be a slim untility called servletrunner.exe . I cannot find it in my Java installation. I tried installing JSDK, but it has not 64 but version – Abhishek Anand Feb 27 '14 at 11:46
  • @Abhishek you have to run a servlet program with a servlet container, such as Tomcat, Jetty, etc. Sorry, i have no idea with servletrunner. – Foredoomed Feb 27 '14 at 14:06
4

Internally call to doGet and doPost will reach like below,

Client ----------------------------> Container  
sends request               |
                            |
                Creates    HttpServletRequest   HttpServletResponse objects 
                            |
                            |                   
                Create Thread for that Servlet and pass above objects to it
                            |
                            |
                Thread Call the Service() method and decision is made to call doGet() or doPost()
                            |
                            |
                    doGet()/doPost() called
Jayesh
  • 6,047
  • 13
  • 49
  • 81
3

I suggest you that :

  1. Open netbeans and create a new web project
  2. Right click the project, add a Servlet
  3. Right click the project and select Run. It will run web app on Glassfish.
  4. It will automatically open your web browser and navigate to servlet address like : localhost:8080/MyServlet etc.

This is the quickest way to run a servlet. have fun.

Burhan ARAS
  • 2,517
  • 25
  • 19
2

Create a java web project with a IDE (Netbeans/eclipse) add a servlet to the project, It will make your life easier

jmj
  • 237,923
  • 42
  • 401
  • 438
2

It seems that you know little about Java EE and Servlets.

Basically, you need to write a web.xml file, which will map an URL to your servlet, build the project, create a web archive (WAR), deploy it on server.

Here's the official manual from Oracle: http://docs.oracle.com/javaee/6/tutorial/doc/bnadp.html.

Try to google for using servlets on tomcat, you will surely find a good tutorial on that.

mhaligowski
  • 2,182
  • 20
  • 26
1

here is an example by @BaluC

http://balusc.blogspot.in/2007/04/imageservlet.html

chaosguru
  • 1,933
  • 4
  • 30
  • 44
1

This is very basic question dude!

You can learn how to do it on Eclipse with this Tutorial link.

Please try learning from some nice books. Many nice Java EE books are available on the market.

Or you can learn Java EE from the oracle Site as well.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140