[英]After logout, session is not ending
我已经创建了一个Java登录应用程序,其中的所有程序都工作正常,但是当我单击注销按钮时,它成功注销并重定向到index.jsp,但是在index.jsp页面中,如果我打印会话值,那么它正在打印相同的内容,我不知道为什么 ?? 但是,注销后,我已终止该会话。 下面是代码,请提出可能的原因:
在index.jsp页面上,以下是检查是否存在会话的代码。 注销后,它正在打印“ isession不为空” ...
<%
if (session == null)
{
System.out.println("session is null");
session.removeAttribute("username");
session.removeAttribute("uniqueID");
session.invalidate();
}
else if(session != null)
{
System.out.println("isession is not null");
System.out.println(session);
}
%>
loginServlet.java
String name = "";
JSONObject obj = result_array.getJSONObject(0);
String res = obj.get("result").toString();
HttpSession session = null;
if (res.equals("true")) {
try {
name = obj.get("name").toString();
session = request.getSession(true);
session.setAttribute("username", name);
session.setAttribute("uniqueID", uname);
//setting session to expiry in 15 mins
session.setMaxInactiveInterval(15*60);
Cookie userName = new Cookie("user", uname);
userName.setMaxAge(15*60);
response.addCookie(userName);
if("0".equals(obj.get("role").toString()))
{
session.setAttribute("role", "user");
response.sendRedirect("home.jsp");
}
else if("1".equals(obj.get("role").toString()))
{
session.setAttribute("role", "admin");
response.sendRedirect("AdminHome.jsp");
}
}
catch (JSONException ex)
{
System.out.println(getClass().getName()+" = " +ex.toString());
this.context.log(ex.toString());
}
logoutservlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// Cookie[] cookies = request.getCookies();
// if (cookies != null) {
// for (Cookie cookie : cookies) {
// if (cookie.getName().equals("JSESSIONID")) {
// System.out.println("JSESSIONID=" + cookie.getValue());
// break;
// }
// }
// }
Cookie loginCookie = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("user")) {
loginCookie = cookie;
break;
}
}
}
if (loginCookie != null) {
loginCookie.setMaxAge(0);
response.addCookie(loginCookie);
}
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute("username");
session.removeAttribute("uniqueID");
session.removeAttribute("role");
session.invalidate();
}
out.print("You have Succefully logged out ");
response.sendRedirect("index.jsp");
out.flush();
out.close();
}
}
缺省情况下,当然会为JSP自动创建会话,除非它已经存在。 因此,注销后再次检查隐式session
对象时,它是一个新对象。
您可以通过打印来验证
<%= session.isNew() %>
要为特定的JSP关闭此功能,您需要设置page
指令的session
属性。
<%@ page session="false" %>
尽管这似乎没有必要,因为登录/注销状态始终可以由会话属性的存在而不是会话本身的无效来确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.