本人不相信人品,请大家也一样:
如下是我知道的jsp页面的中文乱码解决方法:
1、在response中写<meta http-equiv="content-style-type" content="text/html;charset=GB2312">
2、使用new String(字符变量.getBytes("iso-8859-1"),"gb2312")
3、在jsp中处理乱码问题如下解决
String 变量名 = "你好";
lPara = java.net.URLEncoder.encode(new String(变量名.getBytes("GBK"),"iso8859-1"));
<a href="yourServlet?Para=<%=lPara%>">Test</a>
...
yourServlet:
String lPara=request.getParameter("Para");
4、在得到变量的值之前(request.getParameter("界面上的控件名字")),加上如下代码
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
respose.setCharacterEncoding("gb2312");
5、确保在servlet中中文处理代码(方法4中的3行代码)在PrintWriter对象之前
PrintWriter out = response.getWriter();
6、如果1~5的操作都操作了还是乱码问题则:
注意在页面上单吉右键 --编码--给成中文(就OK)
7、要是如上1~6操作还是不能够解决中文问题则:
换个服务器操作即便(换个tom猫在发布或者使用jboss发布)
8、仅仅提供参考
Servlet 中文乱码解决【使用过滤器】
my.EncodingFilter.java://创建类的名字
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 encoding = null;
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Select and set (if needed) the character encoding to be used
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
再在web.xml的<web-apps>中加入如下内容 //在web.xml中实现如下代码
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>my.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value></init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如上操作就可以解决乱码问题
注意:my.EncodingFilter是EncodingFilter这个类在我的工程中的位置
大家如果有其他方法,请回帖!