310 likes | 323 Views
Dive into Servlet architecture, JSP, and web app design. Learn about HTTP methods, servlet basics, and MVC architecture. Understand servlet requests, response handling, and JSP integration for cleaner web development.
E N D
Deeper into Servlets http://flic.kr/p/6UdWYS
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
Do you remember? What two HTTP request “methods” did we introduce last time?How are they different? http://flic.kr/p/9ksxQa
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
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
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
Do you remember? To make the most basic of Servlets,what two things must you create? http://flic.kr/p/9ksxQa
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
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>”); } }
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
Let’s review the basic Servlet architecture,adding in a few new details
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
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
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
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
Let’s consider how you might design amore elaborate Servlet-based web app Example: GeekDates Head First Servlets and JSP (2nd edition), p. 50
One possible design Head First Servlets and JSP (2nd edition), p. 51
A second design that leverages JSPto do away with the println ugliness Head First Servlets and JSP (2nd edition), p. 52
Here’s how the JSP would work Head First Servlets and JSP (2nd edition), p. 52
A better architecture: MVC Head First Servlets and JSP (2nd edition), p. 54
MVC Design What’s wrong with this design?How could it be improved? Head First Servlets and JSP (2nd edition), p. 54
Let’s walk through the Beer version 3 demo from Ch. 3 http://flic.kr/p/5dfuqL
Quiz! Head First Servlets and JSP (2nd edition), p. 58
A quick word about Java EE Tomcat Head First Servlets and JSP (2nd edition), p. 65
Summary • Servlet web app architecture • JSP • MVC architecture http://flic.kr/p/YSY3X