上面写了console的乱码问题,接下来写的是web中servlet中的问题,大楷我比较关心一点,因为遇到这个的情况多一些吧。直接开始吧。
2. jsp和servlet中的乱码问题
分析:
a. 其实在java文件的编译的情况和(一)中的情况是一样的,不过这里是由WEB容器去调用JVM而已,那么我们得知道一些默认的东西
比如特别重要的:(摘要)
如果Servlet 在运行的过程中,需要接受从客户端传来的字符如:表单输入的值和URL中传入的值,此时如果程序中没有设定接受参数时采用的编码格式,则WEB 容器会默认采用ISO-8859-1 编码格式来接受传入的值并在JVM 中转化为UNICODE 格式的保存在WEB 容器的内存中。Servlet 运行后生成输出,输出的字符串是UNICODE 格式的,紧接着,容器将Servlet 运行产生的UNICODE 格式的串(如html语法,用户输出的串等)直接发送到客户端浏览器上并输出给用户,如果此时指定了发送时输出的编码格式,则按指定的编码格式输出到浏览器上,如果没有指定,则默认按ISO-8859-1 编码发送到客户的浏览器上。
注意是ISO-8859-1就行了,tomcat 5.0之前采用是由用户设置的编码方式解析,tomcat 5.0过后有个参数(useBodyEncodingForURI)被默认了false,就使用了ISO-8859-1解析了,这儿是配置中的关键。
测试代码如下:
Servlet类(注释说明)
03 |
import java.io.IOException; |
04 |
import java.io.PrintWriter; |
06 |
import javax.servlet.ServletException; |
07 |
import javax.servlet.http.HttpServlet; |
08 |
import javax.servlet.http.HttpServletRequest; |
09 |
import javax.servlet.http.HttpServletResponse; |
11 |
public class Hello extends HttpServlet { |
13 |
private static final long serialVersionUID = 4878915372815719687L; |
19 |
public void destroy() { |
23 |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
24 |
throws ServletException, IOException { |
26 |
request.setCharacterEncoding("GBK"); |
28 |
response.setContentType("text/html; charset=GBK"); |
30 |
PrintWriter out = response.getWriter(); |
32 |
out.write("Hello, 中文!"); |
36 |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
37 |
throws ServletException, IOException { |
39 |
request.setCharacterEncoding("GBK"); |
41 |
response.setContentType("text/html; charset=GBK"); |
44 |
String input_str = request.getParameter("input_str"); |
45 |
String url_arg = request.getParameter("url_arg"); |
48 |
input_str = (null == input_str) ? "" : input_str; |
49 |
url_arg = (null == url_arg) ? "" : url_arg; |
51 |
PrintWriter out = response.getWriter(); |
53 |
out.println("您输入的字符串是:" + input_str); |
56 |
out.println("您的表单传递的URL参数是:" + url_arg); |
60 |
public void init() throws ServletException { |
index.jsp代码:
01 |
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> |
02 |
<%@ page contentType="text/html; charset=GBK"%> |
04 |
String path = request.getContextPath(); |
05 |
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; |
08 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
11 |
<base href="<%=basePath%>"> |
14 |
<script type="text/javascript"> |
16 |
var base = document.base; |
17 |
base.action = "./Hello?url_arg=中文"; |
26 |
<form name="base" method="post" target="_self"> |
27 |
<input name="input_str" type="text" value="" /> |
28 |
<button id="btn" onclick="javascript:clickFun();">点击发送</button> |
当然要运行servlet,必须配置web.xml
01 |
<?xml version="1.0" encoding="UTF-8"?> |
02 |
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" |
03 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
04 |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee |
05 |
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> |
08 |
<welcome-file>index.jsp</welcome-file> |
11 |
<servlet-name>Hello</servlet-name> |
12 |
<servlet-class>com.test.one.Hello</servlet-class> |
15 |
<servlet-name>Hello</servlet-name> |
16 |
<url-pattern>/Hello</url-pattern> |
接下来改一下tomcat中的server.xml文件中的配置(至于为什么可以google一下之前的属性,有大神比我讲解好)
1 |
<Connector port="8080" protocol="HTTP/1.1" |
2 |
connectionTimeout="20000" |
3 |
redirectPort="8443" <strong><span style="color: rgb(255, 0, 0);">useBodyEncodingForURI="true"</span></strong> /> |