How to execute the servlets:
In order to execute a servlet we must follow the following directory structure:
Steps for DEVELOPING a servlet:
- Import javax.servlet.*, javax.servlet.http.* and other packages if required.
- Choose user defined class.
- Whichever class we have chosen in step-2 must extend either GenericServlet or HttpServlet.
- Override the life cycle methods if required.
FLOW OF EXECUTION in a servlet:
- 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).
- Server receives the request.
- 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.
- Server will call the servlet for executing.
- Servlet will execute in the context of server.
- While server is executing a servlet, server loads an object of servlet class only once (by calling default constructor).
- After loading the servlet, the servlet will call init () method only once to perform one time operations.
- 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.
- 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:
- Prepare a directory structure.
- Write a servlet program save it into either document root or document root\SRC.
- Compile a servlet by setting a classpath.
For Tomcat: Set classpath=
- Copy *.class file into document root/WEB-INF/classes folder and write web.xml file.
- Start the server and copy document root into:
- Open the browser and pass a request or url