1. 创建字符编码过滤器对象,源码如下:
package com.tzy.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 CharacterFilter implements Filter {
String encoding = null;
@Override
public void destroy() {
encoding = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (encoding != null) {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding);
}
/*
* 在过滤器对象的doFilter()方法中,业务逻辑处理完成之后,
* 需要通过FilterChain对象的doFilter()方法将请求传递到下一过滤器或目标资源,否则将出现错误。
*/
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
}
}
2. 在Web.xml文件中对过滤器进行配置,配置如下:
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>com.tzy.filter.CharacterFilter</filter-class
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在Web.xml文件中,配置过滤器,其过滤器的URL映射可以使用正则表达式进行配置,如实例中使用“/*”来匹配所有请求。
3. 我们下面写实例进行测试:
3.1 编写html文件,代码如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>注册</title>
</head>
<body>
<div align="center">
<form action="servlet/RegisterServlet" method="post">
<table>
<thead>
<tr>
<td width="300">个人信息</td>
</tr>
</thead>
<tbody>
<tr>
<td width="100">姓名:</td>
<td width="200"><input type="text" name="name"/></td>
</tr>
<tr>
<td width="100">性别:</td>
<td width="200">
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女
</td>
</tr>
<tr>
<td width="100">联系方式:</td>
<td width="200"><input type="text" name="email"/></td>
</tr>
<tr>
<td width="100">家庭住址:</td>
<td width="200"><input type="text" name="address"/></td>
</tr>
<tr>
<td width="300">
<input type="submit" value="提交"/>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
3.2 处理请求的Servlet代码如下:
package com.tzy.filter;
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 RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String address = request.getParameter("address");
out.print("<html>");
out.print("<h2>个人信息</h2><hr/>");
out.print("姓名:" + name + "<br/>");
out.print("性别:" + gender + "<br/>");
out.print("联系方式:" + email + "<br/>");
out.print("家庭住址:" + address + "<br/>");
out.print("</html>");
}
}
3.3 不要忘了在Web.xml中对Servlet进行配置:
<servlet>
<description>处理注册信息</description>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.tzy.filter.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/servlet/RegisterServlet</url-pattern>
</servlet-mapping>
OK,大功告成!