无法在jsp中使用javascript清除会话

时间:2015-10-20 09:38:50

标签: javascript java jsp session servlets

使用servlet

从login.jsp页面创建会话
String msg = "";
        HttpSession sess = request.getSession();
       // if(sess != null)

        //sess.invalidate();
        if (sess.getId() != null) {
            sess.setAttribute("uname", uname);
            sess.setAttribute("pwd", pwd);
        }

使用

在其他jsp页面中重试会话
<b> Welcome ${uname}</b>

注销超链接

<a href="login_ml.jsp" id="logout_link" onclick='lgt()'>Logout</a></td>   

javascript清除会话

function lgt(){
                var logout = document.getElementById("logout_link");
                logout.session.clear();
                alert("logout");
                }

1 个答案:

答案 0 :(得分:2)

我们无法直接从JS代码清除会话。您必须调用另一个JSP页面才会使该会话失效:

Javascript功能:

 function destroySession() {
        window.location = "killSession.jsp";
   }

<强> killSession.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Log out</title>
</head>
<body>
<%
session.invalidate();
%>
User has been Logged out sucessfully!!!!
</body>
</html>