一、写一个过滤器类
二、在web.xml中配置该过滤器的使用,加入下面配置代码:
三、写JSP和Servlet进行测试
JSP:
Servlet:
输出页面JSP:
package com.test.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 CharacterEncodingFilter implements Filter {
protected FilterConfig filterConfig = null;
protected String encoding = "";
public void destroy() {
filterConfig = null;
encoding = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if (encoding != null) {
//设置request和response的编程格式,注意两个都要设,若没设response的
//charset,则在输出页面会显示乱码。
request.setCharacterEncoding(this.encoding);
response.setContentType("text/html;charset=utf-8");
}
//继续执行下一个过滤器
filterChain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
}
二、在web.xml中配置该过滤器的使用,加入下面配置代码:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.test.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、写JSP和Servlet进行测试
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<!-- 设置网页不缓存 -->
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
<title>filterTest</title>
</head>
<body>
<form action="servlet/LoginServlet" method="post" >
username:<input type="text" id="username" name="username"/><br>
password:<input type="password" id="password" name="password"/><br>
<input type="button" value="提交" οnclick="this.form.submit()" />
</form>
</body>
</html>
Servlet:
package com.test.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;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//页面转发,留意这里并没有对request和response的编码格式进行设置,如果没有
//编码过滤器,则会在跳到的页面中无法显示中文
this.getServletContext().getRequestDispatcher("/resultLogin.jsp").forward(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
输出页面JSP:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<head>
<title>My JSP 'resultLogin.jsp' starting page</title>
</head>
<body>
username::<%=request.getParameter("username") %><br>
password::<%=request.getParameter("password") %>
</body>
</html>