1 / 31

Deeper into Servlets

Deeper into Servlets. http://flic.kr/p/6UdWYS. What are you going to learn about today?. More on architecture of Servlets What JSP is A good way to design your Servlet-based web apps. http://flic.kr/p/8JpkTg. Recall: Client-server architecture used by the Web. Do you remember?.

jjames
Download Presentation

Deeper into Servlets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deeper into Servlets http://flic.kr/p/6UdWYS

  2. What are you goingto learn about today? • More on architecture of Servlets • What JSP is • A good way to design your Servlet-based web apps http://flic.kr/p/8JpkTg

  3. Recall: Client-server architectureused by the Web

  4. Do you remember? What two HTTP request “methods” did we introduce last time?How are they different? http://flic.kr/p/9ksxQa

  5. Do you remember? • GET: No data payload, but parameters can be passed to the server as part of the URL • POST: Has a data payload What two HTTP request “methods” did we introduce last time?How are they different? http://flic.kr/p/9ksxQa

  6. Do you remember? In the case of a Servlet, what does the web server do with the request it receives? http://flic.kr/p/9ksxQa

  7. Do you remember? • Passes it to a Container In the case of a Servlet, what does the web server do with the request it receives? http://flic.kr/p/9ksxQa

  8. Do you remember? To make the most basic of Servlets,what two things must you create? http://flic.kr/p/9ksxQa

  9. Do you remember? • A Java class that inherits HttpServlet • A Deployment Descriptor (DD) • File: web.xml To make the most basic of Servlets,what two things must you create? http://flic.kr/p/9ksxQa

  10. Recall: Servlet example import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Ch1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); java.util.Date today = new java.util.Date(); out.println(“<html> “ + “<body>” + “<h1 align=center>HF\’s Chapter1 Servlet</h1>” + “<br>” + today + “</body>” + “</html>”); } }

  11. Recall: Deployment Descriptor <?xml version=”1.0” encoding=”ISO-8859-1” ?> <web-app xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd” version=”2.4”> <servlet> <servlet-name>Chapter1 Servlet</servlet-name> <servlet-class>Ch1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Chapter1 Servlet</servlet-name> <url-pattern>/Serv1</url-pattern> </servlet-mapping> </web-app> File-localname Javaclass URLpath

  12. Let’s review the basic Servlet architecture,adding in a few new details

  13. Head First Servlets and JSP (2nd edition), pp. 42–43

  14. Head First Servlets and JSP (2nd edition), pp. 42–43

  15. Head First Servlets and JSP (2nd edition), pp. 42–43

  16. Head First Servlets and JSP (2nd edition), pp. 42–43

  17. Time out! Consider this Servlet class: public class MyServlet extends HttpServlet { public void doGet(HttpServletRequestrequest, HttpServletResponseresponse) throws IOException { ... } } Where is service()? What about doGet()? Hint: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html Head First Servlets and JSP (2nd edition), pp. 42–43

  18. public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.println(“<html> “ + “<body>” + ... + “</body>” + “</html>”); } } Head First Servlets and JSP (2nd edition), pp. 42–43

  19. Head First Servlets and JSP (2nd edition), pp. 42–43

  20. You probably noticed how uglygenerating HTML is using Servlets out.println(“<html> “ +“<body>” + “<h1>Skyler\’s Login Page</h1>” + “<br>” + today + “</body>” + “</html>”); Fortunately, there is JSP

  21. JSP: HTML meets Java <html><body> <h1>Skyler’s Login Page</h1> <br> <%= new java.util.Date() %> </body> </html> We’ll discuss JSP more later

  22. Let’s consider how you might design amore elaborate Servlet-based web app Example: GeekDates Head First Servlets and JSP (2nd edition), p. 50

  23. One possible design Head First Servlets and JSP (2nd edition), p. 51

  24. A second design that leverages JSPto do away with the println ugliness Head First Servlets and JSP (2nd edition), p. 52

  25. Here’s how the JSP would work Head First Servlets and JSP (2nd edition), p. 52

  26. A better architecture: MVC Head First Servlets and JSP (2nd edition), p. 54

  27. MVC Design What’s wrong with this design?How could it be improved? Head First Servlets and JSP (2nd edition), p. 54

  28. Let’s walk through the Beer version 3 demo from Ch. 3 http://flic.kr/p/5dfuqL

  29. Quiz! Head First Servlets and JSP (2nd edition), p. 58

  30. A quick word about Java EE Tomcat Head First Servlets and JSP (2nd edition), p. 65

  31. Summary • Servlet web app architecture • JSP • MVC architecture http://flic.kr/p/YSY3X

More Related