Servlet高级

什么是Filter
Filter被称作过滤器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前后实现一些特殊功能。这就好比现实中的污水净化设备,它可以看一个过滤器,专门用于过滤污水杂质。
在这里插入图片描述
Filter接口中的方法

方法声明功能描述
init(FilterConfig filterConfig)init()方法用来初始化过滤器,开发人员可以在init()方法中完成与构造方法类似的初始化功能。如果初始化代码要使用到FilterConfig对象,那么,这些初始化代码就只能在Filter的init()方法中编写,而不能在构造方法编写
doFilter(ServletRequest request,ServletResponse response,FilterChain chain)doFilter()方法有多个参数,其中,参数request 和 response"为 Web服务器或Filter 链中的上一个Filter 传递过来的请求和响应对象;参数chain 代表当前 Filter链的对象,在当前 Filter对象中的doFilter()方法内部需要调用FilterChain 对象的doFilter)方法,才能把请求交付给Filter 链中的下一个 Filter 或者目标程序去处理
destroy()destroy()方法在Web服务器卸载Filter对象之前被调用,该方法用于释放被Filter对象打开的资源,例如关闭数据库和IO流

实现第一个Filter程序

(1)创建一个名为MyServlet的Servlet类

package chapter11;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().write("Hello MyServlet ");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

(2)在web.xml文件中对Servlet进行如下配置。

<servlet>
   <servlet-name>MyServlet</servlet-name>
   <servlet-class>chapter11.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>MyServlet</servlet-name>
   <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

Filter链
在chapter08项目的cn.itcast.chapter08.flter 包中创建过滤器MyFiter03,使用该过滤器来获取web.xml中设置的参数

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() {
}
}

使用Filter实现用户自动登录
实现步骤
编写Use类
在chapter08项目中创建包,在该包中编写User类

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;
	}
}

实现登录页面和首页
编写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>

在chapter8项目的WebContent根目录中,编写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.user.username }!
           <a href="${pageContext.request.contextPath }/LogoutServlet">注销</a>
		</c:otherwise>
	</c:choose>
	<hr />
</body>
</html>

创建Servlet类
编写LoginServlet类
在chapter08项目的包中,编写LoginServlet类,该类用于处理用户的登录请求。如果输入的用户名和密码正确,则发送一个用户自动登录的Cookie,并跳转到首页;否则会提示输入的用户名或密码错误,并跳转至登录页面login.jsp让用户重新登录。

package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import cn.itcast.chapter08.filter.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 ("yanmin".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);
	}
}

编写LogoutServlet类
在chape08项目的r包中,编写Lgouseve类,该类用于注锅用户登录的信息。在这个程序中首先会将Sssn会话中保存的User对象删除,然后将自动登录的Cookie删除,最后跳转到index.jsp。

package cn.itcast.chapter08.filter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginOutServlet 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);
	}
}

创建过滤器
在chapter08项目的包中,编写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.filter.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 ("yanmin".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及AutoLoginFilter过滤器信息。由于要拦截用户访问资源的所有请求,因此,将过滤器<filter -mapping>元素拦截的路径设置为“/*”,
运行项目,查看结果
1.访问login.jsp页面
2.实现用户登录
3.注销用户

监听域对象的属性变更

[任务目标]
通过所学监听器知识,读者应学会使用监听器监听域对象的属性变更。

1.创建测试页面
在chapter08项目的WebContext根目录中,编写一个testattribute.jsp页面,以观察各个域对象属性事件监听器的作用,具体代码如下图所示

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
   测试域对象属性信息监听器的页面</br>
   <% 
   application.setAttribute("username","itcast"); 
   application.setAttribute("username","itheima"); 
   application.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>

2.创建监听器
在chapter08项目的cn.itcast.chaptero8.listener包中,编写一个名称为Myttibutelitere的监听器类,该类实现了SrleCentributel ster、HtStpstibuteliteneree 和SerletRequestributeListener接口,并实现这些接口中的所有方法,如下图所示。

package cn.itcast.chapter08.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

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));
}
}

3.添加监听信息
在web.xml文件中,添加MyttributeListener事件监听器信息
4.启动项目,测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值