桂 林 理 工 大 学
实 验 报 告
班级 网络17-1班 学号 3172052051329 姓名 曾德展 同组实验者
实验名称 Servlet过滤器 日期 2020年 5 月 1 日
一、实验目的:
- 了解过滤器的作用;
- 掌握过滤器的开发与部署的步骤;
- 了解过滤器链。
二、实验环境:
Eclipse && Tomcat7.0
三、实验原理
过滤器是 web 服务器上的组件,它们对客户和资源之间的请求和响应进行过滤。
过滤器的工作原理是:当servlet容器接收到对某个资源的请求,它要检查是否有过滤器
与之关联。如果有过滤器与该资源关联,servlet容器将把该请求发送给过滤器。在过滤器处
理完请求后,它将做下面3件事:
产生响应并将其返回给客户;
如果有过滤器链,它将把(修改过或没有修改过)请求传递给下一个过滤器;
将请求传递给不同的资源。
当请求返回到客户时,它是以相反的方向经过同一组过滤器返回。过滤器链中的每个过
滤器够可能修改响应。
过滤器 API 主要包括:Filter、FilterConfig 和 FilterChain 接口。
四、实验内容:
编写一个过滤器改变请求编码。
【步骤 1】编写一个 loginform.html 文件,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GB2312">
<title>使用过滤器改变请求编码</title>
</head>
<body>
<h2>请输入用户名和口令:</h2>
<form method="post" action="servlet/CheckParamServlet">
<table>
<tr>
<td>用户名:</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>口 令:</td>
<td><input name="pass" type="password"></td>
</tr>
<tr>
<td></td>
<td><input name="ok" type="submit" value="提交">
<input name="cancel" type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
【步骤 2】编写处理请求参数的 Servlet,代码如下:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 CheckParamServlet
*/
@WebServlet("/CheckParamServlet")
public class CheckParamServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckParamServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
String name = request.getParameter("name");
String pass = request.getParameter("pass");
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Param Test</title></head>");
out.println("<h3 align=center>你的用户名为:"+name+"</h3>");
out.println("<h3 align=center>你的口令为:"+pass+"</h3>");
out.println("</body></html>");
}
/**
* @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);
}
}
【步骤 3】在浏览器的地址栏中输入下面 URL:
http://localhost:8080/ helloapp/loginform.html
输入用户名和口令,如下图所示:
图 14.1 loginform.html 页面的运行结果
然后点击“提交”按钮,经 CheckParamServlet 处理后返回的结果如下图所示:
图 14.2 CheckParamServlet 程序的运行结果
从这里我们可以看到,从服务器返回的汉字成了乱码。原因是没有指定 request 的编
码。
下面通过编写一个过滤器改变请求编码。
【步骤 4】过滤器代码如下:
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class EncodingFilter
*/
@WebFilter("/*")
public class EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig config;
/**
* Default constructor.
*/
public EncodingFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
// pass the request along the filter chain
if (request.getCharacterEncoding() == null) {
// 得到指定的编码
String encode = getEncoding();
if (encode != null) {
//设置 request 的编码
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
}
chain.doFilter(request, response);
}
protected String getEncoding() {
return encoding;
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
this.config = fConfig;
// 得到在 web.xml 中配置的编码
this.encoding = fConfig.getInitParameter("Encoding");
}
}
【步骤 5】在 web.xml 文件中配置过滤器,加入下面代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ch03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
【步骤 6】重复第(4)步操作,结果如下:
图 14.3 EncodingFilter 程序的运行结果
1.试简述过滤器有哪些功能?
过滤器对客户和资源之间的请求和响应进行过滤。请求经过一个过滤器到达servlet,servlet产生响应再经过过滤器到达客户,这就在请求和响应到达目的地之前对它们进行监视。过滤器的存在对servlet和客户都是透明的。
2. 如何理解过滤器链。
客户与资源之间建立多个过滤器,从而形成过滤器链。在过滤器链中每个过滤器都是请求处理,然后将请求交给链中的下一个过滤器(如果它是链中的最后一个,将交给实际的资源。)类似的,在响应到达客户之前,每个过滤器以相反的顺序对响应处理。