JavaFilter的使用

使用filter使session失效的用戶,重新跳轉到登錄頁面:
1.前臺簡單的登錄測試頁面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function submitForm(){
document.getElementById("form1").submit();
}
</script>

</head>

<body>
This is Login page. <br>
<form action="login" method="post" id="form1" name="form1">
UserName:<input type="text" id="userName" name="userName"/><input type="button" value="submit" οnclick="submitForm()" id="submit1" />
</form>
</body>
</html>



2.struts.xml的配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.wl.action.test.LoginAction">
<result name="success">
/success.jsp
</result>
</action>
</package>
</struts>


3.LoginAction如下:

package com.wl.action.test;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

String userName;

@Override
public String execute() throws Exception {

ActionContext context=ActionContext.getContext();
Map session=context.getSession();
System.out.println("userName="+userName);
session.put("userName", userName);
return SUCCESS;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}



4.過濾器FilterTest如下:

package com.wl.filter.test;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FilterTest implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub

HttpServletRequest httpReq=(HttpServletRequest)req;
HttpServletResponse httpRes=(HttpServletResponse)res;
HttpSession httpSession=httpReq.getSession();
if(httpSession.getAttribute("userName")==null){
httpRes.sendRedirect("../login.jsp");
}else{
chain.doFilter(req, res);
}
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}



5.配置Web.xml信息:
添加信息:

<!-- configure filter -->
<filter>
<filter-name>filterTest</filter-name>
<filter-class>com.wl.filter.test.FilterTest</filter-class>
</filter>
<filter-mapping>
<filter-name>filterTest</filter-name>
<url-pattern>/filterJsp/*</url-pattern>
</filter-mapping>
<!-- configure session timeout one minute -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>


6.成功跳轉頁面success.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'success.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
Success. <br>
<a href="filterJsp/ExtremeCompomentTest_1.jsp">Forward to Filter URL</a>
</body>
</html>



7.配置了一個Session的監聽器來監聽Session是否失效

package com.wl.listener.test;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class HttpSessionListenerTest implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub

System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS);
}

public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub

System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
}

}



8.WebRoot的目錄結構:
----WebRoot
------filterJsp
-----ExtremeCompomentTest_1.jsp
------login.jsp
------success.jsp

9.結果:
在IE中輸入:http://localhost:8080/FileUpload/login.jsp如下顯示
[img]http://dl.iteye.com/upload/attachment/482102/f2d0f771-78e6-361f-8de1-c5ed03c910d1.png[/img]
提交表單之後跳轉的頁面爲:

[img]http://dl.iteye.com/upload/attachment/482104/89acaf86-7591-3dd7-b7be-309083a5144e.png[/img]
等待1分鐘之後,在Eclipse的控制檯出現"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"信息時,即Session已經失效了,再點擊上面的"Forward to Filter URL"鏈接,這時候過濾器filter就會起作用,驗證Session失效後,跳轉到登錄界面。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章