Javaweb 十

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:8080/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>&nbsp;&nbsp;
        <input type="text" name="username" />${errerMsg }2020080605041</td>
	</tr>
	<tr>
		<td height="30" align="center">密   &nbsp; 码:</td>
		<td>&nbsp;&nbsp;
          <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="登录" />
              &nbsp;&nbsp;&nbsp;&nbsp;
			<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值