15.3 Summarizing MVC Code

This section summarizes the code that would be used for request-based , session-based, and application-based MVC approaches.

Request-Based Data Sharing

With request-based sharing, the servlet stores the beans in the HttpServletRequest , where they are accessible only to the destination JSP page.

Servlet
 
 ValueObject value = new ValueObject(...);  request.setAttribute("key", value);  RequestDispatcher dispatcher =   request.getRequestDispatcher("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response); 
JSP Page
 
 <jsp:useBean id="key" type="somePackage.ValueObject"  scope="request"  /> <jsp:getProperty name="key" property="someProperty" /> 

Session-Based Data Sharing

With session-based sharing, the servlet stores the beans in the HttpSession , where they are accessible to the same client in the destination JSP page or in other pages.

Servlet
 
 ValueObject value = new ValueObject(...);  HttpSession session = request.getSession();   session.setAttribute("key", value);  RequestDispatcher dispatcher =   request.getRequestDispatcher("/WEB-INF/SomePage.jsp"); dispatcher.forward(request, response); 
JSP Page
 
 <jsp:useBean id="key" type="somePackage.ValueObject"  scope="session"  /> <jsp:getProperty name="key" property="someProperty" /> 

Application-Based Data Sharing

With application-based sharing, the servlet stores the beans in the ServletContext , where they are accessible to any servlet or JSP page in the Web application. To guarantee that the JSP page extracts the same data that the servlet inserted, you should synchronize your code as below.

Servlet
 
  synchronized(this) {  ValueObject value = new ValueObject(...);  getServletContext().setAttribute("key", value);  RequestDispatcher dispatcher =     request.getRequestDispatcher("/WEB-INF/SomePage.jsp");   dispatcher.forward(request, response);  }  
JSP Page
 
 <jsp:useBean id="key" type="somePackage.ValueObject"  scope="application"  /> <jsp:getProperty name="key" property="someProperty" /> 


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net