Java Servlet


Published on

Servlet, Session management, Listners, Filter ppt based of Head first servlet jsp

1 Comment
  • thanx for posting this ,if you have teradata please post it
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Servlet

  1. 1. ServletRajeev Gupta MTech CS
  2. 2. What does your web server do?
  3. 3. Clients and ser vers know HTML and HTTP• HTML tells the browser how to display the content to the user.• HTTP is the protocol clients and servers use on the web to communicate.• The server uses HTTP to send HTML to the client.
  4. 4. Anatomy of an HTTP GET request
  5. 5. Anatomy of an HTTP POST request
  6. 6. Anatomy of an HTTP response, and what the heck is a “ MIME type”?
  7. 7. Anatomy of URL
  8. 8. Why Server is not enough• Dynamic Content• Saving data on the server
  9. 9. What a container?• Communication support• Lifecycle management• Multithreading support• Declarative security• JSP Support
  10. 10. How a container handle a dynamic request?
  11. 11. Servlet Life Cycle
  12. 12. Servlet inherits the lifecycle methods
  13. 13. Each request runs in a separate thread!
  14. 14. Besides parameters, what else can I get from a Request object?
  15. 15. dispatching vs. redirecting
  16. 16. ServletContext vs. ServletConfig
  17. 17. Setting ServletConfig
  18. 18. and getting it in Servlet…
  19. 19. Setting and getting ServletContext
  20. 20. Where it fit in JEE context…
  21. 21. Exercise• Hello World Servlet• Login Application –I• Login Application-II• Use ServletContext and ServletConfig
  22. 22. MVCModelViewControllerWhere it fit all??
  23. 23. Beer Advice Application
  24. 24. MVC Arch
  25. 25. Application flow MVC• 1- The client makes a request for the form.html page.• 2 - The Container retrieves the form.html page.• 3 - The Container returns the page to the browser, where the user answers the questions on the form and...• 4- Browser request data to the container• 5- container find the correct Servlet based on the URL, an passes the request to the servlet• 6 – The Servlet call BeerServlet for the help
  26. 26. Application flow MVC• 7- BeerExpert class return an answer, which Servlet add to the request object• 8- The Servlet forward request to the jsp• 9-Jsp get the answer from the request object• 10- JSP generate a page for the container• 11- Container return page to the happy user
  27. 27. The HTML for the initial form page <html><body> <h1 align=”center”>Beer Selection Page</h1> <form method=”POST” action=””> Select beer characteristics<p> Color: <select name=”color” size=”1”> <option value=”light”> light </option> <option value=”amber”> amber </option> <option value=”brown”> brown </option> <option value=”dark”> dark </option> </select> <br><br> <center> <input type=”SUBMIT”> </center> </form> </body></html>
  28. 28. Web.xml
  29. 29. The controller Servlet
  30. 30. Model…..
  31. 31. And finally the view…
  32. 32. Need for listener….• Init parameter (key, value) are really are strings• What if we need objects ….
  33. 33. She wants a ServletContextListener
  34. 34. We will convert dog string to a real dog!!!!
  35. 35. Making and using a context listener• We need three classes and one DD• The ServletContextListener –• The attribute class –• The Servlet –
  36. 36. Writing the listener class
  37. 37. Attributes: What is it???
  38. 38. multithreading issues????What abut multithreading issues???? Note easy to handle But really not rocket science!!!!
  39. 39. How do we make context attributes thread-safe?• Idea 1: – Synchronizing the service method is a spectacularly BAD idea• Idea 2: – SingleThreadModel – Idea 3: » Synchronized Block » What to Use????
  40. 40. Synchronizing the service method is a spectacularly BAD idea
  41. 41. Solution: You don’t need a lock on the servlet... you need the lock on the context!
  42. 42. Are Session attributes thread-safe? Not really!!!
  43. 43. request Attribute : thread safe but…..
  44. 44. Session Management
  45. 45. Simple Cookies Example: for session management
  46. 46. Filters are modular, and configurable in the DD
  47. 47. Three ways filters are like servlets• The Container knows their API• Just like servlets, filters have a lifecycle. » Like servlets, they have init() and destroy() methods. Similar to a servlet’s doGet()/doPost() method, filters have a doFilter() method.• They’re declared in the DD » A web app can have lots of filters, and a given request can cause more than one filter to execute. The DD is the place where you declare which filters will run in response to which requests, and in which order.
  48. 48. Building the request tracking filter
  49. 49. Thanks:-Reference:-Head First Servlet and Jsp