学习目标
一、FilterConfig接口
二。Listener监听器
学习内容
一、FilterConfig接口
为了获取Filter程序在web.xml文件中的配置信息,Servlet API提供了一个FilterConfig接口,该接口封装了Filter程序在web.xml中的所有注册信息,并且提供了一系列获取这些配置信息的方法 。
通过一个案例来演示FiterConfig接口的作用。
( 1 )在chapter08项目的cn.itcast.chapter08.flter 包中创建过滤器MyFiter03,使用该过滤器来获取web.xml中设置的参数,如下图所示。
例: MyFilter03.java
package cn.itcast.chapter08.filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
public class myFilter3 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.printin("encoding初始化费数的值为:"+characterEncoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
2)在web.xm文件中配置过滤器信息。由于Filter链中各个Filter的拦载原序与它价在web.xml文件中元素的映射顺序一致,因此,为了防止其他Ffler影响MyFiter03的拦截效果,这里将MyFiter03映射信息配置在web.xml文件最前端。
重新启动Tomcat 服务器,在浏览器地址栏中输入http://ocalhost:9999/chapter08/ MyServlet"访问MyServlet,控制台窗口中显示的结果如下图所示。
使用Filter实现用户自动登录
2.步骤实现
(1)编写User类
在chapter08项目中创建citathapter08. entity包,在该包中编写User类,该类用于封装用户的信息,如下图所示。
例 User.java
package cn.itcast.chapter08.filter;
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;
}
}
(2)实现登录页面和首页
①在chapter08项目的WebContent根目录中,编写login.jsp 页面,该页面用于创建一个用户登录的表单,这个表单需要填写用户名和密码,以及用户自动登录的时间,如文件8-10所示。
例: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">用户名:YM</td>
<td>
<input type="text" name="username" />${
errerMsg }2020080605041</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>
②在chapter08项目的WebContent根目录中,编写indexjsp页面,该页面用于显示用户的登录信息。如果没有用户登录,在index jsp页面中就显示一个用户登录的超链接。 如果用户E经登录,在index.jsp页面中显示登录的用户名,以及-个注销的超链接,如文下图所示
例:index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"
%>
<%@ page isELIgnored=“false” %>
<%@ taglib prefix="c" uri="http://localhost:9999/chapter08/WEB-INF/login.jsp"%>
<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.