<%@ page language="java" pageEncoding="GB2312"%> 与
<%@ page contentType="text/html; charset=GB2312" pageEncoding="GB2312" language="java" errorPage=""%>区别:
1.过滤器servlet
public class CharsetFilter
implements Filter
{
protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;
public CharsetFilter()
{
encoding = null;
filterConfig = null;
ignore = true;
}
public void destroy()
{
encoding = null;
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
if(ignore || request.getCharacterEncoding() == null)
{
String encoding = selectEncoding(request);
if(encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig)
throws ServletException
{
this.filterConfig = filterConfig;
encoding = filterConfig.getInitParameter("encoding");
System.out.println("11111111111:"+encoding);
String value = filterConfig.getInitParameter("ignore");
if(value == null)
ignore = true;
else
if(value.equalsIgnoreCase("true"))
ignore = true;
else
if(value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}
protected String selectEncoding(ServletRequest request)
{
return encoding;
}
}
2.在 web.xml 配置
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.easyjf.web.CharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
小结:
在使用<%@ page language="java" pageEncoding="GB2312"%> 显示的是乱码。
在使用<%@ page contentType="text/html; charset=GB2312" language="java" pageEncoding="GB2312"%>却是正常的。
1.jsp :
<%@ page contentType="text/html; charset=gb2312" language="java">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
2.web.xml中配置 gb2312