1 / 31

JSPs and MVC

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.

gmatranga
Download Presentation

JSPs and MVC

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. JSPs and MVC http://flic.kr/p/8KTNbe

  2. What are you goingto learn about today? • How to write JSPs • Industry standard architecture for web apps http://flic.kr/p/8JpkTg

  3. Recall how servlets work Head First Servlets and JSP (2nd edition), pp. 95–96

  4. Container creates objects Head First Servlets and JSP (2nd edition), pp. 95–96

  5. Container calls service on your servlet Head First Servlets and JSP (2nd edition), pp. 95–96

  6. Head First Servlets and JSP (2nd edition), pp. 95–96

  7. Head First Servlets and JSP (2nd edition), pp. 95–96

  8. Head First Servlets and JSP (2nd edition), pp. 95–96

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

  10. One possible design: All servlets Head First Servlets and JSP (2nd edition), p. 51

  11. Any problems with the all-servlet design? Problem: Tons of ugly printlns http://flic.kr/p/9ksxQa

  12. 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

  13. JSP: HTML meets Java <html><body> <h1>Skyler’s Login Page</h1> <br> <%= new java.util.Date() %> </body> </html> Java code! (“scriptlet”)

  14. 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”)

  15. Behind the scenes: JSPs become servlets

  16. Example of JSP-to-servlet translation Servlet JSP

  17. Add class members with <%! … %>

  18. Here’s another JSP tag: <%= … %> When the Container sees this:<%= Counter.getCount() %> It turns it into this:out.print(Counter.getCount());

  19. 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

  20. 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)

  21. And let’s not forget comments in JSP:<%-- JSP comment --%>

  22. 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

  23. 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

  24. So we should ditch servletsand just use JSPs, right? Not so fast…

  25. What if we went with all JSPs for GeekDates? Problem:Tons of ugly scriptlets http://flic.kr/p/9ksxQa

  26. 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

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

  28. Any problems with the servlet + JSP design? • Problems: • Redundant code in servlets • Poor reusability http://flic.kr/p/9ksxQa

  29. A better architecture: MVC Translates UI actionsinto operations ondomain objects Domain Objects User Interface Head First Servlets and JSP (2nd edition), p. 54

  30. How ’bout a demonstration! See: https://utopia.cs.memphis.edu/course/comp4081-2013fall/examples/banksim-mvc/ http://flic.kr/p/5dfuqL

  31. 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

More Related