Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Knowledge Sharing : Java Servlet


Published on

Knowledge sharing on Java Servlet material.

Published in: Technology, Education

Knowledge Sharing : Java Servlet

  1. 1. Knowledge Sharing
  2. 2. Agenda <ul><li>What is Servlet? </li></ul><ul><li>Servlet hierarchy & lifecycle </li></ul><ul><li>Servlet program structure </li></ul><ul><li>Deploying servlets on Tomcat </li></ul><ul><li>HTTP Servlets and HTTP request methods </li></ul><ul><li>Understanding servlet API </li></ul><ul><li>Responding to requests </li></ul><ul><li>Accessing form input data </li></ul><ul><li>Working with header fields </li></ul><ul><li>URL redirecting </li></ul>
  3. 3. What is servlet? <ul><li>A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests. </li></ul><ul><li>A Servlet is an object that receives a request and generates a response based on that request </li></ul><ul><li>Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET </li></ul><ul><li>( ) </li></ul>
  4. 4. Advantages of Servlets <ul><li>Faster than CGI scripts because use a different process model. </li></ul><ul><li>Use standard API that is supported by many Web servers. </li></ul><ul><li>Have all of the advantages of the Java languages, including ease of development and platform independence. </li></ul><ul><li>Can access the large set of APIs available for the Java platform. </li></ul><ul><li>( ) </li></ul>
  5. 5. Servlet V.S. CGI <ul><li>A s ervlet stays in memory between requests. A CGI program needs to be loaded and started for each CGI request . </li></ul><ul><li>A servlet doesn’t run in a separate process. This removes the overhead of creating a new process for each request which CGI does. </li></ul><ul><li>A servlet allows each request to be handled by a separate Java thread within the web server process (same amount of threads as request but there only be one copy of the servlet class crea ted in memory). </li></ul><ul><li>( ) </li></ul>
  6. 6. Servlet Hierarchy Servlets (javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (javax.servlet.http.HttpServlet) MyServlet
  7. 7. Servlet Lifecycle
  8. 8. Servlet Lifecycle (cont.)
  9. 9. Servlet Program Structure
  10. 10. [Screencast] Create simple servlet
  11. 11. [Showcase] Servlet Lifecycle
  12. 12. [Showcase] Servlet Lifecycle (cont.)
  13. 13. [Showcase] Servlet Lifecycle (cont.)
  14. 14. Deploying Servlets on Tomcat <ul><li>Create a servlet </li></ul><ul><li>Compile the servlet : </li></ul><ul><ul><li>No different from compiling java program </li></ul></ul><ul><ul><li>( javac ) </li></ul></ul><ul><ul><li>javax.servlet.* and javax.servlet.http.* must be added in Classpath. </li></ul></ul><ul><li>Create web application folder under webapps folder in Tomcat (web container). </li></ul><ul><li>Create WEB-INF folder under web application folder. </li></ul>
  15. 15. Deploying Servlets on Tomcat (cont) <ul><li>Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. </li></ul><ul><li>Copy the servlet class file into ‘classes’ folder </li></ul><ul><li>Edit web.xml to include servlet’s name and url pattern . </li></ul>
  16. 16. Deploying Servlets on Tomcat (cont) <ul><li>Run Tomcat server and then execute the Servlet </li></ul>
  17. 17. HTTP Servlet and HTTP request method
  18. 18. HTTP Servlet and HTTP request method (cont.) <ul><li>HTTP is a request-response oriented protocol. </li></ul><ul><li>An HTTP request consists of : </li></ul><ul><ul><li>Request method ( GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE) </li></ul></ul><ul><ul><li>URI </li></ul></ul><ul><ul><li>Header fields </li></ul></ul><ul><ul><li>Body </li></ul></ul><ul><li>An HTTP response contains : </li></ul><ul><ul><li>Result code </li></ul></ul><ul><ul><li>Header fields and body </li></ul></ul>
  19. 19. Understanding Servlet API <ul><li>Packages in the Java Servlet API 2.1 </li></ul>
  20. 20. Understanding Servlet API (cont) <ul><li>Packages in the Java Servlet API 2.1 </li></ul>
  21. 21. Request Heade r <ul><li>Accept </li></ul><ul><li>Accept-charset </li></ul><ul><li>Accept-encoding </li></ul><ul><li>Accept-language </li></ul><ul><li>Authorization </li></ul><ul><li>Connection </li></ul><ul><li>Content-length </li></ul><ul><li>Cookie </li></ul>The most common headers : <ul><li>From </li></ul><ul><li>Host </li></ul><ul><li>If-modified-since </li></ul><ul><li>Pragma </li></ul><ul><li>Referer </li></ul><ul><li>User-agent </li></ul><ul><li>UA-Pixels, UA-color, UA-OS, UA-CPU </li></ul>
  22. 22. Request Heade r (cont.) <ul><li>Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. </li></ul><ul><li>Call the getHeaderNames() to get an Enumeration of all header names received on the particular request. </li></ul>Reading request header from Servlets :
  23. 23. Accessing form input data <ul><li>Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument. </li></ul><ul><li>Call getParameterValues() method to get the parameter that probably have more than one value. </li></ul><ul><li>Call getParameterNames() to get full list of all parameters received on the particular request. </li></ul>
  24. 24. URL Redirecting <ul><li>Using sendRedirect() method of HttpServletResponse class. </li></ul><ul><li>Using forward() method of RequestDispatcher class. </li></ul>
  25. 25. URL Redirecting (cont.) <ul><li>sendRedirect() It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value) </li></ul><ul><li>RequestDispatcher The target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute(). </li></ul>
  26. 26. URL Redirecting (cont.) <ul><li>sendRedirect() It will updates the browser history. </li></ul><ul><li>RequestDispatcher If use RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3. </li></ul><ul><li>Both kinds of redirections are useful, depending on the precise effect you want. </li></ul>
  27. 27. Thank you Fahmi Jafar (