Dmytro Plekhotkin
Dmytro Plekhotkin

Reputation: 1993

How to run java servlet?

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)

Upvotes: 8

Views: 27289

Answers (7)

Burhan ARAS
Burhan ARAS

Reputation: 2527

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.

Upvotes: 3

Karthikeyan Arumugam
Karthikeyan Arumugam

Reputation: 330

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.

Upvotes: 1

Jayesh
Jayesh

Reputation: 6111

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

Upvotes: 4

Foredoomed
Foredoomed

Reputation: 2239

  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.

Upvotes: 8

chaosguru
chaosguru

Reputation: 1983

here is an example by @BaluC

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

Upvotes: 1

mhaligowski
mhaligowski

Reputation: 2222

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.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240956

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

Upvotes: 2

Related Questions