访问servlet在jsp中声明的HttpSession

use*_*808 1 jsp servlets httpsession

如果登录 servlet 中的用户名、密码和位置正确,我将创建一个 HttpSession 并进入 jsp 页面。下面是servlet中的代码:

    HttpSession sessionsa = request.getSession(true);
    sessionsa.setAttribute("user",userName); //userName is a String variable
    sessionsa.setAttribute("location",location); //location in value place is a String variable
Run Code Online (Sandbox Code Playgroud)

现在在jsp页面上我无法访问属性。jsp上的代码:

    sessionsa = request.getSession();
    String user = (String) sessionsa.getAttribute("user");
    String location = (String) sessionsa.getAttribute("location");
Run Code Online (Sandbox Code Playgroud)

它指出在类 SimplifiedJSPServlet 中找不到符号变量 sessiona。请帮忙。从 2 天起一直在谷歌上搜索它。

Gun*_*iah 5

这样做,首先在您的jsp中使会话创建为假。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
Run Code Online (Sandbox Code Playgroud)

然后为了得到会话,

<%
HttpSession sessionsa = request.getSession(false);
String user = (String) sessionsa.getAttribute("user");
String location = (String) sessionsa.getAttribute("location");
%>
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您将从会话中获取用户和位置。希望这对您有所帮助。