1. Create Dynamic Web Project as follows.
  2. CreateDynamicwebproject

  3. Enter project name as “WebApp” as follows and click on Next>>Next button.
  4. ProjectName

    web

  5. Click on Finish.Project will be created.
  6. projectCreated

  7. Crating package,Select Src folder and then New and then Package.
  8. CreatingPackage

  9. Enter the package name called “com.java2learn” and then click on Finish.
  10. CreatePackage

  11. Create a class that implements Servlet interface.
  12. CompileTimeError

  13. To remove above compile time error ,Add the servlet-api.jar to class path as follows.

    Select Configure Build Path.

  14. ConfigureBuildPath

  15. Click on Add External JAR’s, and Select servlet-api.jar (download servlet-api) form your local system.
  16. ExternalJars

    jarfileAdded

  17. Complete the FirstServlet.java class.
  18. package com.java2learn;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class FirstServlet implements Servlet {
    
    	public void init(ServletConfig conf) throws ServletException {
    		System.out.println("init method");
    	}
    
    	public void service(ServletRequest req, ServletResponse resp)
    			throws ServletException, IOException {
    		System.out.println("service()");
    		PrintWriter out = resp.getWriter();
    		out.println("welcome to servlets");
    	}
    
    	public void destroy() {
    		System.out.println("destory");
    	}
    
    	public ServletConfig getServletConfig() {
    		return null;
    	}
    
    	public String getServletInfo() {
    		return "written by Java2Learn";
    	}
    
    }
    
  19. Create the web.xml as follows.
  20. <?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_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    	<display-name>WebApp</display-name>
    
    	<display-name>WebApp</display-name>
    	<servlet>
    		<servlet-name>FirstServlet</servlet-name>
    		<servlet-class>com.java2learn.FirstServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>FirstServlet</servlet-name>
    		<url-pattern>/firstServlet</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    
  21. Final project structure as follows
  22. FinalProjectStructure

  23. Create the war file.
  24. CreateWarFile

  25. In order to run a web application, the web application must be deployed in the container.
    We must copy the war file in webapps directory of tomcat.
    C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps
  26. Accessing servlet from web application .Open the browser and type the following URL in the address bar
  27. http://localhost:9091/WebApp/firstServlet

  28. Output of the servlet
  29. ServletOutput