Wednesday, 16 October 2013

What servlet code corresponds to the various scope

What servlet code corresponds to the various "scope" values for the <jsp:useBean> tag




Scope
JSP
Servlet Code
application
<jsp:useBean class="foo.Counter" scope="application" />
foo.Counter counter = (foo.Counter)getServletContext().getAttribute("counter");
if (counter == null) {
 counter = new foo.Counter();
 getServletContext().setAttribute("counter", counter);  
}

Session
<jsp:useBean id="counter" class="foo.Counter" scope="session" />
HttpSession session = request.getSession(true);
foo.Counter counter = (foo.Counter)session.getValue("counter");
if (counter == null) {
 counter = new foo.Counter();
 session.putValue("counter", counter);
}

request
<jsp:useBean id="counter" class="foo.Counter" scope="request" />
foo.Counter counter = (foo.Counter)request.getAttribute("counter");
if (counter == null) {
 counter = new foo.Counter();
 request.setAttribute("counter", counter);
}

page
<jsp:useBean id="counter" class="foo.Counter" scope="page" />
foo.Counter counter = new foo.Counter();

No comments:

Post a Comment

Very Impotent Links for Java and Portal development development

Jar Download for Spring  Maven Or Gradel Dependency for Spring