javax.servlet,http packages
TRANSCRIPT
Servlet
Differences between Generic and Http Servlet
• javax.servlet.GenericServletSignature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
javax.servlet.GenericServlet
Signature:public abstract class GenericServlet extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable• GenericServlet defines a generic, protocol-independent servlet.• GenericServlet uses service() method to handle request• GenericServlet gives a blueprint and makes writing servlet easier.• GenericServlet provides simple versions of the lifecycle methods
init and destroy and of the methods in the ServletConfig interface.• GenericServlet implements the log method, declared in the
ServletContext interface.• To write a generic servlet, it is sufficient to override the abstract
service method.
javax.servlet.http.HttpServlet
Signature: public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable• http is protocol dependent• HttpServlet defines a HTTP protocol specific servlet.• HttpServlet uses doGet and doPost methods to handle requests.
• HttpServlet gives a blueprint for Http servlet and makes writing them easier.
• HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.
Package javax.servlet.http
Interface Summary
HttpServletRequest Extends the ServletRequest interface to provide request information for HTTP servlets.
HttpServletResponse Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.
HttpSessionProvides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
HttpSessionActivationListenerObjects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated.
HttpSessionAttributeListenerThis listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
HttpSessionBindingListener Causes an object to be notified when it is bound to or unbound from a session.
HttpSessionContext Deprecated. As of Java(tm) Servlet API 2.1 for security reasons, with no replacement.
HttpSessionListener Implementations of this interface are notified of changes to the list of active sessions in a web application.
Class Summary
CookieCreates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server.
HttpServlet Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.
HttpServletRequestWrapperProvides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet.
HttpServletResponseWrapperProvides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet.
HttpSessionBindingEvent
Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session.
HttpSessionEvent This is the class representing event notifications for changes to sessions within a web application.
HttpUtils Deprecated. As of Java(tm) Servlet API 2.3.
Generic Servlet
• javax.servlet Class GenericServlet
• java.lang.Object javax.servlet.GenericServlet All Implemented Interfaces: java.io.Serializable, Servlet, ServletConfig
import java.io.IOException; import java.io.PrintWriter;import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;public class GenericServletExample extends GenericServlet { public void init() { log("inside init() method"); } public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException { log("Handling request"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("<html><head><title>GenericServle example</title></head>");
out.write("<body><h1>GenericServlet: Hallo world </h1></body></html>"); out.close(); } public void destroy() { log("inside destroy() method"); } }
HttpServletimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class Hello extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>");out.println("<h1>hello</h1>"); out.println("</body>"); out.println("</html>"); }}
Compiling Hello.javaC:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\lalitha\WEB-INF\
classes>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar" Hello.java
Runhttp://localhost:8080/lalitha/Hello
<web-app><servlet> <servlet-name>Hello</servlet-name> <servlet-class>Hello</servlet-class> </servlet>
<servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping></web-app>
Servlet Life Cycle
• A servlet is basically a small Java program that runs within a Web server.
• It can receive requests from clients and return responses.
• The whole life cycle of a servlet breaks up into 3phases:• Initialization: A servlet is first loaded and initialized
usually when it is requested by the corresponding clients.
• Service: After initialization, the servlets serve clients on request, implementing the application logic of the web application they belong to.
• Destruction: When all pending requests are processed and the servlets have been idle for a specific amount of time, they may be destroyed by the server and release all the resources they occupy.
More specifically, the behavior of a servlet is described in javax.servlet.Servlet interface, in which the following methods are defined:
• public void init(ServletConfig config) throws ServletExceptionThis method is called once when the servlet is loaded into the servlet engine, before the servlet is asked to process its first request.The init method has a ServletConfig parameter. The servlet can read its initialization arguments through the ServletConfig object. How the initialization arguments are set is servlet engine dependent but they are usually defined in a configuration file.
• public void service(ServletRequest request, ServletResponse response) throwsServletException, IOException
This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded. Once a servlet is loaded, it remains in the server’s memory as a single object instance.
• public void destroy()This method is called once just before the servlet is unloaded and taken out of service.
The following servlet presents information about how many times it has beenaccessed:
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class SimpleCounter extends HttpServlet {int count = 0;public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {res.setContentType("text/plain");PrintWriter out = res.getWriter();count++;out.println("Since loading, this servlet has been accessed " +count + " times.");}}
import java.io.*; import javax.servlet.*; public class HelloServlet extends GenericServlet {
public void init ( ServletConfig config ) { super.init ( config ); } public void service ( ServletRequest
req, ServletResponse res ) throws ServletException, IOException {
PrintStream out = new PrintStream ( res.getOutputStream ( ) ); out.println ( "Hello, World!" ); } public void destroy ( ) {
super.destroy ( ); } }