index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Handle mess encoding</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="serviceServlet">Hello ServletServiceMethod</a><br/>
<!-- 转码方式处理中文乱码 -->
<a href="serviceServlet?name=小狗">servletService处理get数据</a><br/>
<!-- 编码解码方式处理中文乱码 -->
<a href="serviceServlet?name=
<%=URLEncoder.encode("小狗","utf-8")%>&method=encode">servletService处理get数据</a><br/>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HandleEncodingServlet.java
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetEncodingServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
//设置返回的编码为utf-8
req.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");
PrintWriter out = res.getWriter();
//因为http://localhost:8080/Test/serviceServlet?name=小狗 以这样的超链接方式get提交的话
//url中的中文会以系统默认的编码方式编码,就是ISO-8859-1,所以这里要对该字符串转码
//name = new String(name.getBytes("ISO-8859-1"),"utf-8")
out.print("<meta charset='utf-8'/>");
String name = req.getParameter("name");
String method = req.getParameter("method");
if(name!=null && method==null){
System.out.println(name);
name = new String(name.getBytes("ISO-8859-1"),"utf-8");
out.print("<span style='color:red;'>new String转码处理得到的get数据是:name="+ name + "</span>");
}
if(method!=null && method.equals("encode")){
name = new String(name.getBytes("ISO-8859-1"),"utf-8");
//name = URLDecoder.decode(name,"utf-8");
System.out.println(name);
out.print("<span style='color:red;'>URLEncoder和URLDecoder处理得到的get数据是:name="+ name + "</span>");
}
out.print("<h3>servlet service method</h3>");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总结:因为在get方式提交的数据情况下,可以推出j2ee底层总是将jsp中提交的数据以默认的编码ISO-8859-1进行了一次编码。
所以如果我们没有这个觉悟的话,尽管设置了
req.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");也还是 一样会得到乱码。因此要得到最终的好码,我们一定要转一次ISO-8859-1到utf-8码上,这样就可以得到数据的好码。然后 显示的话,就通过设置req,res的编码就行。
一般的,我们用的都是utf-8这样既支持中文又支持英文的国际标准编码方式。