Rumah  >  Artikel  >  hujung hadapan web  >  js、URL传递含有中文参数时的乱码问题解决

js、URL传递含有中文参数时的乱码问题解决

巴扎黑
巴扎黑asal
2016-12-20 11:46:511323semak imbas

1、使用代码完成字符集修改 

方法(一): 
html页面: 

function testOne() { 

   var url =  "testOne_test.do?expr="+你好; 

   location  = encodeURI(url); 



后台java代码: 

String expr = new String(request.getParameter("expr").getBytes("ISO-8859-1"),"UTF-8"); 

方法(二): 

html页面: 

function testOne() { 
   var url =  "testOne_test.do?expr="+你好; 
   location = encodeURI(encodeURI(url)); 


后台java代码: 
String expr = java.net.URLDecoder.decode(lrequest.getParameter("expr") , "UTF-8"); 
2、修改tomcat中的配置参数 

在tomcat下面找到server.xml 
 
根据需要修改为UTF-8等字符集。 
3、在web工程中添加spring.jar,使用spring的CharacterEncodingFilter 

view plaincopy to clipboardprint? 

      
     encoding  
     org.springframework.web.filter.CharacterEncodingFilter  
       
      encoding  
      UTF-8  
    
  
   
  
      
     encoding  
     /*  
   
  



org.springframework.web.filter.CharacterEncodingFilter 中的转码部分: 

view plaincopy to clipboardprint? 

    protected void doFilterInternal(  
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
      throws ServletException, IOException {  
      
     if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {  
      request.setCharacterEncoding(this.encoding);  
      if (this.forceEncoding && responseSetCharacterEncodingAvailable) {  
       response.setCharacterEncoding(this.encoding);  
      }  
     }  
     filterChain.doFilter(request, response);  
    } 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn