This section summarizes the code that would be used for request-based , session-based, and application-based MVC approaches. Request-Based Data SharingWith 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 SharingWith 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. ServletValueObject 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 SharingWith 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. Servletsynchronized(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" /> |