老样子,PPT先贴上,想看PPT,就看PPT,想看代码,就在下面(^-^)V
过虑器简介:
过虑器的过虑过程:
创建自定义的过虑器:
QUICK-START:
也可以在doFilter方法,设置response的响应头信息。设置好之后再其他的Servlet中就不用再设置响应头了,如下:
response.setContentType(“text/html;charset=UTF-8”);
发布过虑器:
将字符集配置到过虑器的初始化参数中:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
主页页面
web.xml
hello
cn.hncu.filter.HelloFilter
second
cn.hncu.filter.SecondFilter
charset
cn.hncu.filter.CharacterFilter
charset
UTF-8
LoginServlet
cn.hncu.servlet.LoginServlet
second
/*
hello
/*
charset
/*
LoginServlet
/LoginServlet
index.jsp
LoginServlet.java这个服务器没什么内容就是,接收前台的name值,然后输出来
package cn.hncu.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 LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
System.out.println("name: "+name);
}
}
HelloFilter.java
package cn.hncu.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;
/*
* 过滤器开发:
* 1.写一个类实现Filter的三个抽象方法
* 2.配置web.xml(需要的参数可以通过F2查看)
*/
public class HelloFilter implements Filter{
//过滤器的init方法在服务器启动的时候就执行
//servlet要配置load-on-startup,就可以在服务器启动时执行初始化,否则只有第一次请求时才会执行
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("这里执行了过滤器的init方法");
}
@Override//该方法只有在拦截到相应请求时或者响应时才会执行
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("这里执行了过滤器的doFilter方法");//拦截请求
chain.doFilter(request, response);//放行,如果不放行,请求就被拦在这里了,无法到达后台
System.out.println("这里是服务器的响应....");//拦截响应
}
@Override
public void destroy() {
System.out.println("这里执行了过滤器的destroy方法");
}
}
SecondFilter.java
package cn.hncu.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 SecondFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("这里是第二个filter执行了过滤器的init方法");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("这里是第二个filter执行了过滤器的doFilter方法");
chain.doFilter(request, response);
System.out.println("这里是第二个filter是服务器的响应....");
}
@Override
public void destroy() {
System.out.println("这里是第二个filter执行了过滤器的destroy方法");
}
}
这是一个设置编码,解决中文乱码问题的较好的一个模板!
CharacterFilter.java
package cn.hncu.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 CharacterFilter implements Filter{
String charset;
@Override
public void init(FilterConfig config) throws ServletException {
charset=config.getInitParameter("charset");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// request.setCharacterEncoding("utf-8");这里的设置编码写死了
request.setCharacterEncoding(charset);//这里更灵活,应让用户在xml中配置编码
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
init方法在服务器启动的时候就执行
doFilter方法放行
destroy方法,在服务器关闭的时候执行
两个过滤器时,init方法的执行顺序:详细见web.xml中有解释!
两个过滤器时,doFilter方法的执行顺序:
中文乱码问题:
用过滤器就解决了:
包、类分布图:
利用过滤器实现安全登录
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
主页页面
购物1
购物2
会员专区
web.xml配置
charset
cn.hncu.filter.CharacterFilter
charset
UTF-8
LoginSecureFilter
cn.hncu.filter.LoginSecureFilter
LoginServlet
cn.hncu.servlet.LoginServlet
BuyServlet
cn.hncu.servlet.BuyServlet
BuyServlet2
cn.hncu.servlet.BuyServlet2
charset
/*
LoginSecureFilter
/jsps/*
/buy/*
/imgs/*
REQUEST
FORWARD
LoginServlet
/LoginServlet
BuyServlet
/buy/BuyServlet
BuyServlet2
/buy/BuyServlet2
index.jsp
LoginServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//这里省去若干代码(service,dao到数据库中验证身份)
String name=request.getParameter("name");
if(name!=null&&! "".equals(name.trim())&&name.length()>2){
//这里假设登录成功
request.getSession().setAttribute("name", name);
}
response.sendRedirect(request.getContextPath()+"/jsps/welcome.jsp");
}
}
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
welcome ${name } !
那个CharacterFilter.java过滤器,在上面已经贴过,这里不再重复。
LoginSecureFilter.java
package cn.hncu.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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginSecureFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
if(req.getSession().getAttribute("name")==null){
//这里就表示没有登录-----踢到登录界面
HttpServletResponse resp=(HttpServletResponse) response;
//注意这里是重定向,要用绝对路径
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
BuyServlet.java和BuyServlet2.java没有实际意义的代码,只是用来测试过滤器的,也不再贴出了。
a.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
welcome 会员${name } !
这里有图片可以浏览:
主页图:
登录成功图:
包、类分布图: