how does jsp:useBean scope attribute work?

℡╲_俬逩灬. 提交于 2021-01-29 03:25:23

问题


I am trying to understand how exactly scope attribute in jsp:useBean JSP action tag works. In my understanding scope is used to indicate where the bean is located (request,session,application etc.), but after some testing I came across an interesting situation where it's not the case, please consider the following JSP code (I am using scriplets here just for the sake of simplicity):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"  import="package2JSP.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
</head>

<body>
    <%
        User user1 = new User("id1","name1");
        User user2 = new User("id2","name2");
        request.setAttribute("user", user1);
        session.setAttribute("user", user2);
    %>
    <%-- Here I expect to create user bean that represents user2 from session scope--%>
    <jsp:useBean id="user" class="package2JSP.User" scope="session"/>

    <%-- Here I expect user name to be name2 but it is name1 instead--%>
    <jsp:getProperty property="name" name="user"/>
</body>
</html>

So basically here I created 2 users objects and set them as "user" attributes in request and session scopes, when I tried to retrieve "user" from "session" scope using jsp:useBean it seems as if "user" from "request" scope was retrieved.

Can you please explain me why it happened? And what was the development reason to make jsp:useBean work this way instead of normally selecting the attribute from the specified scope, are there any advantages of it?

Now I know I could use JSTL/EL to retrieve the needed value i.e. <c:out value="${sessionScope.user.name}" /> but I just want to know how jsp:useBean works.


回答1:


In this situation are 2 tags involved:

  • jsp:useBean
  • jsp:getProperty

The jsp:useBean

According to Specification - CHAPTER JSP.5 - JSP.5.1 <jsp:useBean>:

A jsp:useBean action associates an instance of a Java programming language object defined within a given scope and available with a given id with a newly declared scripting variable of the same id . When a action is used in an scriptless page, or in an scriptless context (as in the body of an action so indicated), there are no Java scripting variables created but instead an EL variable is created.

The compiled java code is:

      package2JSP.User user = null;
      synchronized (session) {
        user = (package2JSP.User) _jspx_page_context.getAttribute("user", PageContext.SESSION_SCOPE);
        if (user == null){
          try {
            user = (package2JSP.User) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "package2JSP.User");
          } catch (ClassNotFoundException exc) {
            throw new InstantiationException(exc.getMessage());
          } catch (Exception exc) {
            throw new ServletException("Cannot create bean of class " + "package2JSP.User", exc);
          }
          _jspx_page_context.setAttribute("user", user, PageContext.SESSION_SCOPE);
        }
      }

If you want to access this user object you can use a scriptlet(expression) <%=user.getName()%>

The jsp:getProperty

According to Specification - CHAPTER JSP.5 - JSP.5.3 <jsp:getProperty>say:

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 JSP Compiler make from the jsp:getProperty tag a call to the findAttribute():

out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString(
  (((package2JSP.User)_jspx_page_context.findAttribute("user")).getName())));

findAttribute()
Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

Basically: the first match is returned.

Using useBean and getProperty is considered bad practice.
Using JSTL/EL is the better way to work with attributes.



来源:https://stackoverflow.com/questions/41679798/how-does-jspusebean-scope-attribute-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!