Netbeans判斷session到期跳轉到登陸頁

來源:互聯網
上載者:User

Netbeans下visual web pack開發時候 用到了判斷session 是否到期,到期就跳轉到登陸頁面

判斷session到期代碼如下:

 //得到請求執行個體
        HttpServletRequest request = (HttpServletRequest)this.getFacesContext().getExternalContext().getRequest();
        //得到session執行個體
        HttpSession session = request.getSession(false);
        //如果session到期
        if (session.getAttribute("sGrants") == null)
        {
            try {
                //跳到登陸頁面
                FacesContext.getCurrentInstance().getExternalContext().redirect("../default.jsp");
                //返回
                return ;
            } catch (Exception ex) {
                //列印異常資訊
                ex.printStackTrace();
            }
        }

登陸頁面代碼:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%
response.setHeader("progma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
if(session.getAttribute("sGrants") != null){
session.setAttribute("sGrants",null);
}
%>
<!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=UTF-8">
        <title>人力資源管理系統登入</title>
        <link href="style/main.css" rel="stylesheet" type="text/css" />
        <link href="style/login.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" language="javascript">
            function cc(){
                 CollectGarbage();
                 window.opener = null;
                 window.close();
            }
           if(top.location !== self.location) {
               top.location=self.location;
           }
           self.moveTo(0,0)
           self.resizeTo(screen.availWidth,screen.availHeight)
        </script>
   <script language="javascript" type="text/javascript" src="js/check.js"></script>
    </head>
    <body>
 <f:view>
  <h:form>       
<div class="logo"><img src="images/l_1.gif" /></div>
<div class="l_top"></div>
<div class="l_box">
<div class="login">   
        <table cellpadding="4">
            <tr>
                <td align="right">使用者名稱</td>
                <td>
                    <h:inputText id="loginName" required="true"   validator="#{validator.validateName}" style="width:175px;"/>
                    <h:message for="loginName" showDetail="" showSummary="true" style="color: #ff0000; "/>
                </td>
                   
            </tr>
            <tr>
                <td align="right">密&nbsp;&nbsp;&nbsp;&nbsp;碼</td>
                <td><h:inputSecret id="loginPass" required="true" validator="#{validator.validatePass}"  style="width:175px;"/>
                    <h:message for="loginPass" showDetail="" showSummary="true" style="color: #ff0000; "/>
            </td>
                
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><h:commandButton image="images/l_3.gif" value="登陸" action="#{validator.button2_action}" onclick="return s();"/>
                    <h:commandButton image="images/l_3_c.gif" value="取消" onclick="cc();"/>
                </td>
            </tr>      

        </table>
  </div>
</div>
<div class="l_foot"></div>   
  </h:form>
 </f:view>
    </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.