首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JavaFilter的运用

2012-12-26 
JavaFilter的使用使用filter使session失效的用户,重新跳转到登录页面:1.前台简单的登录测试页面login.jsp

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>


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" 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;}}


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 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}}


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 stubSystem.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS);}public void sessionDestroyed(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");}}


8.WebRoot的目录结构:
----WebRoot
       ------filterJsp
                -----ExtremeCompomentTest_1.jsp
       ------login.jsp
       ------success.jsp

9.结果:
在IE中输入:http://localhost:8080/FileUpload/login.jsp如下显示

提交表单之后跳转的页面为:


等待1分钟之后,在Eclipse的控制台出现"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"信息时,即Session已经失效了,再点击上面的"Forward to Filter URL"链接,这时候过滤器filter就会起作用,验证Session失效后,跳转到登录界面。

热点排行