Javax.servlet,http packages

Uploaded on


  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Servlet
  • 2. Differences between Generic and Http Servlet• javax.servlet.GenericServlet Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig,
  • 3. javax.servlet.GenericServletSignature:public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig,• 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.
  • 4. javax.servlet.http.HttpServletSignature: public abstract class HttpServlet extends GenericServlet implements• 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.
  • 5. Package javax.servlet.httpInterface Summary Extends the ServletRequest interface to provide requestHttpServletRequest information for HTTP servlets. Extends the ServletResponse interface to provide HTTP-HttpServletResponse specific functionality in sending a response. Provides a way to identify a user across more than one pageHttpSession request or visit to a Web site and to store information about that user. Objects that are bound to a session may listen to containerHttpSessionActivationListener events notifying them that sessions will be passivated and that session will be activated. This listener interface can be implemented in order to getHttpSessionAttributeListener notifications of changes to the attribute lists of sessions within this web application. Causes an object to be notified when it is bound to orHttpSessionBindingListener unbound from a session. Deprecated. As of Java(tm) Servlet API 2.1 for securityHttpSessionContext reasons, with no replacement. Implementations of this interface are notified of changes toHttpSessionListener the list of active sessions in a web application.
  • 6. Class Summary Creates a cookie, a small amount of information sent by a servletCookie to a Web browser, saved by the browser, and later sent back to the server. Provides an abstract class to be subclassed to create an HTTPHttpServlet servlet suitable for a Web site. Provides a convenient implementation of the HttpServletRequestHttpServletRequestWrapper interface that can be subclassed by developers wishing to adapt the request to a Servlet. Provides a convenient implementation of theHttpServletResponseWrapper HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet. Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from aHttpSessionBindingEvent session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session. This is the class representing event notifications for changes toHttpSessionEvent sessions within a web application.HttpUtils Deprecated. As of Java(tm) Servlet API 2.3.
  • 7. Generic Servlet• javax.servlet Class GenericServlet• java.lang.Object javax.servlet.GenericServlet All Implemented Interfaces:, Servlet, ServletConfig
  • 8. import;import;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"); } }
  • 9. HttpServletimport*;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>"); }}
  • 10. Compiling Hello.javaC:Program FilesApache Software FoundationTomcat 5.5webappslalithaWEB- INFclasses>javac -classpath "C:Program FilesApache Software FoundationTomcat 5.5commonlibservlet-api.jar" Hello.javaRunhttp://localhost:8080/lalitha/Hello
  • 11. <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>
  • 12. 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.
  • 13. • 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.
  • 14. 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 ServletException This 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.
  • 15. The following servlet presents information about how many times it has been accessed:import*;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.");}}
  • 16. import*; 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 ( ); }}