String test="中文測(cè)試";
String temp=URLEncoder.encode(test,"GBK"); //編碼
System.out.println(temp);
String test2=URLDecoder.decode(temp,"GBK"); //解碼
System.out.println(test2);
String h4="中文測(cè)試";
String h5=URLEncoder.encode(h4,"utf-8");
System.out.println(h5);
String h6=URLDecoder.decode(h5,"utf-8");
System.out.println(h6);
以上代碼在java類(lèi)中測(cè)試沒(méi)問(wèn)題。但在jsp之間傳值卻行不通,jsp之間只能通過(guò)另外一種方式,
例如兩個(gè)jsp : A1.jsp,A2.jsp
(1)A1.jsp 默認(rèn)編碼為 utf-8,即<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> ,
例如:A1.jsp 調(diào)用javascript (或response.sendRedirect()跳轉(zhuǎn))向A2.jsp傳值,
window.open(A2.jsp?name="中文測(cè)試","newwindows","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=no");
(2)A2.jsp 接收代碼應(yīng)該為:
String name=new String(request.getParameter("name").toString().trim().getBytes("ISO8859_1"),"utf-8");
同理如果A1.jsp 默認(rèn)編碼為 gbk ,即<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> 。則A2.jsp接收代碼應(yīng)該為 String name=new String(request.getParameter("name").toString().trim().getBytes("ISO8859_1"),"gbk");