1.设定tomcat的URI Encode为gb2312http://localhost:8080/admin密码和用户名在你安装的时候设定的,如果忘记的话,去改%CATALINA_HOME%\conf\tomcat-users.xml1.1 点击左边框架中Service菜单项左边的小圆,展开列表.
1.2 点击左边框架中connector(8080)菜单项,右边框架出现设置页。
1.3 将URI Encoding参数改为gb2312。
1.4 点击右边框架中右上角save按钮
1.5 点击顶部框架Commit Changes按钮。
2.jsp文件写法:
<%@page contentType="text/html;charset=gb2312"%>
<%
response.setContentType("text/html; charset=gb2312");
request.setCharacterEncoding("gb2312");
// do something
%>
<html>
<head>
<title>success</title>
<meta content="text/html; charset=gb2312" http-equiv=Content-Type>
</head>
<body>
<!--show something-->
</body>
</html>
3.如果从servlet中传中文参数:
response.sendRedirect(java.net.URLEncoder.encode("somepage.jsp?chs=中文", "gb2312");
1.2 点击左边框架中connector(8080)菜单项,右边框架出现设置页。
1.3 将URI Encoding参数改为gb2312。
1.4 点击右边框架中右上角save按钮
1.5 点击顶部框架Commit Changes按钮。
2.jsp文件写法:
<%@page contentType="text/html;charset=gb2312"%>
<%
response.setContentType("text/html; charset=gb2312");
request.setCharacterEncoding("gb2312");
// do something
%>
<html>
<head>
<title>success</title>
<meta content="text/html; charset=gb2312" http-equiv=Content-Type>
</head>
<body>
<!--show something-->
</body>
</html>
3.如果从servlet中传中文参数:
response.sendRedirect(java.net.URLEncoder.encode("somepage.jsp?chs=中文", "gb2312");
response.setContentType("text/html; charset=gb2312");
request.setCharacterEncoding("gb2312");
2. 还有一招编码过滤器,作用是省去到处写
response.setContentType("text/html; charset=gb2312");
request.setCharacterEncoding("gb2312"):但是...... 好像效果不好?(哎 sorry, 大家有空自己试试)2.1 类代码:
package 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;
import javax.servlet.UnavailableException;public class EncodingFilter implements Filter
{
protected String contentType = null;
protected String encoding = null;
public void destroy()
{
this.encoding = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if (request.getCharacterEncoding() == null)
{
if (contentType != null)
{
response.setContentType(contentType);
}
if (encoding != null)
{
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException
{
contentType = filterConfig.getInitParameter("contentType");
encoding = filterConfig.getInitParameter("encoding");
}
}2.2 需要在WEB-INF/web.xml文件中加上:
2.2.1 这段需要加在<context-param>标记之后,<servlet>标记之前。
2.2.2 如果有多个filter, 那么所有<filter>标记在一起,<filter-mapping>标记在一起。
2.2.3 xml解析器不认tab字符。( 2.2.2 , 2.2.3 其实是web.xml编写经验来的:))) ) <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
<init-param>
<param-name>contentType</param-name>
<param-value>text/html; charset=gb2312</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>