我没有从jsp代码中的pageContext对象获取属性:
<%
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方法时,它工作得很好。
发布于 2017-07-19 22:44:50
尝试将您的代码更改为:
<%
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")%>
我将以下三行代码从:
the request Scope name is : <%=pageContext.getAttribute("requestScope")%>
the sessionScope name is : <%=pageContext.getAttribute("sessionScope")%>
the appication Scope name is : <%=pageContext.getAttribute("applicationScope")%>
至:
the request Scope name is : <%=request.getAttribute("requestScope")%>
the sessionScope name is : <%=session.getAttribute("sessionScope")%>
the appication Scope name is : <%=application.getAttribute("applicationScope")%>
https://stackoverflow.com/questions/45183112
复制相似问题