一、Filter接口
①参数
FilterConfig 类似 ServletConfig的用途
ServletRequest 请求
ServletResponse 响应
FilterChain 连接器链
②方法
public void init(FilterConfig cfg) throws ServletException
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException
public void destroy()
③配置
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.bright.filter.MyFilter</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>root</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/hello</url-pattern>
</filter-mapping>
<url-pattern>: 是被拦截的servlet的路径
④多个拦截器拦截同一个servlet
拦截器配置相同的url,拦截器执行的顺序,按照web.xml中配置的顺序
⑤一个拦截器拦截多个servlet
通配符匹配
⑥.使用拦截器处理字符集问题。统一处理字符集
在servlet执行之前,处理req.和resp的字符集
⑦ 非法登陆的统一处理
注意: 一定不要拦截用于登陆的servlet
⑧filter中,可以放行执行后续类,也可以直接响应浏览器
二、小项目:
1.验证码(session的使用)
2.保持用户名、密码,一周内自动登录(cookie)
3.统一设置字符集
4.非法登录的拦截器处理
5.使用session监听器,统计应用中登录的人数(通过生命周期监听器)
用户登录成功
–购物
–显示
登录失败
–显示表单重新登录
①登录表单form:FormServlet
前面导包等省略
public class FormServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();
out.println("用户登录<br/>");
out.println("<form action='login' method='post'>");
out.println("用户名:<input type='text' name='uname'><br/>");
out.println("<input type='checkbox' name='save' value=''>一周内自动登录<br/>");
out.println("<input type='submit' value='提交'/>");
out.println("</form>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
②处理登录login:LoginServlet
前面导包等省略
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//先获取uname
String uname=req.getParameter("uname");
PrintWriter out=resp.getWriter();
//如果用户存在
if(uname!=null&&uname.length()>0){
HttpSession session=req.getSession();//没有session就创建
session.setAttribute("islogin", "ok");//如果登录成功,就把islogin存在session中,并记作ok
out.println("用户名:"+uname+"<br/>");
out.println("恭喜你,登录成功<br/>");
out.println("<a href='user/buy'>购买</a><br/>");
out.println("<a href='user/show'>显示</a><br/>");
}else{
out.println("登录失败<br/>");
out.println("<a href='form'>重新登录</a>");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
③购物buy:BuyServlet
前面导包等省略
public class BuyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();
out.println("买买买====剁手剁手剁手");
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
④显示信息show:ShowServlet
public class ShowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();
out.println("买好了,付款咯8888¥");
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
⑤设置字符集的过滤器:EncodingFilter
public class EncodingFilter implements Filter {
private String encoding="";
public void init(FilterConfig cfg) throws ServletException {
this.encoding=cfg.getInitParameter("encoding");
}
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//设置请求的字符集
req.setCharacterEncoding(encoding);
//设置响应的字符集
resp.setCharacterEncoding(encoding);
//设置响应类型
resp.setContentType("text/html");
//这一句不能少
chain.doFilter(req, resp);
}
}
⑥非法登录的拦截器处理:LoginFilter
public class LoginFilter implements Filter {
public void destroy() { }
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//在请求发送之前,判断用户是否登录
HttpSession session=((HttpServletRequest)req).getSession();
//得到sesison
String islogin=(String)req.getAttribute("islogin");
//判断session是否和ok相等
if("ok".equals(islogin)){
chain.doFilter(req, resp);
}else{
((HttpServletResponse)resp).sendRedirect("/day10/login");
}
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
``
⑦配置: