jsp乱码问题 URL传值乱码问题

来源:互联网 发布:申友留学 知乎 编辑:程序博客网 时间:2024/05/21 19:47

 jsp乱码问题解决

1、一般的jsp页面显示乱码
   <%@ page contentType="text/html; charset=UTF-8"%>

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

2、表单提交中文时出现乱码

如果jsp提交英文字符能正确显示,而提交中文时就会出现乱码。
原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF- 8和GB2312编码方式表示字符时不一样,
这样就出现了不能识别字符

解决:再jsp页面设定 <%request.seCharacterEncoding("UTF-8");%>对请求进行统一编码
为了避免每页都要写request.setCharacterEncoding("UTF-8"),建议使用过滤器对所有jsp
进行编码处理,

过滤器——.java文件:
   public class SetCharacterEncodingFilter implements Filter{

 /*过滤器,解决中文乱码问题*/
  protected String encoding = "UTF-8";
  protected FilterConfig filterConfig = null;
  protected boolean ignore = true;
 public void destroy() {
  // TODO Auto-generated method stub
     this.encoding = null;
           this.filterConfig = null;
  
 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  // TODO Auto-generated method stub
   try {
             // Conditionally select and set the character encoding to be used
             if (ignore || (request.getCharacterEncoding() == null)) {
                 String encoding = selectEncoding(request);
                 if (encoding != null)
                     request.setCharacterEncoding(encoding);
             }
    
             // Pass control on to the next filter
             chain.doFilter(request, response);
             } catch (IOException e) {
                
                 throw e;
             } catch (ServletException e) {
                
                 throw e;
             }

  
 }

 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stub
       this.filterConfig = filterConfig;
          this.encoding = filterConfig.getInitParameter("encoding");
          String value = filterConfig.getInitParameter("ignore");
          if (value == null)
              this.ignore = true;
          else if (value.equalsIgnoreCase("true"))
              this.ignore = true;
          else if (value.equalsIgnoreCase("yes"))
              this.ignore = true;
          else
              this.ignore = false;
  
 }
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
 
}
过滤器——web.xml文件中的配置:
  <filter>
   <filter-name>Set Character Encoding</filter-name>
   <filter-class>
    net.better_best.www.utils.SetCharacterEncodingFilter
   </filter-class>              
   <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>Set Character Encoding</filter-name>
   <servlet-name>action</servlet-name>
  </filter-mapping>
 

3、数据库连接出现乱码
只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上
String Url="jdbc:mysql://localhost/digitgulf?user=root&password=root&useUnicode=true&characterEncoding=UTF-8";
  并在页面中使用如下代码:
  response.setContentType("text/html;charset=UTF-8");
  request.setCharacterEncoding("UTF-8");

 

4、URL传值乱码问题解决,这个也是最麻烦的事情啦

如果是Tomcat的话,需要在server.xml里面的connector里面务必设置如下参数:
<Connector port="8080"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

debug="0" connectionTimeout="20000" useBodyEncodingForURI="true"

disableUploadTimeout="true" URIEncoding=”UTF-8”/>

设定URIEncoding=”UTF-8”  useBodyEncodingForURI="true"

但是这样还是会出问题的,比如URL传递参数是奇数或者偶数时...
       
        String techName= request.getParameter("selTechnicalArticlesTitle").trim();
        //URL 传递值时用Encoder进行编码 
         java.net.URLEncoder.encode(techName, "UTF-8")
        //对页面传来的字符进行解码:
         java.net.URLDecoder.decode(techName,"UTF-8");

        //多个参数传递

techType.do?techid=1&techName=中国&name=汉字

techid,techName,name是参数名

与你在Action当中用request.getParameter("techName") 一定要相同;

你传的时候名称与你页面查询条件输入框的名称一定要对应,因为Action当中取的是页面输入框的名称;

明白?


 

 

 

 

原创粉丝点击