首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未从jsp中的pageContext对象获取属性

未从jsp中的pageContext对象获取属性
EN

Stack Overflow用户
提问于 2017-07-19 15:00:40
回答 1查看 3.2K关注 0票数 1

我没有从jsp代码中的pageContext对象获取属性:

代码语言:javascript
复制
<%

    String username =request.getParameter("name");
    if(username!=null){
        //session.setAttribute("sessionUsername", username);
        //application.setAttribute("appUsername", username);
        pageContext.setAttribute( "requestParams",username );
        pageContext.setAttribute("requestScope", username, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute("sessionScope", username, PageContext.SESSION_SCOPE);
        pageContext.setAttribute("applicationScope", username, PageContext.APPLICATION_SCOPE);
    }
    /* String sessionName =(String)session.getAttribute("sessionUsername");

    String applicaString = (String)application.getAttribute("appUsername");
     */
    %>
<%-- <br>
the name is : <%=username%>
<br>
the session name is : <%=sessionName%>
<br>
the application name is : <%=applicaString%>
 --%>
<br>
the request Scope  name is : <%=pageContext.getAttribute("requestScope")%>

<br>
the sessionScope name is : <%=pageContext.getAttribute("sessionScope")%>

<br>
the appication Scope name is : <%=pageContext.getAttribute("applicationScope")%>

<br>
the page Scope name is : <%=pageContext.getAttribute("requestParams")%>

我在应用程序作用域和请求作用域中得到了null,但是当我使用具有默认页面作用域的setAttribute方法时,它工作得很好。

EN

回答 1

Stack Overflow用户

发布于 2017-07-19 22:44:50

尝试将您的代码更改为:

代码语言:javascript
复制
<%

    String username =request.getParameter("name");
    if(username!=null){
        //session.setAttribute("sessionUsername", username);
        //application.setAttribute("appUsername", username);
        pageContext.setAttribute( "requestParams",username );
        pageContext.setAttribute("requestScope", username, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute("sessionScope", username, PageContext.SESSION_SCOPE);
        pageContext.setAttribute("applicationScope", username, PageContext.APPLICATION_SCOPE);
    }
    /* String sessionName =(String)session.getAttribute("sessionUsername");

    String applicaString = (String)application.getAttribute("appUsername");
     */
    %>
<%-- <br>
the name is : <%=username%>
<br>
the session name is : <%=sessionName%>
<br>
the application name is : <%=applicaString%>
 --%>
<br>
the request Scope  name is : <%=request.getAttribute("requestScope")%>

<br>
the sessionScope name is : <%=session.getAttribute("sessionScope")%>

<br>
the appication Scope name is : <%=application.getAttribute("applicationScope")%>

<br>
the page Scope name is : <%=pageContext.getAttribute("requestParams")%>

我将以下三行代码从:

代码语言:javascript
复制
the request Scope  name is : <%=pageContext.getAttribute("requestScope")%>
the sessionScope name is : <%=pageContext.getAttribute("sessionScope")%>
the appication Scope name is : <%=pageContext.getAttribute("applicationScope")%>

至:

代码语言:javascript
复制
the request Scope  name is : <%=request.getAttribute("requestScope")%>
the sessionScope name is : <%=session.getAttribute("sessionScope")%>
the appication Scope name is : <%=application.getAttribute("applicationScope")%>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45183112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档