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">密 码</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> </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>