选项卡或浏览器窗口关闭jsp时注销

时间:2017-04-08 12:49:09

标签: javascript html jsp

我是新的@jsp,并且想要在关闭选项卡或浏览器窗口时调用注销功能。我做了一些研究,发现我需要使用window.onbeforeunload。但问题是,我不知道将它放在我的jsp页面中。这是代码:

<%@ page language="java" contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"%>
<!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=windows-1256">

       <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
        <%@ taglib uri="http://java.sun.com/jstl/fmt_rt"  prefix="fmt" %>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
        <%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>   

  <link rel="stylesheet" href="css/login.css">
<title>Insert title here</title>

   <script type="text/javascript" language="javascript" runat="server">
     var divSelected = null;
     function switchURL()  
     {  
          var x = document.getElementById("femp").value;
              <c:choose>  
                     <c:when test="${puser.LOGIN==true}">
                       document.getElementById("temp").value = x;
                  //     alert("${puser.LOGIN}");
                     </c:when>  
                     <c:otherwise>  
                       document.getElementById("temp").value = 0;
                       document.getElementById("femp").value = 0;
                      //   alert("${puser.LOGIN}");
                     </c:otherwise>
                    </c:choose>        


     }  
</script>




   <script type="text/javascript" language="javascript" runat="server">
     var divSelected = null;
       function Logout()  
     {  
        document.getElementById("Form1").target  ="";
        document.getElementById("Form1").action  = "/dcHR/login";
   //   window.location = "login";


     }  





</script>

</head>
<body>
 <section class="container">
    <div class="login">

<form id="Form1" action="http://localhost:80/reports/rwservlet" method="Post"  target="_blank">

 <input type="hidden" id="cmdkey" name="cmdkey" value="repserv">
  <input type="hidden" id="temp" name="P_TO_EMP" value="0">
    <input type="hidden" id="femp" name="P_FROM_EMP" value="${puser.USR_USER}" > 
    <input type="hidden" id="pcomp" name="P_COMP" value="${puser.USR_CP}">
    <p> Years:<input type="text"  name="P_YEAR"> </p>
    <p> Month:<input type="text"  name="P_PERIOD"> </p>
           <input type="submit" value="Report"  name="pbtn" onclick="switchURL()" />
           <input type="submit" value="Logout" name="pbtn"  onclick="Logout()"/>

</form>


  </div>


  </section>
</body>
</html>

有人可以帮助我。

关心所有人。

1 个答案:

答案 0 :(得分:0)

您必须在窗口对象上添加侦听器,如下所示

window.addEventListener("beforeunload", function(){
    //Ajax call to invoke logout action
});

将此代码放入您的jsp页面。