日期:2014-05-16  浏览次数:20304 次

jsp之间中文传值的问题,中文URL传值时的处理
String test="中文测试";
String temp=URLEncoder.encode(test,"GBK");   //编码
System.out.println(temp);
  
String test2=URLDecoder.decode(temp,"GBK"); //解码
System.out.println(test2);

String h4="中文测试";
String h5=URLEncoder.encode(h4,"utf-8");
System.out.println(h5); 
String h6=URLDecoder.decode(h5,"utf-8");
System.out.println(h6);


以上代码在java类中测试没问题。但在jsp之间传值却行不通,jsp之间只能通过另外一种方式,
例如两个jsp  : A1.jsp,A2.jsp
(1)A1.jsp 默认编码为 utf-8,即<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> ,
例如:A1.jsp 调用javascript (或response.sendRedirect()跳转)向A2.jsp传值,

window.open(A2.jsp?name="中文测试","newwindows","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=no");

(2)A2.jsp  接收代码应该为:
String  name=new String(request.getParameter("name").toString().trim().getBytes("ISO8859_1"),"utf-8");

同理如果A1.jsp 默认编码为 gbk ,即<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> 。则A2.jsp接收代码应该为 String  name=new String(request.getParameter("name").toString().trim().getBytes("ISO8859_1"),"gbk");