00.通过demo进行学习(编写登录验证页面进行学习filter)
1.编写一级过滤器,实现编码的统一
package demo.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;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
//@WebFilter("/*") //后面统一使用xml进行配置
public class Filter1 extends HttpFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("初始化请求第一个过滤器!");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("一级过滤器启动!");
//编码统一,处理乱码!
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);
System.out.println("一级过滤器通过!");
}
public void destroy() {
System.out.println("一级过滤器destory");
}
}
2.编写二级过滤器实现访问当前文件夹下的Servlet,如果用户已经登录则显示“登录成功”,否则再次跳转到登录页面,并显示“请登录”
package demo.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 登录验证的过滤器
*/
//@WebFilter("/request/*")
public class Filter2 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("二级过滤器启动!");
//0.强制转换
HttpServletRequest request = (HttpServletRequest) req;
//HttpServletResponse response = (HttpServletResponse) req;
//1.获取资源请求路径
String uri = request.getRequestURI();
//2.判断是否包含登录相关资源路径,要注意排除掉其他资源
if( uri.contains(" uri.contains(\"/login2.jsp\") || /LoginServlet") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/fonts/") || uri.contains("/checkCodeServlet") ){
//包含,用户就是想登录。放行
chain.doFilter(req, resp);
System.out.println("二级过滤器通过!");
}else{
//不包含,需要验证用户是否登录
//3.从获取session中获取user
Object user = request.getSession().getAttribute("uname");
if(user != null){
request.setAttribute("login_message","登录成功!");
System.out.println("二级过滤器通过,并登录成功!");
//登录了。放行
chain.doFilter(req, resp);
}else{
//没有登录。跳转登录页面
request.setAttribute("login_msg","请登录!");
request.getRequestDispatcher("/login2.jsp").forward(request,resp);
}
}
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
2-1、登录验证的servlet
package com.filter.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
//获取Session
HttpSession session = request.getSession();
//验证用户名和密码是否有效
//将用户名放入session 并且设置名称为uname(在过滤器中获取时,直接获取uname)
session.setAttribute("uname",username);
//设定session的有效期
session.setMaxInactiveInterval(900);
//将获取的用户名和密码打印在页面上
response.getWriter().print("用户名:"+username);
response.getWriter().print("<br/>");
response.getWriter().print("密码:"+pwd);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2-2、登录的表单
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="pwd"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
3. 在xml文件配置中实现过滤器链
<!-- filter1过滤器配置文件 -->
<filter>
<filter-name>Filter1</filter-name>
<!-- 注意自己项目的路径 -->
<filter-class>demo.filter.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- filter2过滤器配置文件 -->
<filter>
<filter-name>Filter2</filter-name>
<!-- 注意自己项目的路径 -->
<filter-class>demo.filter.Filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>