FilterConfig接口、Listener监听器
FilterConfig接口
FilterConfig 是 Servlet API 提供的一个用于获取 Filter 程序在 web.xml 文件中的配置信息的接口,该接口封装了 Filter 程序在 web.xml 中的所有注册信息,并且提供了一系列获取这些配置信息的方法,具体如下所示。
FilterConfig 接口中的方法
方法声明 | 功能描述 |
---|---|
String getFilterName() | getFilterName() 方法用于返回在 web.xml 文件中为 Filter 所设置的名称,也就是返回 <filter-name> 元素的设置值 |
String getInitParameter(String name) | getInitParameter(String name) 方法用于返回在 web.xml 文件中为 Filter 所设置的某个名称的初始化参数值,如果指定名称的初始化参数不存在,则返回 null |
Enumeration getInitParameterNames() | getInitParameterNames() 方法用于返回一个 Enumeration 集合对象,该集合对象包含在 web.xml 文件中为当前 Filter 设置的所有初始化参数的名称 |
ServletContext getServletContext() | getServletContext()方法用于返回 FilterConfig 对象中所包装的 ServletContext 对象的引用 |
创建过滤器MyFiter03,使用该过滤器来获取web.xml中设置的参数
package cn.itcast.chapter08.filter;
import java.io.*;
import javax.servlet.*;
public class MyFilter03 implements Filter {
private String characterEncoding;
FilterConfig fc;
public void init(FilterConfig fConfig) throws ServletException {
// 获取FilterConfig对象
this.fc = fConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 输出参数信息
characterEncoding=fc.getInitParameter("encoding");
System.out.println("encoding初始化参数的值为:"+characterEncoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
运行结果
使用Filter实现用户自动登录
实现步骤
编写一个User类,该类用于封装用户的信息
package cn.itcast.chapter08.entity;
public class User {
private String username;
private String password;
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;
}
}
编写login.jsp 页面,该页面用于创建一个用户登录的表单,这个表单需要填写用户名和密码,以及用户自动登录的时间
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<html>
<head></head>
<center><h3>用户登录</h3></center>
<body style="text-align: center;">
<form action="${pageContext.request.contextPath }/LoginServlet"
method="post">
<table border="1" width="600px" cellpadding="0" cellspacing="0"
align="center" >
<tr>
<td height="30" align="center">用户名:</td>
<td>
<input type="text" name="username" />${errerMsg }</td>
</tr>
<tr>
<td height="30" align="center">密 码:</td>
<td>
<input type="password" name="password" /></td>
</tr>
<tr>
<td height="35" align="center">自动登录时间</td>
<td><input type="radio" name="autologin"
value="${60*60*24*31 }" />一个月
<input type="radio" name="autologin"
value="${60*60*24*31*3 }" />三个月
<input type="radio" name="autologin"
value="${60*60*24*31*6 }" />半年
<input type="radio" name="autologin"
value="${60*60*24*31*12 }" />一年
</td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
<html>
编写indexjsp页面,该页面用于显示用户的登录信息。如果没有用户登录,在index jsp页面中就显示一个用户登录的超链接。 如果用户E经登录,在index.jsp页面中显示登录的用户名,以及-个注销的超链接
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>显示登录的用户信息</title>
</head>
<body>
<br />
<center>
<h3>欢迎光临</h3>
</center>
<br />
<br />
<c:choose>
<c:when test="${sessionScope.user==null }">
<a href="${pageContext.request.contextPath }/login.jsp">用户登录</a>
</c:when>
<c:otherwise>
欢迎你,${sessionScope.user.username }!
<a href="${pageContext.request.contextPath }/LogoutServlet">注销</a>
</c:otherwise>
</c:choose>
<hr />
</body>
</html>
编写一个LoginServlet类,该类用于处理用户的登录请求。如果输入的用户名和密码正确,则发送一一个用户自动登录的Cookie,并跳转到首页;否则会提示输入的用户名或密码错误,并跳转至登录页面login.jsp 让用户重新登录
package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import cn.itcast.chapter08.entity.User;
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");
// 检查用户名和密码
if ("itcast".equals(username) && "123456".equals(password)) {
// 登录成功
// 将用户状态 user 对象存入 session域
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
// 发送自动登录的cookie
String autoLogin = request.getParameter("autologin");
if (autoLogin != null) {
// 注意 cookie 中的密码要加密
Cookie cookie = new Cookie("autologin", username + "-"
+ password);
cookie.setMaxAge(Integer.parseInt(autoLogin));
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
// 跳转至首页
response.sendRedirect(request.getContextPath()+"/index.jsp");
} else {
request.setAttribute("errerMsg", "用户名或密码错");
request.getRequestDispatcher("/login.jsp")
.forward(request,response);
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
编写一个Logouservlet类,该类用于注锅用户登录的信息。在这个程序中首先会将Sssn会话中保存的User对象删除,然后将自动登录的Cookie删除,最后跳转到index.jsp
package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 用户注销
request.getSession().removeAttribute("user");
// 从客户端删除自动登录的cookie
Cookie cookie = new Cookie("autologin", "msg");
cookie.setPath(request.getContextPath());
cookie.setMaxAge(0);
response.addCookie(cookie);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
创建过滤器
编写AutoLoginFilter类,该类用于拦截用户登录的访问请求,判断请求中是否包含用户自动登录的Cookie。如果包含,则获取Cookie中的用户名和密码,并验证用户名和密码是否正确。如果正确,则将用户的登录信息封装到Use对象存入Session域中,完成用户自动登录
package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import cn.itcast.chapter08.entity.User;
public class AutoLoginFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
// 获得一个名为 autologin 的cookie
Cookie[] cookies = request.getCookies();
String autologin = null;
for (int i = 0; cookies != null && i < cookies.length; i++) {
if ("autologin".equals(cookies[i].getName())) {
// 找到了指定的cookie
autologin = cookies[i].getValue();
break;
}
}
if (autologin != null) {
// 做自动登录
String[] parts = autologin.split("-");
String username = parts[0];
String password = parts[1];
// 检查用户名和密码
if ("itcast".equals(username)&& ("123456").equals(password)) {
// 登录成功,将用户状态 user 对象存入 session域
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
}
}
// 放行
chain.doFilter(request, response);
}
public void destroy() {
}
}
配置映射信息
在web.xml文件中,配置所有相关Servlet及Autol oginFilter过滤器信息。由于要拦截用户访问资源的所有请求,因此,将过滤器<filter -mapping>元素拦截的路径设置为“/*
运行项目,查看结果
访问login.jsp 页面
表单中输入用户名"itcast" "密码“123456”,并选择用户自动登录的时间
单击图中的[登录]按钮,便可完成用户自动登录,此时,在浏览器窗口中会显示登录的用户名,如图所示。
单击图中的[注销]超链接,就可以注销当前的用户,然后显示index.jsp页面
使用Fiter实现统一全站编码
编写一个 form.jsp页面,该页面用于提交用户登录的表单信息
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<!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">
</head>
<center>
<h3>用户登录</h3>
</center>
<body style="text-align: center;">
<a href="<%=request.getContextPath()%>/CharacterServlet?name=传智播客&password=123456">单击超链接登录</a>
<form action="<%=request.getContextPath()%>/CharacterServlet"
method="post">
<table border="1" width="600px" cellpadding="0" cellspacing="0"
align="center">
<tr>
<td height="30" align="center">用户名:</td>
<td> <input type="text" name="name" />
</td>
</tr>
<tr>
<td height="30" align="center">密 码:</td>
<td> <input type="password" name="password" />
</td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
创建Servlet
编写一个CharacterServlet类,该类用于获取用户输入的请求参数,并将参数输出到控制台
package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class CharacterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("name"));
System.out.println(request.getParameter("password"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
创建过滤器
编写一个CharacterFiter 类,CharacterFilter类用于拦截用户的请求访问,实现统一全站编码的功能。只是针对请求的方式不同,解决乱码的方式也不相同。其中,POST方式的请求参数存放在消息体中,可以通过setCharacterEncoding()方法进行设置;而GET方式的请求参数存放在消息头中,必须得通过获取UBr参数才能进行设置。如果每次单独对GET请求方式进行处理,势必会很麻烦。为此,可以通过HtpSeretRequestrapper类对HtSereletRequeset类进行包装,通过重写get Paramel()的方式来设置GET方式提交参数的编码
package cn.itcast.chapter08.filter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CharacterFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// 拦截所有的请求 解决全站中文乱码
// 指定 request 和 response 的编码
request.setCharacterEncoding("utf-8"); // 只对消息体有效
response.setContentType("text/html;charset=utf-8");
// 对request进行包装
CharacterRequest characterRequest = new CharacterRequest(request);
chain.doFilter(characterRequest, response);
}
public void destroy() {
}
}
// 继承 默认包装类HttpServletRequestWrapper
class CharacterRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public CharacterRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
// 子类继承父类一定会覆写一些方法,此处用于重写getParamter()方法
public String getParameter(String name) {
// 调用被包装对象的getParameter()方法,获得请求参数
String value = super.getParameter(name);
if (value == null)
return null;
// 判断请求方式
String method = super.getMethod();
if ("get".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
// 解决乱码后返回结果
return value;
}
}
在web.xml文件中,配置CharacterServlet和CharacterFilter的映射信息。由于要拦截用户访问资源的所有请求,因此,将CharacterFilter映射信息中元素拦截的路径设置为“/*”
启动项目,测试结果
Listener监听器——Servlet事件监听器概述
创建一个cn.itcast.chapter08.listener包,在该包中编写一个MyListener类,这个类实现了ServletContextL istener. HttpSessionListener 和ServletRequestListener 3个监听器接口,并实现了这些接口中的所有方法
package cn.itcast.chapter08.listener;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyListener implements
ServletContextListener, HttpSessionListener,ServletRequestListener {
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContext对象被创建了");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContext对象被销毁了");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被创建了");
}
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被销毁了");
}
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被创建了");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被销毁了");
}
}
添加监听器类信息
在chapter08项目下的web.xml文件中,添加Mylistener 事件监听器信息
创建测试页面
为了查看HttpSessionL istener和ServletRequestL istener监听器的运行效果,在chapter8项目的WebContent目录中编写一个简单的页面文件myjsp.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>this is MyJsp.jsp page</title>
</head>
<body>
测试监听对象的请求
<br>
<a href="destroy.jsp">销毁session</a>
</body>
</html>
设置监听超时信息
为了尽快地查看到HttpSession对象销毁的过程,可以在chapter08应用的web. xm|文件中设置session的超时时间为2min
在上述配置中, 标签指定的超时必须为一个整数。如果这个整数为0或负整数,则session永远不会超时;如果这个数是正整数,则项目中的session将在指定分钟后超时。
使用监听器监听域对象的属性变更
编写一个testattribute.jsp页面,以观察各个域对象属性事件监听器的作用
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h3>这是一个测试对象属性信息监听器的页面</h3>
<%
getServletContext().setAttribute("username", "itcast");
getServletContext().setAttribute("username", "itheima");
getServletContext().removeAttribute("username");
session.setAttribute("username", "itcast");
session.setAttribute("username", "itheima");
session.removeAttribute("username");
request.setAttribute("username", "itcast");
request.setAttribute("username", "itheima");
request.removeAttribute("username");
%>
</body>
</html>
创建监听器
在cn.itcast.chaptero8.listener包中,编写一个名称为Myttibutelitere的监听器类,该类实现了SrleCentributel ster、HtStpstibuteliteneree 和SerletRequestributeListener接口,并实现这些接口中的所有方法
package cn.itcast.chapter08.listener;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyAttributeListener implements ServletContextAttributeListener,
HttpSessionAttributeListener, ServletRequestAttributeListener {
public void attributeAdded(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext添加属性:" + name + "="
+ sae.getServletContext().getAttribute(name));
}
public void attributeRemoved(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext移除属性: " + name);
}
public void attributeReplaced(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext替换属性:" + name + "="
+ sae.getServletContext().getAttribute(name));
}
public void attributeAdded(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession添加属性:" + name + "="
+ hbe.getSession().getAttribute(name));
}
public void attributeRemoved(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession移除属性: " + name);
}
public void attributeReplaced(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession替换属性:" + name + "="
+ hbe.getSession().getAttribute(name));
}
public void attributeAdded(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest添加属性:" + name + "="
+ sra.getServletRequest().getAttribute(name));
}
public void attributeRemoved(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest移除属性: " + name);
}
public void attributeReplaced(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest替换属性:" + name + "="
+ sra.getServletRequest().getAttribute(name));
}
}
添加监听信息
在web.xml文件中,添加MyttributeListener事件监听器信息
启动项目,测试结果