过滤器链实现中文转码和文字屏蔽功能(3)
</filter-mapping>
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>com.mars.servlet.MessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageServlet</servlet-name>
<url-pattern>/MessageServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>submit.jsp</welcome-file>
</welcome-file-list>
</web-app>
[java]
package com.mars.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class CharacterFilter implements Filter {
public void init(FilterConfig arg0) throws ServletException {
System.out.println("调用init");
}
public void destroy() {
System.out.println("调用destroy");
}
// 解决post方式的乱码问题
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
chain.doFilter(request, response);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>