310 likes | 338 Views
JSPs and MVC. http://flic.kr/p/8KTNbe. What are you going to learn about today?. How to write JSPs Industry standard architecture for web apps. http://flic.kr/p/8JpkTg. Recall how servlets work. Head First Servlets and JSP (2 nd edition) , pp. 95–96. Container creates objects.
E N D
JSPs and MVC http://flic.kr/p/8KTNbe
What are you goingto learn about today? • How to write JSPs • Industry standard architecture for web apps http://flic.kr/p/8JpkTg
Recall how servlets work Head First Servlets and JSP (2nd edition), pp. 95–96
Container creates objects Head First Servlets and JSP (2nd edition), pp. 95–96
Container calls service on your servlet Head First Servlets and JSP (2nd edition), pp. 95–96
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: All servlets Head First Servlets and JSP (2nd edition), p. 51
Any problems with the all-servlet design? Problem: Tons of ugly printlns http://flic.kr/p/9ksxQa
Recall how ugly/inconvenient generatingHTML is using Servlets out.println(“<html> “ +“<body>” + “<h1>Skyler\’s Login Page</h1>” + “<br>” + today + “</body>” + “</html>”); Fortunately, there’s JSP
JSP: HTML meets Java <html><body> <h1>Skyler’s Login Page</h1> <br> <%= new java.util.Date() %> </body> </html> Java code! (“scriptlet”)
Here’s another JSP example public class Counter { private static int count; public static synchronizedintgetCount() { count++; return count; } } <html> <body> The page count is: <% out.println(Counter.getCount()); %> </body> </html> Java code! (“scriptlet”)
Example of JSP-to-servlet translation Servlet JSP
Here’s another JSP tag: <%= … %> When the Container sees this:<%= Counter.getCount() %> It turns it into this:out.print(Counter.getCount());
What’s wrong with this code? package foo; public class Counter { private static int count; public static synchronizedintgetCount() { count++; return count; } } There’s a package now! But it’s not specified here andthere’s no import! <html> <body> The page count is: <% out.println(Counter.getCount()); %> </body> </html> http://flic.kr/p/9ksxQa
You can fix it with a JSP “directive” tag <%@ page import=”foo.*” %> <html> <body> The page count is: <% out.println(Counter.getCount()); %> </body> </html> There are three flavors of directive tags: • <%@ page … %> page-specific properties • <%@ include … %> like #include in C • <%@ taglib … %> import custom tags (eg, JSTL)
JSP also gives you several implicit objects • out : JspWriter • request : HttpServletRequest • response : HttpServletResponse • session : HttpSession • application : ServletContext • config : ServletConfig • exception : Throwable • pageContext : PageContext • page : Object
Stuff for you to investigate on your own • EL: Expression Language • Makes it simpler to invoke Java code (not defined in JSP) • Looks like this: ${pageContent.currentTip} • JSTL: JSP Standard Tag Library • Lots of off-the-shelf functionality at your disposal • For example, see <c:URL> for doing URL encoding
So we should ditch servletsand just use JSPs, right? Not so fast…
What if we went with all JSPs for GeekDates? Problem:Tons of ugly scriptlets http://flic.kr/p/9ksxQa
Another possible design:Servlets + JSPs All HTML goes in JSPs—no printlns! Business logic goes in servlets—minimal scriptlets! Head First Servlets and JSP (2nd edition), p. 52
Here’s how the servlet + JSP design would work Head First Servlets and JSP (2nd edition), p. 52
Any problems with the servlet + JSP design? • Problems: • Redundant code in servlets • Poor reusability http://flic.kr/p/9ksxQa
A better architecture: MVC Translates UI actionsinto operations ondomain objects Domain Objects User Interface Head First Servlets and JSP (2nd edition), p. 54
How ’bout a demonstration! See: https://utopia.cs.memphis.edu/course/comp4081-2013fall/examples/banksim-mvc/ http://flic.kr/p/5dfuqL
Summary • JSPs translate to servlets • JSP tags: <% … %>, etc. • JSP implicit objects • MVC architecture for Java EE web apps • Model -> POJOs • View -> JSPs • Controller -> Servlets http://flic.kr/p/YSY3X