The most important point about MVC is the idea of separating the business logic and data access layers from the presentation layer. The syntax is quite simple, and in fact you should be familiar with much of it already. Here is a quick summary of the required steps; the following subsections supply details.
Defining Beans to Represent the DataBeans are Java objects that follow a few simple conventions. In this case, since a servlet or other Java routine (never a JSP page) will be creating the beans, the requirement for an empty (zero-argument) constructor is waived. So, your objects merely need to follow the normal recommended practices of keeping the instance variables private and using accessor methods that follow the get/set naming convention. Since the JSP page will only access the beans, not create or modify them, a common practice is to define value objects : objects that represent results but have little or no additional functionality. Writing Servlets to Handle RequestsOnce the bean classes are defined, the next task is to write a servlet to read the request information. Since, with MVC, a servlet responds to the initial request, the normal approaches of Chapters 4 and 5 are used to read request parameters and request headers, respectively. The shorthand populateBean method of Chapter 4 can be used, but you should note that this technique populates a form bean (a Java object representing the form parameters), not a result bean (a Java object representing the results of the request). Although the servlets use the normal techniques to read the request information and generate the data, they do not use the normal techniques to output the results. In fact, with the MVC approach the servlets do not create any output; the output is completely handled by the JSP pages. So, the servlets do not call response.setContentType , response.getWriter , or out.println . Populating the BeansAfter you read the form parameters, you use them to determine the results of the request. These results are determined in a completely application-specific manner. You might call some business logic code, invoke an Enterprise JavaBeans component, or query a database. No matter how you come up with the data, you need to use that data to fill in the value object beans that you defined in the first step. Storing the ResultsYou have read the form information. You have created data specific to the request. You have placed that data in beans. Now you need to store those beans in a location that the JSP pages will be able to access. A servlet can store data for JSP pages in three main places: in the HttpServletRequest , in the HttpSession , and in the ServletContext . These storage locations correspond to the three nondefault values of the scope attribute of jsp:useBean : that is, request , session , and application .
Forwarding Requests to JSP PagesYou forward requests with the forward method of RequestDispatcher . You obtain a RequestDispatcher by calling the getRequestDispatcher method of ServletRequest , supplying a relative address. You are permitted to specify addresses in the WEB-INF directory; clients are not allowed to directly access files in WEB-INF , but the server is allowed to transfer control there. Using locations in WEB-INF prevents clients from inadvertently accessing JSP pages directly, without first going through the servlets that create the JSP data. Core Approach
Once you have a RequestDispatcher , you use forward to transfer control to the associated address. You supply the HttpServletRequest and HttpServletResponse as arguments. Note that the forward method of RequestDispatcher is quite different from the sendRedirect method of HttpServletRequest (Section 7.1). With forward , there is no extra response/request pair as with sendRedirect . Thus, the URL displayed to the client does not change when you use forward . Core Note
For example, Listing 15.1 shows a portion of a servlet that forwards the request to one of three different JSP pages, depending on the value of the operation request parameter. Listing 15.1 Request Forwarding Examplepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String operation = request.getParameter("operation"); if (operation == null) { operation = "unknown"; } String address; if (operation.equals("order")) { address = "/WEB-INF/Order.jsp"; } else if (operation.equals("cancel")) { address = "/WEB-INF/Cancel.jsp"; } else { address = "/WEB-INF/UnknownOperation.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } Forwarding to Static ResourcesIn most cases, you forward requests to JSP pages or other servlets. In some cases, however, you might want to send requests to static HTML pages. In an e-commerce site, for example, requests that indicate that the user does not have a valid account name might be forwarded to an account application page that uses HTML forms to gather the requisite information. With GET requests, forwarding requests to a static HTML page is perfectly legal and requires no special syntax; just supply the address of the HTML page as the argument to getRequestDispatcher . However, since forwarded requests use the same request method as the original request, POST requests cannot be forwarded to normal HTML pages. The solution to this problem is to simply rename the HTML page to have a .jsp extension. Renaming somefile.html to somefile.jsp does not change its output for GET requests, but somefile.html cannot handle POST requests, whereas somefile.jsp gives an identical response for both GET and POST . Redirecting Instead of ForwardingThe standard MVC approach is to use the forward method of RequestDispatcher to transfer control from the servlet to the JSP page. However, when you are using session-based data sharing, it is sometimes preferable to use response.sendRedirect . Here is a summary of the behavior of forward .
Here is a summary of sendRedirect .
Extracting Data from BeansOnce the request arrives at the JSP page, the JSP page uses jsp:useBean and jsp:getProperty to extract the data. For the most part, this approach is exactly as described in Chapter 14. There are two differences however:
The scope you specify should match the storage location used by the servlet. For example, the following three forms would be used for request-, session-, and application-based sharing, respectively. <jsp:useBean id="key" type="somePackage.SomeBeanClass" scope="request" /> <jsp:useBean id="key" type="somePackage.SomeBeanClass" scope="session" /> <jsp:useBean id="key" type="somePackage.SomeBeanClass" scope="application" /> |