一聚教程网:一个值得你收藏的教程网站

热门教程

Servlet的中文乱码常用解决方法

时间:2022-06-29 02:39:17 编辑:袖梨 来源:一聚教程网

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"
 
 完整的应如下:
    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文件的标签之间添加如下代码
 
   encoding
   GB2312
 

6.使用Servlet规范中的过虑器指定编码
 <1>.新创建一个servlet时,Interfaces接口:继承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中的相关内容的改为
       并修改中的/* ; “/*”表示执行任何文件
 
 简介过滤器:
 A.每个过滤器都会在 web.xml中有单独的配置:
 
  过滤器的别名
  过滤器的物理地址,带有完整的包路径的
 

 
  过滤器的别名
  过滤器访问的路径
 

 B.当由于某种原因想要删除servlet类时,删除后在web.xml中还会保留删除的servlet类的记录,
 所以必须手动在web.xml中删除一下已删除的servlet类的信息
 
 过滤器的在web.xml中的典型配置和主要代码如下:

 web.xml:
 
  CharacterEncodingFilter
  net.vschool.web.CharacterEncodingFilter
 
   encoding
   GBK
 

 

 
  CharacterEncodingFilter
  /*
 

 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中



  Empty web.xml file for Web Application
 
  Filter1
  project1.Filter1
 

 
  Filter1
  *.*
 

 
  35
 

 
  html
  text/html
 

 
  txt
  text/plain
 

热门栏目