web_day40_Filter_入门案例&自动登录

一、Filter

1、介绍

  • Filter(过滤器)是一个运行在服务器端,先于与之相关的Servlet或者Jsp页面之前运行,对请求资源进行过滤功能的技术
  • Filter是JavaWEB三大组件(Servlet、Filter、 Listener)之一

2、编写流程

  • 编写一个类实现javax.servlet.Filter接口
  • 在web.xml中配置<filer>和<filter-mapping>配置项【快速创建方式不需要】

3、应用场景

  • 自动登录
  • 全站中文乱码处理
  • 屏蔽非法字符
  • 数据格式压缩

4、入门案例

package com.itheima.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理post请求中文乱码问题
		request.setCharacterEncoding("utf-8");
		// 处理响应的中文乱码问题
		response.setContentType("text/html;charset=utf-8");
		
		System.out.println("已抵达目标资源...");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
package com.itheima.web.filter;

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;

public class AFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("进入过滤器了...");
		// 放行
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {

	}

}

 

5、执行流程

 

二、案例:自动登录

1、自动登录需求分析

2、自动登录案例执行流程

登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css"
	type="text/css" />
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"
	type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"
	type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/style.css" type="text/css" />

<style>
body {
	margin-top: 20px;
	margin: 0 auto;
}

.carousel-inner .item img {
	width: 100%;
	height: 300px;
}

.container .row div {
	/* position:relative;
	 float:left; */
	
}

font {
	color: #666;
	font-size: 22px;
	font-weight: normal;
	padding-right: 17px;
}
</style>
</head>
<body>




	<!--
            	时间:2015-12-30
            	描述:菜单栏
            -->
	<div class="container-fluid">
		<div class="col-md-4">
			<img src="${pageContext.request.contextPath}/img/logo2.png" />
		</div>
		<div class="col-md-5">
			<img src="${pageContext.request.contextPath}/img/header.png" />
		</div>
		<div class="col-md-3" style="padding-top: 20px">
			<ol class="list-inline">
				<li><a href="login.htm">登录</a></li>
				<li><a href="register.htm">注册</a></li>
				<li><a href="cart.htm">购物车</a></li>
			</ol>
		</div>
	</div>
	<!--
            	时间:2015-12-30
            	描述:导航条
            -->
	<div class="container-fluid">
		<nav class="navbar navbar-inverse">
			<div class="container-fluid">
				<!-- Brand and toggle get grouped for better mobile display -->
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed"
						data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
						aria-expanded="false">
						<span class="sr-only">Toggle navigation</span> <span
							class="icon-bar"></span> <span class="icon-bar"></span> <span
							class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="#">首页</a>
				</div>

				<!-- Collect the nav links, forms, and other content for toggling -->
				<div class="collapse navbar-collapse"
					id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li class="active"><a href="#">手机数码<span class="sr-only">(current)</span></a></li>
						<li><a href="#">电脑办公</a></li>
						<li><a href="#">电脑办公</a></li>
						<li><a href="#">电脑办公</a></li>
					</ul>
					<form class="navbar-form navbar-right" role="search">
						<div class="form-group">
							<input type="text" class="form-control" placeholder="Search">
						</div>
						<button type="submit" class="btn btn-default">Submit</button>
					</form>

				</div>
				<!-- /.navbar-collapse -->
			</div>
			<!-- /.container-fluid -->
		</nav>
	</div>







	<div class="container"
		style="width:100%;height:460px;background:#FF2C4C url('${pageContext.request.contextPath}/img/loginbg.jpg') no-repeat;">
		<div class="row">
			<div class="col-md-7">
				<!--<img src="${pageContext.request.contextPath}/image/login.jpg" width="500" height="330" alt="会员登录" title="会员登录">-->
			</div>

			<div class="col-md-5">
				<div
					style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;">
					<font>会员登录</font>USER LOGIN
					<div>
						${msg }
					</div>
					<div>&nbsp;</div>
					<%--登录表单start --%>
					<form action="${pageContext.request.contextPath }/LoginServlet" method="post" class="form-horizontal">

						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">用户名</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="username" name="username"
									placeholder="请输入用户名">
							</div>
						</div>
						<div class="form-group">
							<label for="password" class="col-sm-2 control-label">密码</label>
							<div class="col-sm-6">
								<input type="password" class="form-control" id="password" name="password"
									placeholder="请输入密码">
							</div>
						</div>
						<div class="form-group">
							<label for="verifyCode" class="col-sm-2 control-label">验证码</label>
							<div class="col-sm-3">
								<input type="text" class="form-control" id="verifyCode" name="verifyCode"
									placeholder="请输入验证码">
							</div>
							<div class="col-sm-3">
								<img src="${pageContext.request.contextPath}/img/code.jpg" />
							</div>

						</div>
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<div class="checkbox">
									<label> 
										<input type="checkbox" name="autoLogin"> 自动登录
									</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
									<label> 
										<input type="checkbox" name="rememberme"> 记住用户名
									</label>
								</div>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<input type="submit" width="100" value="登录"
									style="background: url('${pageContext.request.contextPath}/img/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
    height:35px;width:100px;color:white;">
							</div>
						</div>
					</form>
					<%--登录表单end --%>
				</div>
			</div>
		</div>
	</div>

	<div style="margin-top: 50px;">
		<img src="${pageContext.request.contextPath}/img/footer.jpg"
			width="100%" height="78" alt="我们的优势" title="我们的优势" />
	</div>

	<div style="text-align: center; margin-top: 5px;">
		<ul class="list-inline">
			<li><a>关于我们</a></li>
			<li><a>联系我们</a></li>
			<li><a>招贤纳士</a></li>
			<li><a>法律声明</a></li>
			<li><a>友情链接</a></li>
			<li><a target="_blank">支付方式</a></li>
			<li><a target="_blank">配送方式</a></li>
			<li><a>服务声明</a></li>
			<li><a>广告声明</a></li>
		</ul>
	</div>
	<div style="text-align: center; margin-top: 5px; margin-bottom: 20px;">
		Copyright &copy; 2005-2016 传智商城 版权所有</div>
</body>
</html>

首页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
	<c:if test="${not empty existUser }">
		你好,${existUser.username }
	</c:if>
</body>
</html>

自动登录过滤器

package com.itheima.web.filter;

import java.io.IOException;
import java.sql.SQLException;

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.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.CookieUtils;

public class LoginFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("自动登录过滤器");
		// 1.强转
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		// 2.从session中获得用户信息
		User sessionUser = (User) request.getSession().getAttribute("existUser");
		// 3.判断
		if (sessionUser != null) {
			// 说明用户已经处于登录状态,不需要自动登录,放行
			chain.doFilter(request, response);
			// 不执行下面的代码
			return;
		}
		/// **************用户处于未登录状态,判断用户是否需要自动登录
		// 4.获得所有的Cookie
		Cookie[] cookies = request.getCookies();
		Cookie cookie = CookieUtils.getCookieByName(cookies, "autoLogin");
		// 5.非空判断
		if (cookie != null) {
			// 说明需要自动登录,获得用户名和密码
			String username = cookie.getValue().split(":")[0];
			String password = cookie.getValue().split(":")[1];
			// 创建User对象
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			// 调用service层方法
			UserService service = new UserService();
			try {
				User existUser = service.login(user);
				// 判断
				if (existUser != null) {
					// 放行,让其进入目标资源
					request.getSession().setAttribute("existUser", existUser);
					chain.doFilter(request, response);
					// 不让下面的代码执行
					return;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 6.放行
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {

	}

}

 LoginServlet

package com.itheima.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.itheima.domain.User;
import com.itheima.service.UserService;

/**
 * 用户登录
 * 
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理post请求中文乱码问题
		request.setCharacterEncoding("utf-8");
		// 处理响应的中文乱码问题
		response.setContentType("text/html;charset=utf-8");

		// 1.创建一个user对象
		User user = new User();
		try {
			BeanUtils.populate(user, request.getParameterMap());

			UserService service = new UserService();
			User existUser = service.login(user);

			if (existUser != null) {
				// 登录成功,判断用户是否勾选了自动登录的复选框
				String autoLogin = request.getParameter("autoLogin");
				if (autoLogin != null) {
					// 说明用户已经勾选了自动登录复选框,将用户名和密码保存到Cookie(不要直接将user对象保存到cookie,cookie的数据可能被篡改!)
					Cookie cookie = new Cookie("autoLogin", existUser.getUsername() + ":" + existUser.getPassword());
					cookie.setPath("/");
					cookie.setMaxAge(60 * 60 * 24 * 7);
					response.addCookie(cookie);
				}
				// 将用户信息保存到域对象中
				request.getSession().setAttribute("existUser", existUser);
				//页面跳转
                response.sendRedirect(request.getContextPath()+"/index.jsp");

			} else {
				// 说明登录失败,给出提示信息
				request.setAttribute("msg", "<span style='color:red'>用户名或密码错误</span>");
				 //转发
                request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
package com.itheima.utils;

import javax.servlet.http.Cookie;

public class CookieUtils {

	/**
	 * 获取指定名称的cookie
	 */
	public static Cookie getCookieByName(Cookie[] cookies, String cookieName) {
		// 1.非空判断
		if (cookies != null) {
			// 2.遍历
			for (Cookie cookie : cookies) {
				// 3.判断
				if (cookieName.equals(cookie.getName())) {
					// 4.返回
					return cookie;
				}
			}
		}
		return null;
	}
}

 service层

package com.itheima.service;

import java.sql.SQLException;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;

public class UserService {

	public User login(User user) throws SQLException {
		UserDao dao = new UserDao();
		return dao.login(user);
	}

}

dao层

package com.itheima.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.itheima.domain.User;
import com.itheima.utils.C3P0Utils;

public class UserDao {

	public User login(User user) throws SQLException {
		 // 1.获得QueryRunner核心对象
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        // 2.编写SQL语句
        String sql = "select * from user where username=? and password=?";
        // 3.设置实际参数
        Object[] params = {user.getUsername(),user.getPassword()};
        // 4.执行查询操作
        return qr.query(sql, new BeanHandler<>(User.class), params);
	}

}

web.xml

<?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" 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">
  <display-name>web_day40</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>AServlet</display-name>
    <servlet-name>AServlet</servlet-name>
    <servlet-class>com.itheima.web.servlet.AServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AServlet</servlet-name>
    <url-pattern>/AServlet</url-pattern>
  </servlet-mapping>
  <filter>
    <display-name>AFilter</display-name>
    <filter-name>AFilter</filter-name>
    <filter-class>com.itheima.web.filter.AFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>AFilter</filter-name>
    <url-pattern>/AServlet</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.itheima.web.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <filter>
    <display-name>LoginFilter</display-name>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.itheima.web.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>
</web-app>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值