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" onclick="submitForm()" id="submit1" /> </form> </body></html>
<?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" name="code">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;@Overridepublic 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;}}
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 stubHttpServletRequest 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}}
<!-- 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>
<%@ 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>
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 stubSystem.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS);}public void sessionDestroyed(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");}}