Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 321 Week 8. Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction.

Similar presentations


Presentation on theme: "COMP 321 Week 8. Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction."— Presentation transcript:

1 COMP 321 Week 8

2 Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction

3 MVC Example

4

5 Displaying the Form 1. Client makes a request for form.html 2. Container retrieves the form.html page 3. Container returns page to the browser, User answers questions and submits

6 Displaying Results 4. Browser sends request data to container 5. Container finds servlet based on URL, and forwards request 6. Servlet calls BeerExpert for help 7. Expert class returns an answer, which servlet adds to request object 8. Servlet forwards request to JSP 9. JSP gets answer from request object 10. JSP generates page 11. Container returns page to user

7 Development File Structure

8 Deployment File Structure

9 Form HTML Beer Selection Page Select beer characteristics Color: light amber brown dark

10 Deployment Descriptor <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/web-app_2_4.xsd" version="2.4"> Ch3 Beer com.example.web.BeerSelect Ch3 Beer /SelectBeer.do

11 Servlet Code (Version #1) // Version 1 public class BeerSelect extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice "); // For now, just show selected color so we can test servlet deployment String c = request.getParameter("color"); out.println(" Got beer color " + c); }

12 Model Class public class BeerExpert { public List getBrands(String color) { List brands = new ArrayList(); if(color.equals("amber")) { brands.add("Jack Amber"); brands.add("Red Moose"); } else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return brands; }

13 Servlet Code (Version #2) public class BeerSelect extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice "); Iterator it = result.iterator(); while(it.hasNext()) { out.print(" try: " + it.next()); }

14 Current Flow 1. The browser sends the request data to the Container 2. The Container finds the correct Servlet based on the URL, and passes the request to the Servlet. 3. The Servlet calls the BeerExpert for help. 4. The expert class returns an answer. 5. The Container returns the page to the happy user.

15 Dispatching to the JSP // Version 3 public class BeerSelect extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); request.setAttribute("styles", result); RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request, response); }

16 …and in the JSP… Beer Recommendations JSP <% List styles = (List)request.getAttribute("styles"); Iterator it = styles.iterator(); while(it.hasNext()) { out.print(" try: " + it.next()); } %>

17 Servlet Lifecycle Container loads class Container creates instance Container calls init() Container calls service() each time a request comes in Container calls destroy()

18 Servlet API public interface Servlet { void init(ServletConfig config); void service(ServletRequest request, ServletResponse response); void destroy();... } public abstract class GenericServlet implements Servlet { // Implements init(), service(), destroy() } public abstract class HttpServlet extends GenericServlet { void service(ServletRequest req, ServletResponse resp) { … } protected void service(HttpServletRequest req, HttpServletResponse resp) { … } protected void doGet(HttpServletRequest req, HttpServletResponse resp) { … } protected void doPost(HttpServletRequest req, HttpServletResponse resp) { … } }

19 How to Write a Servlet – Programming Tips Don’t do anything in the constructor – the object isn’t really a servlet yet! Initialization belongs in init() Servlet is given a ServletConfig and a ServletContext - pay attention to the difference Each request is made on a separate thread, but they all use the same object

20 Lab 8-1 Servlets Introduction

21 Progress Check Due this week:  Lab 7-1 Preparing to Build Web Applications Due next week:  Begin working on Lab 8-1 Servlets


Download ppt "COMP 321 Week 8. Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction."

Similar presentations


Ads by Google