jsp参数传递乱码

来源:互联网 发布:网络语小主是什么意思 编辑:程序博客网 时间:2024/06/05 15:12

方案一代码

  1. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %>    
  2. Username:<%=str %>   
  3. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %> Username:<%=str %>  

既然request.getParameter("username")默认情况下返回的字符串是用"ISO-8859-1"解出来的,那就先把这个不可辨认的字符串再用"ISO-8859-1"来打散,也就是:request.getParameter("username").getBytes("ISO-8859-1")。最后再用跟你的页面的charset一致的字符集来重组这个字符串:new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8")。这样就能见到它的庐山真面目了。

方案一是一种比较万能的方法,不管是post还是get都适用,但可以看出它的缺点是:对于每个可能出现汉字的参数都要显示的做这么一段处理。一个两个还行,要是很多的话,那就应该考虑一下是不是可以选用下一种方案。

方案二代码

  1. <%request.setCharacterEncoding("UTF-8"); %>   
  2. <%request.setCharacterEncoding("UTF-8"); %>  

方案二是在页面的最开始或者是在该页面中使用的第一个request.getParameter("")方法之前加上上述一段代码,它的作用是用作为参数传入的编码集去覆盖request对象中的默认的"ISO-8859-1"编码集。这样request.getParameter("")方法就会用新的编码集去解码,因为"UTF-8"支持中文,所以作为参数传过来的“世界杯”三个汉字就能正确的接收到了。但关于request.setCharacterEncoding("")方法,API文档中有如下的说明:

Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effectb.

所以方案二只对post方式提交的请求有效,因为参数都在request的body区。而对get方式提交的请求则是无效的,这时你会发现同样的做法但显示的还是乱码。所以你的请求要是是以get方式提交的话,那你还是乖乖的选用方案一吧!

从上面的叙述可以知道,方案二需要在每个页面的前头加上<%request.setCharacterEncoding("UTF-8"); %>这段代码,这样做是不是也挺累的,所以我们想到了使用过滤器来帮助我们做这件事儿,那就清爽、简单多了。

Encodingfilter代码

  1. public class EncodingFilter implements Filter {    
  2.         
  3.     private String charset;    
  4.     @Override    
  5.     public void destroy() {    
  6.         // TODO Auto-generated method stub    
  7.     }    
  8.    
  9.     @Override    
  10.     public void doFilter(ServletRequest request, ServletResponse response,    
  11.             FilterChain chain) throws IOException, ServletException {    
  12.         //用init方法取得的charset覆盖被拦截下来的request对象的charset    
  13.         request.setCharacterEncoding(this.charset);    
  14.         //将请求移交给下一下过滤器,如果还有的情况下。    
  15.         chain.doFilter(request, response);    
  16.     }    
  17.    
  18.     @Override    
  19.     public void init(FilterConfig config) throws ServletException {    
  20.         //从web.xml中的filter的配制信息中取得字符集    
  21.         this.charset = config.getInitParameter("charset");    
  22.     }    
  23. }   
  24. public class EncodingFilter implements Filter { private String charset; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //用init方法取得的charset覆盖被拦截下来的request对象的charset request.setCharacterEncoding(this.charset); //将请求移交给下一下过滤器,如果还有的情况下。 chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { //从web.xml中的filter的配制信息中取得字符集 this.charset = config.getInitParameter("charset"); } }  

要想这个过滤器生效,还得到web.xml里加入下面的配制信息。

Web.xml代码

  1. <filter>    
  2.    <filter-name>EncodingFilter</filter-name>    
  3.    <filter-class>cn.eric.encodingtest.filter.EncodingFilter</filter-class>    
  4.    <init-param>    
  5.        <param-name>charset</param-name>    
  6.        <param-value>UTF-8</param-value>    
  7.    </init-param>    
  8. </filter>    
  9. <filter-mapping>    
  10.    <filter-name>EncodingFilter</filter-name>    
  11.    <url-pattern>/*</url-pattern>    
  12. </filter-mapping>   
  13. <filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.eric.encodingtest.filter.EncodingFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>  

2、直接使用URL后接参数的形式(超级链接)。

有些时候可能会遇到通过一个超级链接来把参数传到下一个页面,而刚好这个参数的值有可能会出现中文的情况。就像下面这样:

  1. <a href="./jstlresult.jsp?content=世界杯">Go South Africa 

跟form提交有些不同的是:当你点击这个超级链接后在浏览器的地址栏里看到的是http://localhost:8080/TomcatJndiTest/jstlresult.jsp?content=世界杯,而不是http://localhost:8080/TomcatJndiTest/jstlresult.jsp?content=%E4%B8%96%E7%95%8C%E6%9D%AF

这里浏览器并没有帮我们把这个转化工作搞定,所以这里要自己动手,丰衣足食了。做法如下:

  1. <a href="./jstlresult.jsp?content=<%=java.net.URLEncoder.encode("世界杯","utf-8") %>">Go South Africa 

这样的话在第二个页面就能使用

  1. <%String str = new String(request.getParameter("content").getBytes("ISO-8859-1"),"utf-8"); %> 

的方法来正确的得到这个参数值了。

总结一下:

1、post提交的方式:使用过滤器,将到达页面前的request对象中的字符编码设定成跟你页面统一的编码。

2、get提交的方式:<%String str = new String(request.getParameter("content").getBytes("ISO-8859-1"),"utf-8"); %>这样的字符串重组的方法。

3、超级链接方式:先将链接url中的汉字用java.net.URLEncoder.encode("paramValue","charset")方法处理一下,下面的做法参照2