Servlet Tutorial

Execute the Servlets

How to execute the servlets:

In order to execute a servlet we must follow the following directory structure:

Execute Servlet

Steps for DEVELOPING a servlet:

  1. Import javax.servlet.*, javax.servlet.http.* and other packages if required.
  2. Choose user defined class.
  3. Whichever class we have chosen in step-2 must extend either GenericServlet or HttpServlet.
  4. Override the life cycle methods if required.

FLOW OF EXECUTION in a servlet:

  1. Client makes a request. The general form of a request is http://(IP address or DNS [Domain Naming Service] name of the machine where server is installed) : (port number of the server) / (Document root) : (Resource name).

    For example:

    http://localhost:7001/DateSer/suman
  2. Server receives the request.
  3. Server will scan web.xml (contains declarative details) if the requested resource is not available in web.xml server generates an error called resource not available otherwise server goes to a servlet.
  4. Server will call the servlet for executing.
  5. Servlet will execute in the context of server.
  6. While server is executing a servlet, server loads an object of servlet class only once (by calling default constructor).
  7. After loading the servlet, the servlet will call init () method only once to perform one time operations.
  8. After completion of init () method, service () method will be called each and every time. As long as we make number of requests only service () method will be called to provide business logic.
  9. Servlet will call destroy () method either in the case of servlet is removed or in the case of server is closed.

HOW TO EXECUTE a servlet:

  1. Prepare a directory structure.
  2. Write a servlet program save it into either document root or document root\SRC.
  3. Compile a servlet by setting a classpath. For Tomcat: Set classpath=
  4. Copy *.class file into document root/WEB-INF/classes folder and write web.xml file.
  5. Start the server and copy document root into:
  6. Open the browser and pass a request or url