我面临着一个非常奇怪的情况。我创建了一个简单的 Web 应用程序,我在其中使用索引文件登录,然后它转到 servlet 进行验证并转发到欢迎 jsp 页面......从那里我还有两个链接连接到两个不同的 jsp 页面。现在发生的事情是我在 LoginServlet 中设置了包含用户名和密码的对象。现在,当它转发到欢迎 jsp 时,我能够检索该对象,但之后在每个 jsp 中我都得到 session 变量 null。尽管同一个应用程序在我 friend 的 eclipse 上运行良好。
我正在使用 tomcat 8 和 eclipse mars 这是登录 servlet
`LoginModel model = (LoginModel) request.getAttribute("login");
HttpSession session=request.getSession();
session.setAttribute("loginmodel", model);
System.out.println("uid "+model.getUid());
LoginService lser = new LoginServiceImpl();
int x = lser.login(model);
if (x == 1) {
request.getRequestDispatcher("welcome").forward(request, response);
} else {
response.getWriter().println("<h2>Login Id or Password invalid</h2>");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
欢迎页面
<body>
<%LoginModel model=(LoginModel)session.getAttribute("loginmodel");
out.print(model);
%>
${sessionScope.loginmodel};
<h2><a href="logout">LOGOUT</a></h2>
<section class="container">
<div class="login">
<h2><a href="viewall">VIEW ALL</a></h2>
<h2><a href="edit">EDIT</a></h2>
</div>
</section>
</body>
这里是编辑jsp
<body>
<%LoginModel lm=(LoginModel)session.getAttribute("loginmodel");
out.print(lm);
%>
<h2><a href="logout">LOGOUT</a> <a href="welcome">HOME</a></h1><br><br>
<section class="container">
<div class="login">
<form action="editjspcontroller">
<h3><p><label>Enter No</label></p></h3><p><input type="text" name="ppno" id="no"></p>
<p class="submit"><input type="submit"></p>
</form>
</div>
</section>
</body>
最佳答案
我没有发现给定代码有任何问题。
你说确切的应用程序可以在你 friend 的机器上运行。
检查您是否禁用了 cookie
使用其他浏览器运行您的应用。
关于java - session 变量在第一次请求后变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38113992/