I declared a request scoped bean in a jsp page and tried to access it another page.
I was able to access the bean on another page using scriptlet
as well as EL
, however it got an exception if used jsp;getProperty
.
Below is the code of both the pages.
PageScope.jsp(First Page) :
<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />
<jsp:setProperty name="taskBean" property="multiplier" value="55" />
<jsp:setProperty name="taskBean" property="multiplicand" value="<%= dd %>" />
<%
System.out.println("Forwarding request to other page.");
RequestDispatcher dispatcher = request.getRequestDispatcher("RequestScope.jsp");
dispatcher.forward(request, response);
%>
RequestScope.jsp(Another Page):
<%
out.println("Accessing request scoped bean");
TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");
out.println(taskBean.getMultiplier());
%>
${taskBean.multiplier}
<jsp:getProperty name="taskBean" property="multiplier" />
//THE ABOVE LINE THROWS EXCEPTION.
Is it possible to access bean using jsp:getProperty. Please let me know i am wrong. Thanks.
Edit : Below is the stacktrace.
org.apache.jasper.JasperException: file:/RequestScope.jsp(28,4) jsp:getProperty for bean with name 'taskBean'. Name was not previously introduced as per JSP.5.3
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1104)
org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1126)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Generator.generate(Generator.java:3489)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jsp.PageScope_jsp._jspService(PageScope_jsp.java:109)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
I believe the tag
Will be needed in the RequestScope.jsp file, since it's already be created and exists in the scope. Likely the takeBean is better created in the servlet, and the servlet creates the taskBean and set the values, then performs the dispatch.forward instead of from another jsp page.
You need to add
before you access the properties using
jsp:getProperty
Also,
The "name" attribute of
jsp:getProperty
should match the "id" attribute of the declared bean.Then access
With Scriptlet When you use
Check You have already added
<%@page import="com.mybean.TaskBean"%>
statemement in your JSP.So, When JSP page gets compiled like
RequestScope_jsp.java
.First, It has import statement for your bean Second,
_jspService
method hasrequest
andresponse
parameters as argument.Thus, you can forwarded JSP pages access them using
scriptlets
. The same things happens withEL
.: When you access bean property using
jsp:getProperty
, you require to add<jsp:useBean>
which is used to locate and instantiate a bean class1 . You need to declare the bean before using its name in jsp:getProperty.
You can read about PageContext here: http://docs.oracle.com/javaee/5/api/javax/servlet/jsp/PageContext.html Another reference to pageContext implicit object is in
JSP.2.4 Implicit Objects
section.Since you forward to another JSP there will be new PageContext.
It will be safe to use the same instruction, the specification says that the bean with the same name (in the target scope) will not be re-created if it exists:
So add the same tag as in PageScope.jsp to the second JSP (RequestScope.jsp):
2 . Regarding why it is accessible with EL, the JSP spec describes this in
JSP.2.9 Resolution of Variables and their Properties
. You better take a look yourself, here is a sample from that section3 . Regarding why it's accessible with scriptlet is obvious
jsp 1:
jsp 2:
You just take it from the place (request attribute) where to the bean was placed by the first
jsp:useBean
invocation.