0
Knowledge Sharing
Agenda <ul><li>What is Servlet? </li></ul><ul><li>Servlet hierarchy & lifecycle </li></ul><ul><li>Servlet program structur...
What is servlet? <ul><li>A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond...
Advantages of Servlets <ul><li>Faster than CGI scripts because use a different process model. </li></ul><ul><li>Use standa...
Servlet V.S. CGI <ul><li>A  s ervlet stays in memory between requests. A CGI program   needs to be loaded and started for ...
Servlet Hierarchy Servlets (javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (...
Servlet Lifecycle
Servlet Lifecycle (cont.)
Servlet Program Structure
[Screencast] Create simple servlet
[Showcase] Servlet Lifecycle
[Showcase] Servlet Lifecycle (cont.)
[Showcase] Servlet Lifecycle (cont.)
Deploying Servlets on Tomcat <ul><li>Create a servlet </li></ul><ul><li>Compile the servlet : </li></ul><ul><ul><li>No dif...
Deploying Servlets on Tomcat (cont) <ul><li>Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. </li></ul><ul><li>Co...
Deploying Servlets on Tomcat (cont) <ul><li>Run Tomcat server and then execute the Servlet </li></ul>http://www.roseindia....
HTTP Servlet and HTTP request method
HTTP Servlet and HTTP request method (cont.) <ul><li>HTTP is a request-response oriented protocol. </li></ul><ul><li>An HT...
Understanding Servlet API <ul><li>Packages in the Java Servlet API 2.1 </li></ul>http://java.sun.com/products/servlet/2.1/...
Understanding Servlet API (cont) <ul><li>Packages in the Java Servlet API 2.1 </li></ul>http://java.sun.com/products/servl...
Request  Heade r <ul><li>Accept </li></ul><ul><li>Accept-charset </li></ul><ul><li>Accept-encoding </li></ul><ul><li>Accep...
Request  Heade r (cont.) <ul><li>Call the getHeader() method of the HttpServletRequest, which returns a String if the head...
Accessing form input data <ul><li>Call getParameter() method of the HttpServletRequest, supplying the parameter name (case...
URL Redirecting <ul><li>Using sendRedirect() method of HttpServletResponse class. </li></ul><ul><li>Using forward() method...
URL Redirecting (cont.) <ul><li>sendRedirect()  It is a new request from the client, and the way to pass data is through t...
URL Redirecting (cont.) <ul><li>sendRedirect()  It will updates the browser history.  </li></ul><ul><li>RequestDispatcher ...
Thank you Fahmi Jafar (fahmijafar@fahmijafar.net)
Upcoming SlideShare
Loading in...5
×

Knowledge Sharing : Java Servlet

3,694

Published on

Knowledge sharing on Java Servlet material.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,694
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
210
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "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>( http://en.wikipedia.org/wiki/Java_Servlet ) </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>( http://java.sun.com ) </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>( http://www.novocode.com/doc/servlet-essentials/chapter1.html ) </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 OurServlet.java ) </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>http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  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>http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  16. 16. Deploying Servlets on Tomcat (cont) <ul><li>Run Tomcat server and then execute the Servlet </li></ul>http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  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>http://www.novocode.com/doc/servlet-essentials/chapter1.html
  19. 19. Understanding Servlet API <ul><li>Packages in the Java Servlet API 2.1 </li></ul>http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  20. 20. Understanding Servlet API (cont) <ul><li>Packages in the Java Servlet API 2.1 </li></ul>http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  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>http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  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 : http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  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>http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  24. 24. URL Redirecting <ul><li>Using sendRedirect() method of HttpServletResponse class. </li></ul><ul><li>Using forward() method of RequestDispatcher class. </li></ul>http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  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>http://www.theserverside.com/discussions/thread.tss?thread_id=26425
  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>http://www.theserverside.com/discussions/thread.tss?thread_id=26425
  27. 27. Thank you Fahmi Jafar (fahmijafar@fahmijafar.net)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×