Servlet的中文乱码常用解决方法
Servlet的中文乱码常用解决方法
在servlet页面就出错了,打印出就是乱码,why?
public class toDetail extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
int id=Integer.parseInt(request.getParameter("ID"));
goodsBeanAction gba=new goodsBeanAction();
goodsBean gb=gba.getGoodsBean(id);
request.setAttribute("goodsInfo", gb);
request.getRequestDispatcher("showDetail.jsp教程").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
最佳答案
你的ID属性是不是可以输入中文的?如果是,则需要在接收参数之前设置字符编码,request.setCharacterEncoding("gbk");,还有一个response.setContentType("text/html;charset=gbk");这样就行了
具体如下:
1.JSP里CHARSET要为GB2312
contentType="text/html;charset=GB2312"
2.SERVERLET类里要有
request.setCharacterEncoding(gb2312);
3.字符集的重新格式化
java.net.URLEncoder java.net.URLDecoder 对应的encode 与decode进行编码解码。
在传输数据时候浏览器会自动对要表单传输的数据进行url编码,所使用的编码方式取决于当前网页显示时候使用的编码方式。
对于http请求消息的url地址后的参数,getparameter等方法进行自动url解码时采用的编码方式取决servlet引擎;tomcat默认用ISO8859-1进行解码。
重新格式化语句如下:
String str1 = new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");
4.TOMCAT的CONF文件夹里的SERVER.XML中大约92行左右.(如果你没改过)
找到connector区块,加入如下一行:
URIEncoding="GBK" 或 URIEncoding="GB2312" 或 URIEncoding="UTF-8"
完整的应如下:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups教程="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GB2312"
/>
5.Tomcat的conf文件夹下的web.xml文件的<servlet></servlet>标签之间添加如下代码
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
6.使用Servlet规范中的过虑器指定编码
<1>.新创建一个servlet时,Inte易做图ces接口:继承javax.servlet.Filter
options中只需选中:create Inherited Methods; create Constructors;
建好后在其doFilter()方法中:
arg0.setCharacterEncoding("gb2312"); //实现请求乱码的处理
arg1.setCharacterEncoding("gb2312"); //实现响应乱码的处理
arg2.doFilter(arg0,arg1); //继续执行其他过滤器 或 jsp、servlet
<2>.再在web.xml中把过滤器servlet中的相关内容的<servlet></servlet>改为<filter></filter>
并修改<filter-mapping></filter-mapping>中的<url-patterm>/*</url-patterm> ; “/*”表示执行任何文件
简介过滤器:
A.每个过滤器都会在 web.xml中有单独的配置:
<filter>
<filter-name>过滤器的别名</filtr-name>
<filter-class>过滤器的物理地址,带有完整的包路径的</filter-class>
</filter>
<filter-mapping>
<filter-name>过滤器的别名</filter-name>
<filter-patterm>过滤器访问的路径</filter-patterm>
</filter-mapping>
B.当由于某种原因想要删除servlet类时,删除后在web.xml中还会保留删除的servlet类的记录,
所以必须手动在web.xml中删除一下已删除的servlet类的信息
过滤器的在web.xml中的典型配置和主要代码如下:
web.xml:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>net.vschool.web.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
CharacterEncodingFilter.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;
public class CharacterEncodingFilter implements Filter
{
protected String encoding = null;
public void init(FilterConfig filterConfig) throws ServletException
{
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
}
}7.解决数据库教程的乱码
在jdbc.url中明确字符集 (不适用于sqlserver)
在连接数据库时:con=DriverManager.getConnection(url,"sa","");
url="jdbc:协议:子协议://IP:端口; 库 ? useUnicode=true & characterEncoding=gb2312";
过滤器不就得了
public class Filter1 implements Filter {
private FilterConfig _filterConfig = null;public void init(FilterConfig filterConfig) throws ServletException {
_filterConfig = filterConfig;
}public void destroy() {
_filterConfig = null;
}public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {chain.doFilter(request, response);
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
}
}
然后在web.xml中<?xml version = '1.0' encoding = 'GBK'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<description>Empty web.xml file for Web Application</description>
<filter>
<filter-name>Filter1</filter-name>
<filter-class>project1.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>35</session-timeout>
</session-config>
<mime-mapping>
<extension>html</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
</web-app>
补充:Jsp教程,Jsp/Servlet开发工具