1. Create a web application structure as follows.(To know more about structure of web application )
  2. WebApp

  3. Create a FirstServlet class by implementing Servlet interface.
  4. import java.io.PrintWriter;
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.ServletException;
    import java.io.IOException;
    public class FirstServlet implements Servlet {
    
    	static {
    	  System.out.println("servlet loading");
    	}
    
    	public FirstServlet() {
    	 System.out.println("servlet initialization");
    	}
    
    	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";
    	}
    }
    
  5. Copy FirstServlet.java into folder let’s say F:\SERVLETS/
  6. FirstServletFile

  7. Go to the above the directory and compile the FirstServlet.java
  8. CompileTimeError

  9. To remove the above compile time error, We should set the CLASSPATH for servlet-api.jar
  10. Copy the servlet-api.jar file into F:\SERVLETS/ and set the CLASSPATH as follows and then compile the FirstServlet.java file, It will generate FirstServlet.class file.
  11. FirstServletFile

  12. Copy FirstServlet.class file into classes folder of WebApp directory(1st step)
  13. Create the web.xml file
  14. <?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">
      <display-name>WebApp</display-name>
      <servlet>
       <servlet-name>FirstServlet</servlet-name>
       <servlet-class>FirstServlet</servlet-class>
      </servlet>
      <servlet-mapping>
       <servlet-name>FirstServlet</servlet-name>
       <url-pattern>/firstServlet</url-pattern>
      </servlet-mapping>
     </web-app>
    

    <servlet-name>FirstServlet</servlet-name>
    Name of the servlet(We can give any name).

    <servlet-class>FirstServlet</servlet-class>
    .class file name(FirstServlet)

    <url-pattern>/firstServlet</url-pattern>
    To access the servlet from browser.

    Note:
    <servlet-name> of the <servlet> tag and <servlet-mapping> tag should be same.

  15. Copy the web.xml file into WEB-INF folder.
  16. Final Application structure as follows.
  17. Dynamic

  18. Create a war(web archive) using the jar command.
  19. CreateWarCommand

  20. 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
  21. Accessing servlet from web application .Open the browser and type the following URL in the address bar
  22. http://localhost:9091/WebApp/firstServlet

  23. Output of the servlet
  24. ServletOutput

  25. Output of the server console
  26. serverConsole

Download Project:WebApp
Download War:WebApp.War