Servlet过滤器
一、实验目的:
1. 了解过滤器的作用;
2. 掌握过滤器的开发与部署的步骤;
3. 了解过滤器链。
二、实验环境:
MyEclipse10+Tomcat 7.0+Java EE 6.0
三、实验内容:
编写一个过滤器改变请求编码
【步骤 1】编写一个 loginform.html 文件,代码如下:
<!DOCTYPE html>
<html>
<head>
<title>使用过滤器改变请求编码</title>
<meta http-equiv="Content-Type" content="text/html;charset=GB2312">
</head>
<body>
<div align="center">
<h2>请输入用户名和口令</h2>
<form action="servlet/CheckParamServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>口令:</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="ok" value="提交">
<input name="cancel" type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
【步骤 2】编写处理请求参数的 Servlet,新建CheckParamServlet.java文件,包名为servlet,代码如下:
//CheckParamServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckParamServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public CheckParamServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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>");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
下面通过编写一个过滤器改变请求编码。
【步骤 3】新建EnCodingFilter.java文件,包名为filter,过滤器代码如下:
//EnCodingFilter.java
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;
public class EnCodingFilter implements Filter {
protected String encoding=null;
protected FilterConfig config;
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
//得到在web.xml中配置的编码
this.encoding = filterConfig.getInitParameter("Encoding");
}
public void doFilter (ServletRequest request,ServletResponse response,FilterChain chain)
throws IOException,ServletException {
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;
}
public void destroy() {
}
}
【步骤 4】修改 web.xml 文件,加入下面代码:
<servlet>
<servlet-name>CheckParamServlet</servlet-name>
<servlet-class>servlet.CheckParamServlet</servlet-class>
</servlet>
<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>
<servlet-mapping>
<servlet-name>CheckParamServlet</servlet-name>
<url-pattern>/servlet/CheckParamServlet</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>EnCodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
运行结果:
点击提交
运行成功,不会出现中文乱码