Wednesday, 26 June 2013

Creating servlet in eclipse with Tomcat

1. Create Dynamic Web Project
   Select from the menu File --> New --> Dynamic Web Project.
  

 2. Check 'Generate web.xml deployment descriptor' checkbox and click "Finish" button and Eclipse IDE will  generate the web project  automatically as shown below




3. Project "helloworld" is created.'


4. You need to add the Servlet API to your classpath.
    refer http://lekshmideepu.blogspot.in/2013/06/the-import-javaxservlet-cant-be-resolved.html
5. Create a Servlet Class
    Create a package under src in your project (say com.hello)
     Right click the package, select new-->servlet.(say HelloWorldServlet as class name).
     Click next and finish

6. Add the below code in 'doGet' method of HelloWorldServlet

          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

         So your HelloWorldServlet  looks like below

package com.hello;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorldServlet
 */
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public HelloWorldServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}



7. Create Servlet Mapping in Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
        <display-name>helloworldservlet</display-name>
        <servlet-name>helloservlet</servlet-name>
        <servlet-class>com.hello.HelloWorldServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>helloservlet</servlet-name>
     <url-pattern>/hello</url-pattern>
  </servlet-mapping>

 
</web-app>

  
9. Add your project to "Tomcat" server and start the server

8. Open a browser and type url:
   http://localhost:8080/helloworld/hello