Struts1中文乱码问题解决

注意下面几点:

1、jsp页面中pageEncoding="utf-8":<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

2、创建过滤器类MyFilter1.java:

package com.whut.filters;

import java.io.IOException;
import java.io.PrintWriter;

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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyFilter1 extends HttpServlet implements Filter {

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		arg0.setCharacterEncoding("utf-8");//设置接收编码
		arg2.doFilter(arg0, arg1);//必需,不然会停在这
		arg1.setContentType("text/html;charset=utf-8");
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

3、在web.xml中配置:

<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.whut.filters.MyFilter1</filter-class>
</filter>

<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页