filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标资源访问前后进行逻辑处理
作用:
步骤:
实例代码
//[filter/QuickFilter1.java]
package 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 QuickFilter1 implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("quickfilter1... start...");
//放行请求
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
//[web.xml]中添加的内容
QuickFilter1
filter.QuickFilter1
QuickFilter1
/*
filter生命周期及其与生命周期相关的方法
Filter的API详情
@Override
public void init(FilterConfig arg0) throws ServletException {
//获得web.xml中QuickFilter1 的名字
System.out.println(arg0.getFilterName());
//获得当前filter的初始化参数aaa bbb
System.out.println(arg0.getInitParameter("aaa"));
//获得所有初始化参数的名字
System.out.println(arg0.getInitParameterNames());
//获得servletContext对象(故先有servletContext)
System.out.println(arg0.getServletContext());
System.out.println("init...");
}
基本配置如下:
<filter>
<filter-name>QuickFilter1filter-name>
<filter-class>filter.QuickFilter1filter-class>
<init-param>
<param-name>aaaparam-name>
<param-value>bbbparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>QuickFilter1filter-name>
<url-pattern>/*url-pattern>
<dispatcher>REQUESTdispatcher>
filter-mapping>
<filter>
<filter-name>QuickFilter2filter-name>
<filter-class>filter.QuickFilter2filter-class>
filter>
<filter-mapping>
<filter-name>QuickFilter2filter-name>
<url-pattern>/*url-pattern>
<dispatcher>FORWARDdispatcher>
filter-mapping>
url-pattern配置时
完全匹配 /sertvle1
目录匹配 /aaa/bbb/* (最多的)
/user/*:访问前台的资源进入此过滤器
/admin/*:访问后台的资源时执行此过滤器
扩展名匹配 .abc .jsp
注意:url-pattern可以使用servlet-name替代,也可以混用,表示过滤指定的servlet。
dispatcher:访问的方式(了解)
实现流程:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post" style="height:100px;width:200px;margin:0 auto">
用户名:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="checkbox" name="autoLogin" value="autoLogin"/>自动登录<br>
<input type="submit" value="提交" style="height:20px;width:50px;margin:0 auto;"/>
form>
body>
html>
package web;
import java.io.IOException;
import java.sql.SQLException;
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 javax.servlet.http.HttpSession;
import domain.User;
import service.UserService;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取数据
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
User user = null;
UserService service = new UserService();
try {
//检查用户信息
user = service.login(username,password);
} catch (SQLException e) {
e.printStackTrace();
}
if(user != null) {
//登陆成功
//判断用户是否勾选自动登录
String autoLogin = request.getParameter("autoLogin");
if(autoLogin!=null) {
Cookie cookie_username = new Cookie("cookie_username",user.getUsername());
Cookie cookie_password = new Cookie("cookie_password",user.getPassword());
//设置cookie的持久化时间
cookie_username.setMaxAge(60*60);//设置一小时可用时间
cookie_password.setMaxAge(60*60);//设置一小时可用时间
//设置cookie的携带路径
cookie_username.setPath(request.getContextPath());
cookie_password.setPath(request.getContextPath());
//发送cookie
response.addCookie(cookie_username);
response.addCookie(cookie_password);
}
//将登陆的用户的user对象存到session中
session.setAttribute("user",user);
//重定向到首页
response.sendRedirect(request.getContextPath()+"/success.jsp");
}else {
//登录失败,转发到登录页面,给出提示信息
request.setAttribute("loginInfo", "用户名和密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package service;
import java.sql.SQLException;
import dao.UserDao;
import domain.User;
public class UserService {
public User login(String username, String password) throws SQLException {
UserDao dao = new UserDao();
return dao.login(username,password);
}
}
package dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import domain.User;
import utils.DataSourceUtils;
public class UserDao {
public User login(String username, String password) throws SQLException {
System.out.println(username+" "+password);
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username = ? and password = ?";
User user = runner.query(sql, new BeanHandler(User.class),username,password);
System.out.println(user);
return user;
}
}
package domain;
public class User {
private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
}
public User(int id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public User() {
super();
}
}
//success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
"Content-Type" content="text/html; charset=UTF-8">
Insert title here
if test="${empty user }">
- "${pageContext.request.contextPath}/">登陆
if>
if test="${!empty user }">
欢迎您,${user.username}
if>
//login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
"Content-Type" content="text/html; charset=UTF-8">
Insert title here
${loginInfo}
package 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 javax.servlet.http.HttpSession;
import domain.User;
import service.UserService;
public class AutoLoginFilter implements Filter{
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
//获得cookie中的用户名和密码进行登录操作
//定义cookie_username
String cookie_username = null;
//定义cookie_password
String cookie_password = null;
//获得cookie
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie:cookies) {
//获得名字是cookie_username和cookie_password的cookie
if(cookie.getName().equals("cookie_username")) {
cookie_username = cookie.getValue();
}
if(cookie.getName().equals("cookie_password")) {
cookie_password = cookie.getValue();
}
}
}
//判断username和password是否是null
if(cookie_username!=null&&cookie_password!=null) {
HttpSession session = request.getSession();
User user = null;
UserService service = new UserService();
try {
user = service.login(cookie_username,cookie_password);
} catch (SQLException e) {
e.printStackTrace();
}
//将登陆的用户存到session中
if(user != null) {
session.setAttribute("user",user);
}
}
//放行
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
<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>WEB24_FILTERdisplay-name>
<filter>
<filter-name>AutoLoginFilterfilter-name>
<filter-class>filter.AutoLoginFilterfilter-class>
filter>
<filter-mapping>
<filter-name>AutoLoginFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<description>description>
<display-name>LoginServletdisplay-name>
<servlet-name>LoginServletservlet-name>
<servlet-class>web.LoginServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginServletservlet-name>
<url-pattern>/loginurl-pattern>
servlet-mapping>
web-app>
测试功能是否好用的思路
解决全局乱码的问题
问题出现原因
cookie不能存中文
登录时输入中文,后台接受时会出现乱码
get时需要在后台添加先编码再解码
parameter = new String(parameter.getBytes("ios8859-1"),"UTF-8");
request.setCharacterEncoding("UTF-8");
解决问题
解决cookie不能存中文的问题:
String username_code = URLEncoder.encode(username,"UTF-8");
String password_code = URLEncoder.encode(password,"UTF-8");
//恢复中文用户名
cookie_username = URLDecoder.decode(cookie_username,"UTF-8");
//恢复中文密码
cookie_password = URLDecoder.decode(cookie_password,"URF-8");
解决登录中输入中文后台接受出现乱码的问题
package filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.HttpServletRequestWrapper;
public class EncodingFilter implements Filter{
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
//被增强的对象
HttpServletRequest request = (HttpServletRequest)arg0;
if(request.getMethod().equals("POST")) {
request.setCharacterEncoding("UTF-8");
arg2.doFilter(request, arg1);
}else if(request.getMethod().equals("GET")){
//在传递request之前,对request的getParamter方法进行增强
/*
* 利用装饰者模式(包装)
* 1.增强类与被增强的类实现同一个接口
* 2.在增强类中传入被增强的类
* 3.需要增强的方法重写,不需要增强的方法调用被增强对象的
*/
//增强对象
EnhanceRequest enchangeRequest = new EnhanceRequest(request);
arg2.doFilter(enchangeRequest, arg1);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
//HttpServletRequestWrapper是专门为了实现装饰者模式的类
class EnhanceRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public EnhanceRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
//对getParamter增强
@Override
public String getParameter(String name) {
String parameter = request.getParameter(name);//乱码
try {
parameter = new String(parameter.getBytes("iso8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parameter;
}
}