Servlet 01


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Servlet 01

  1. 1. <ul><li>Objectives </li></ul><ul><li>In this lesson, you will learn to: </li></ul><ul><li>Identify the characteristics of a Servlet </li></ul><ul><li>Create a Servlet </li></ul><ul><li>Deploy a Servlet </li></ul>
  2. 2. <ul><li>Need for Servlets </li></ul><ul><li>Servlets are required to: </li></ul><ul><ul><li>Reduce the overhead on the server and network </li></ul></ul><ul><ul><li>To take care of processing data on the Web server </li></ul></ul>
  3. 3. <ul><li>Servlets are: </li></ul><ul><ul><li>Java programs that can be deployed on a Java enabled Web server </li></ul></ul><ul><ul><li>Used to extend the functionality of a Web server </li></ul></ul><ul><ul><li>Used to add dynamic content to Web pages </li></ul></ul>
  4. 4. <ul><li>Characteristics of Servlets </li></ul><ul><li>Servlets are: </li></ul><ul><li>Efficient </li></ul><ul><ul><li>Initialization code for a servlet is executed only once </li></ul></ul><ul><li>Robust </li></ul><ul><ul><li>Provide all powerful features of Java </li></ul></ul>
  5. 5. <ul><li>Portable across Web servers </li></ul><ul><li>Persistent </li></ul><ul><ul><li>Increase the performance of a system by preventing frequent disk access </li></ul></ul>
  6. 6. <ul><li>Comparison between Servlets and Applets </li></ul><ul><li>Applets: </li></ul><ul><ul><li>Are embedded in Web pages </li></ul></ul><ul><ul><li>Require the browser to be Java-enabled </li></ul></ul><ul><ul><li>Take a lot of time to be downloaded </li></ul></ul>
  7. 7. <ul><li>Servlets: </li></ul><ul><ul><li>Execute on the Web server, thus help overcome problems with download time </li></ul></ul><ul><ul><li>Do not require the browser to be Java-enabled </li></ul></ul>
  8. 8. <ul><li>Comparison between Servlets and other Server- </li></ul><ul><li>Side Scripting Technologies </li></ul><ul><li>Common Gateway Interface (CGI) scripts, JSP, and ASP are alternatives to servlets </li></ul><ul><li>CGI Scripts: </li></ul><ul><ul><li>Are programs written in C, C++, or Perl </li></ul></ul><ul><ul><li>Get executed in a server </li></ul></ul><ul><ul><li>Run in separate processes for each client access </li></ul></ul><ul><ul><li>Require the interpreter to be loaded on the server </li></ul></ul><ul><li>A JSP file is automatically converted to a servlet before it is executed </li></ul>
  9. 9. <ul><li>Comparison between Servlets and other Server- Side Scriptings </li></ul><ul><li>Active Server Pages (ASP): </li></ul><ul><ul><li>Is a server-side scripting language developed by Microsoft </li></ul></ul><ul><ul><li>Enables a developer to combine HTML and a scripting language in the same Web page </li></ul></ul><ul><ul><li>Are not compatible with all Web servers </li></ul></ul>
  10. 10. <ul><li>Working of Servlets </li></ul><ul><li>Client browser passes requests to a servlet using the following methods: </li></ul><ul><li>GET </li></ul><ul><ul><li>Uses a query string to send additional information to the server </li></ul></ul><ul><ul><li>Query string is displayed in the client browser </li></ul></ul>
  11. 11. <ul><li>POST </li></ul><ul><ul><li>Sends the data as packets to the server through a separate socket connection </li></ul></ul><ul><ul><li>Complete transaction is invisible to the client </li></ul></ul><ul><ul><li>Slower compared to the GET method </li></ul></ul>
  12. 12. <ul><li>The javax.servlet Package </li></ul><ul><li>Hierarchy of classes that are used to create a servlet </li></ul>Class java.lang.Object Class javax.servlet.GenericServlet Class javax.servlet.HttpServlet Interface javax.servlet.Servlet Interface javax.servlet.ServletConfig Interface
  13. 13. <ul><li>The javax.servlet Package (Contd.) </li></ul><ul><li>A brief description of the classes and interfaces </li></ul>Provides methods to process requests from the clients. HTTPServletRequest interface Provides a HTTP specific implementation of the Servlet interface. HTTPServlet class Description Class/Interface
  14. 14. Response to the client is sent in the form of a HTML page through an object of the HTTPServletResponse class. HTTPServlet Response interface Used to store the servlets startup configuration values and the initialization parameters. ServletConfig class
  15. 15. <ul><li>Life Cycle of a Servlet </li></ul><ul><li>Life cycle of a servlet: </li></ul>Client (Browser) init() service() destroy() Request Response
  16. 16. <ul><li>Life Cycle of a Servlet (Contd.) </li></ul><ul><li>The following table describes few methods that are used in creating a servlet: </li></ul>Receives all requests from clients, identifies the type of the request, and dispatches them to the doGet() or doPost() methods for processing. Servlet. service() Contains all initialization code for the servlet. Servlet.init (ServletConfig config) throws ServletException Functionality Method name
  17. 17. <ul><li>Life Cycle of a Servlet (Contd.) </li></ul>Executes only once when the servlet is removed from server. Servlet. destroy() Sets the type of content that is to be sent as response to the client browser. HTTPServlet Response. setContentType (String type) Returns a reference to a PrintWriter object. HTTPServlet Response. getWriter() Functionality Method name
  18. 18. <ul><li>Deploying a Servlet </li></ul><ul><li>A servlet can be deployed in: </li></ul><ul><ul><li>Java Web Server (JWS) </li></ul></ul><ul><ul><li>JRun </li></ul></ul><ul><ul><li>Apache </li></ul></ul><ul><ul><li>Java 2 Enterprise Edition (J2EE) server </li></ul></ul>
  19. 19. <ul><li>Identify the mechanism to record the </li></ul><ul><li>number of hits on a Web site </li></ul><ul><li>Client browsers that are used to access the Web site run on different machines </li></ul><ul><ul><li>Hence, the client (browser) cannot keep track of the hit count data </li></ul></ul><ul><li>This data has to be captured on the server’s side </li></ul><ul><ul><li>Thus, a technique that can be used to write a server-side program is necessary to solve this problem </li></ul></ul>
  20. 20. <ul><li>Identify the classes to be used </li></ul><ul><li>A class that extends the HttpServlet class must be coded to solve the problem </li></ul>
  21. 21. <ul><li>Identify the methods to be used </li></ul><ul><li>The init() method needs to be coded in the hitcountServlet class to initialize the hitcounter to zero </li></ul><ul><li>The doGet() method needs to be coded to increment the hit counter whenever a client browser makes a request for the site home page </li></ul>
  22. 22. <ul><li>Write and compile the servlet </li></ul><ul><li>Set the values of PATH and CLASSPATH variables </li></ul><ul><li>Code the servlet program </li></ul><ul><li>Compile the program by using the command javac </li></ul>
  23. 23. <ul><li>Execute the servlet </li></ul><ul><li>Type in the address bar of the browser to execute the servlet </li></ul>
  24. 24. <ul><li>Problem for you…. </li></ul><ul><li>In the URL that is provided, identify the Web context name and the alias name of the servlet </li></ul><ul><li> </li></ul>
  25. 25. <ul><li>Summary </li></ul><ul><li>In this lesson, you learned that: </li></ul><ul><li>Servlets are server-side Java programs that can be deployed on a Web Server </li></ul><ul><li>The Servlet interface provides the basic framework for coding servlets </li></ul>
  26. 26. <ul><li>Servlets are : </li></ul><ul><ul><li>Portable </li></ul></ul><ul><ul><li>Extensible </li></ul></ul><ul><ul><li>Persistent </li></ul></ul><ul><ul><li>Robust </li></ul></ul>
  27. 27. <ul><li>Summary (Contd.) </li></ul><ul><li>The life cycle of a servlet is composed of the init() , service() , and the destroy() methods </li></ul><ul><li>Servlets can be deployed in the J2EE server and JWS </li></ul><ul><li>The deploytool command is used to deploy a servlet on a J2EE server </li></ul>