2012/8/4解决JSP显示中文乱码

1.在action中对要传递的参数进行URL编码,如:this.username=URLEncoder.encode("老猪","UTF-8");

2.在视图中 个用UFT-8的方式new一个String出来,然后用URLDecoder.decode方法对这个String进行解码,如:<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8")) %>  ,注意在JSP文件中导入java.net.URLDecoder包、如<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="UTF-8"%>

附:如果在JSP中要写JAVA代码就用<%%>括起来。

 

刚才我试了下,<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %>这样也行。

 

posted @ 2012-08-04 18:08  呼啸而过  阅读(240)  评论(0编辑  收藏  举报