上面写了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类(注释说明)
01 | package com.test.one; |
02 |
03 | import java.io.IOException; |
04 | import java.io.PrintWriter; |
05 |
06 | import javax.servlet.ServletException; |
07 | import javax.servlet.http.HttpServlet; |
08 | import javax.servlet.http.HttpServletRequest; |
09 | import javax.servlet.http.HttpServletResponse; |
10 |
11 | public class Hello extends HttpServlet { |
12 |
13 | private static final long serialVersionUID = 4878915372815719687L; |
14 |
15 | public Hello() { |
16 | super (); |
17 | } |
18 |
19 | public void destroy() { |
20 | super .destroy(); // Just puts "destroy" string in log |
21 | } |
22 |
23 | public void doGet(HttpServletRequest request, HttpServletResponse response) |
24 | throws ServletException, IOException { |
25 | //输入:设置请求编码格式 |
26 | request.setCharacterEncoding( "GBK" ); |
27 | //输出:设置响应编码格式 |
28 | response.setContentType( "text/html; charset=GBK" ); |
29 | |
30 | PrintWriter out = response.getWriter(); |
31 | out.write( "<hr>" ); |
32 | out.write( "Hello, 中文!" ); |
33 | out.write( "<hr>" ); |
34 | } |
35 |
36 | public void doPost(HttpServletRequest request, HttpServletResponse response) |
37 | throws ServletException, IOException { |
38 | //输入:设置请求编码格式 |
39 | request.setCharacterEncoding( "GBK" ); |
40 | //输出:设置响应编码格式 |
41 | response.setContentType( "text/html; charset=GBK" ); |
42 | |
43 | //从请求中接收参数 |
44 | String input_str = request.getParameter( "input_str" ); |
45 | String url_arg = request.getParameter( "url_arg" ); |
46 | |
47 | //出错处理 |
48 | input_str = ( null == input_str) ? "" : input_str; |
49 | url_arg = ( null == url_arg) ? "" : url_arg; |
50 | |
51 | PrintWriter out = response.getWriter(); |
52 | out.write( "<hr>" ); |
53 | out.println( "您输入的字符串是:" + input_str); |
54 | out.write( "<hr>" ); |
55 | // out.println("您的表单传递的URL参数是:" + new String(url_arg.getBytes("ISO-8859-1"), "GBK")); |
56 | out.println( "您的表单传递的URL参数是:" + url_arg); |
57 | out.write( "<hr>" ); |
58 | } |
59 |
60 | public void init() throws ServletException { |
61 | // Put your code here |
62 | } |
63 |
64 | } |
index.jsp代码:
01 | <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> |
02 | <%@ page contentType="text/html; charset=GBK"%> |
03 | <% |
04 | String path = request.getContextPath(); |
05 | String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; |
06 | %> |
07 |
08 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
09 | < html > |
10 | < head > |
11 | < base href="<%=basePath%>"> |
12 | < title >编码测试</ title > |
13 | |
14 | < script type = "text/javascript" > |
15 | function clickFun() { |
16 | var base = document.base; |
17 | base.action = "./Hello?url_arg=中文"; |
18 | base.submit(); |
19 | } |
20 | </ script > |
21 | |
22 | </ head > |
23 | |
24 | < body > |
25 | <!-- 测试表单 --> |
26 | < form name = "base" method = "post" target = "_self" > |
27 | < input name = "input_str" type = "text" value = "" /> |
28 | < button id = "btn" onclick = "javascript:clickFun();" >点击发送</ button > |
29 | </ form > |
30 | </ body > |
31 | </ html > |
当然要运行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"> |
06 | |
07 | < welcome-file-list > |
08 | < welcome-file >index.jsp</ welcome-file > |
09 | </ welcome-file-list > |
10 | < servlet > |
11 | < servlet-name >Hello</ servlet-name > |
12 | < servlet-class >com.test.one.Hello</ servlet-class > |
13 | </ servlet > |
14 | < servlet-mapping > |
15 | < servlet-name >Hello</ servlet-name > |
16 | < url-pattern >/Hello</ url-pattern > |
17 | </ servlet-mapping > |
18 | <!-- |
19 | <filter> |
20 | <filter-name>encodingFilter</filter-name> |
21 | <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> |
22 | <init-param> |
23 | <param-name>encoding</param-name> |
24 | <param-value>GBK</param-value> |
25 | </init-param> |
26 | <init-param> |
27 | <param-name>forceEncoding</param-name> |
28 | <param-value>true</param-value> |
29 | </init-param> |
30 | </filter> |
31 |
32 | <filter-mapping> |
33 | <filter-name>encodingFilter</filter-name> |
34 | <url-pattern>*.html</url-pattern> |
35 | </filter-mapping> |
36 | <filter-mapping> |
37 | <filter-name>encodingFilter</filter-name> |
38 | <url-pattern>*.jsp</url-pattern> |
39 | </filter-mapping> |
40 | --> |
41 | </ web-app > |
接下来改一下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 > /> |