Scope of jsp:useBean

后端 未结 3 1684
小鲜肉
小鲜肉 2021-02-06 19:56

home.jsp



<%
      username=\"Jitendra\";
%>



        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 20:23

    On Tomcat 6, home.jsp is translated to the Servlet code:

    java.lang.String username = null;
    synchronized (application) {
      username = (java.lang.String) _jspx_page_context.getAttribute("username", 
                                                      PageContext.APPLICATION_SCOPE);
      if (username == null){
        username = new java.lang.String();
        _jspx_page_context.setAttribute("username", username,
                                                      PageContext.APPLICATION_SCOPE);
      }
    }
    
    username="Jitendra"; 
    
    org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, 
                                                     "include.jsp", out, false);
    

    Although the behaviour is the same, the exact code generated depends on the application server implementation.

    The scope of the local username variable does not extend into the Servlet that will be generated from include.jsp. You are not setting the value "Jitendra" into the application scope, only setting the value of the local variable.

    As others have pointed out, an immutable String does not make a very good bean.

提交回复
热议问题