Accessing request scoped bean

2019-08-06 04:45发布

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)

3条回答
做个烂人
2楼-- · 2019-08-06 05:22

I believe the tag

<jsp:useBean id="taskBean" type="com.mybean.TaskBean" scope="request" />

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.

查看更多
SAY GOODBYE
3楼-- · 2019-08-06 05:39
Name was not previously introduced" indicates that you haven't told your JSP about this bean as of yet.

You need to add

<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

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

<jsp:getProperty name="taskBean" property="multiplier" />

With Scriptlet When you use

TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");

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 has request and response parameters as argument.

_jspService(HttpServletRequest request, HttpServletResponse response)

Thus, you can forwarded JSP pages access them using scriptlets. The same things happens with EL.

Remember thumb rule

: When you access bean property using jsp:getProperty, you require to add <jsp:useBean> which is used to locate and instantiate a bean class

查看更多
地球回转人心会变
4楼-- · 2019-08-06 05:48

1 . You need to declare the bean before using its name in jsp:getProperty.

JSP.5.3

The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.

The object named by the name must have been “introduced” to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.

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:

JSP.5.1

The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.

So add the same tag as in PageScope.jsp to the second JSP (RequestScope.jsp):

 <jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

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 section

${product}

This expression will look for the attribute named product, searching the page, request, session, and application scopes, and will return its value. If the attribute is not found, null is returned.

3 . Regarding why it's accessible with scriptlet is obvious

jsp 1:

<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

jsp 2:

TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");

You just take it from the place (request attribute) where to the bean was placed by the first jsp:useBean invocation.

查看更多
登录 后发表回答