*****检测用户是否登陆的过滤器:不需要用户跳转到每个页面都需要登陆,访问一群页面时,只在某个页面上登陆一次,就可以访问其他页面;

1.自定义抽象的 HttpFilter类, 实现自 Filter 接口

package com.lanqiao.javaweb;

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; // 自定义的 HttpFilter, 实现自 Filter 接口 public abstract class HttpFilter implements Filter { // 用于保存 FilterConfig 对象. private FilterConfig filterConfig; // 不建议子类直接覆盖. 若直接覆盖, 将可能会导致 filterConfig 成员变量初始化失败 @Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
init();
} //供子类继承的初始化方法. 可以通过 getFilterConfig() 获取 FilterConfig 对象.
protected void init() {} // 直接返回 init(ServletConfig) 的 FilterConfig 对象 public FilterConfig getFilterConfig() {
return filterConfig;
} /**
* 原生的 doFilter 方法, 在方法内部把 ServletRequest 和 ServletResponse
* 转为了 HttpServletRequest 和 HttpServletResponse, 并调用了
* doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
*
* 若编写 Filter 的过滤方法不建议直接继承该方法. 而建议继承
* doFilter(HttpServletRequest request, HttpServletResponse response,
* FilterChain filterChain) 方法
*/
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp; doFilter(request, response, chain);
} //抽象方法, 为 Http 请求定制. 必须实现的方法. public abstract void doFilter(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws IOException, ServletException; //空的 destroy 方法。
@Override
public void destroy() {} }

2.在src目录下建立类:LoginFilter ,其继承于父类: HttpFilter

package com.lanqiao.javaweb;

import java.io.IOException;
import java.util.Arrays;
import java.util.List; import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.lanqiao.javaweb.HttpFilter; public class LoginFilter extends HttpFilter{ //1. 从 web.xml 文件中获取 sessionKey, redirectUrl, uncheckedUrls
private String sessionKey;
private String redirectUrl;
private String unchekcedUrls; @Override
protected void init() {
ServletContext servletContext = getFilterConfig().getServletContext(); sessionKey = servletContext.getInitParameter("userSessionKey");
redirectUrl = servletContext.getInitParameter("rediretPage"); //不需要检查的url是: login/a.jsp,/login/list.jsp,/login/login.jsp,/login/doLogin.jsp
unchekcedUrls = servletContext.getInitParameter("uncheckedUrls");
} @Override
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException { //1. 获取请求的 servletPath
// /login/b.jsp
String servletPath = request.getServletPath(); //2. 检查 1 获取的 servletPath 是否为不需要检查的 URL 中的一个, 若是, 则直接放行. 方法结束
List<String> urls = Arrays.asList(unchekcedUrls.split(","));
if(urls.contains(servletPath)){
filterChain.doFilter(request, response);
return;
} //3. 从 session 中获取 sessionKey 对应的值, 若值不存在, 则重定向到 redirectUrl
Object user = request.getSession().getAttribute(sessionKey);
if(user == null){
response.sendRedirect(request.getContextPath() + redirectUrl);
return;
} //4. 若存在, 则放行, 允许访问.
filterChain.doFilter(request, response);
} }

3.在lib下边的web.xml文件为:对LoginFilter类进行配置和映射;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.lanqiao.javaweb.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>loginFilter</filter-name>

    //映射到WebContent目录下的login里
    <url-pattern>/login/*</url-pattern>
  </filter-mapping


<!-- 用户信息放入到 session 中的键的名字 -->
<context-param>
<param-name>userSessionKey</param-name>
<param-value>USERSESSIONKEY</param-value>
</context-param> <!-- 若未登录, 需重定向的页面 -->
<context-param>
<param-name>rediretPage</param-name>
<param-value>/login/login.jsp</param-value>
</context-param> <!-- 不需要拦截(或检查)的 URL 列表 -->
<context-param>
<param-name>uncheckedUrls</param-name>
<param-value>/login/a.jsp,/login/list.jsp,/login/login.jsp,/login/doLogin.jsp,/login/b.jsp</param-value>
</context-param> </web-app>

4.WebContent目录下的login里面的jsp;页面之间进行跳转,只要一个页面输入用户名一次就可以实现跳转到其他页面不需要输入用户名了

a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <h4>AAA PAGE</h4> <a href="list.jsp">Return...</a> </body>
</html>

b.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <h4>BBB PAGE</h4> <a href="list.jsp">Return...</a> </body>
</html>

c.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <h4>CCC PAGE</h4> <a href="list.jsp">Return...</a> </body>
</html>

d.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <h4>DDD PAGE</h4>
<a href="list.jsp">Return...</a> </body>
</html>

doLogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <%
//1. 获取用户的登录信息
String username = request.getParameter("username"); //2. 若登录信息完整, 则把登录信息放到 HttpSession
if(username != null && !username.trim().equals("")){
session.setAttribute(application.getInitParameter("userSessionKey"), username);
//3. 重定向到 list.jsp
response.sendRedirect("list.jsp");
}else{
response.sendRedirect("login.jsp");
} %> </body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <a href="a.jsp">AAA</a>
<br><br> <a href="b.jsp">BBB</a>
<br><br> <a href="c.jsp">CCC</a>
<br><br> <a href="d.jsp">DDD</a>
<br><br> </body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body> <form action="doLogin.jsp" method="post">
username: <input type="text" name="username"/>
<input type="submit" value="Submit"/>
</form> </body>
</html>

过滤器(Filter)案例:检测用户是否登陆的过滤器的更多相关文章

  1. [原创]java WEB学习笔记45:自定义HttpFilter类,理解多个Filter 代码的执行顺序,Filterdemo:禁用浏览器缓存的Filter,字符编码的Filter,检查用户是否登陆过的Filter

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. java之过滤器Filter (应用场景)

    filter在开发中的常见应用:     * 1.filter可以目标资源执行之前,进行权限检查,检查用户有无权限,如有权限则放行,如没有,则拒绝访问     * 2.filter可以放行之前,对re ...

  3. Servlet中的过滤器Filter用法

    1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...

  4. Servlet中的过滤器Filter详解

    加载执行顺序 context-param->listener->filter->servlet web.xml中元素执行的顺序listener->filter->stru ...

  5. 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能

    在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...

  6. 用过滤器Filter判断用户是否登陆

    用过滤器Filter判断用户是否登陆 WEB.XML <!-- 用户session的 键   sessionKEY --> <context-param> <param- ...

  7. 过滤器(filter)实现用户登录拦截

    过滤器(filter)实现用户登录拦截 >>>>>>>>>>>>>>>>>>>> ...

  8. java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

    java 过滤器Filter: package com.sun.test.aircraft.filter;import javax.servlet.*;import java.io.IOExcepti ...

  9. 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)

    一.Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...

随机推荐

  1. button点击传多个参数

    // --------------------button点击传多个参数------------------------ UIButton *btn = [UIButton buttonWithTyp ...

  2. javascript异步加载的三种解决方案

    默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很大的javascript,如果放在页 ...

  3. Applescript 带参数调用某个App的方法

    do shell script "open '/Users/eran/Documents/Workground/DragonAdventure/FlashCode/tools/SWFInfo ...

  4. 轉發和重定向-JSP

    最近在復習JSP,寫案例時遇到轉發和重定向的問題,忽然忘記了好多東西.趕緊搜索了下,感覺還是比較常用的. 轉:http://blog.csdn.net/CYHJRX/article/details/3 ...

  5. C++builder 图像字符流的存储和加载

    __fastcall TForm6::TForm6(TComponent* Owner) : TForm(Owner) { #if 1 //for debug AllocConsole(); Atta ...

  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  7. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  8. mysql报错 "code":"08S01","msg":"SQLSTATE

    2016-04-25 09:22 97人阅读 评论(0) 收藏 举报 分类: Magento(6) 今天在批量伪造测试数据时,MySQL收到下面异常:ERROR 1153 (08S01): Got a ...

  9. fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL

    SELECT * FROM FND_PROFILE_OPTIONS_VL TT WHERE TT.PROFILE_OPTION_NAME LIKE '%AFLOG%' FND:启用调试日志 详细的参考 ...

  10. less文件的样式无法生效的一个原因,通过WEB浏览器访问服务器less文件地址返回404错误

    有一种情况容易导致less文件的样式无法生效,就是部分服务器(以IIS居多)会对未知后缀的文件返回404,导致无法正常读取.less文件.解决方案是在服务器中为.less文件配置MIME值为text/ ...