本文實例為大家分享了java表單提交中文亂碼的解決方法,供大家參考,具體內容如下
主頁index.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> < html > < head > < title >servlet演示</ title > </ head > < body > < h2 >中文亂碼</ h2 > <!-- /servletDemo_1/encode最好用絕對目錄,因為index.jsp位置可能會更改 --> < form action = "/servletDemo_1/encode" method = "post" > 姓名:< input type = "text" name = "name" />< br /> 密碼:< input type = "password" name = "pwd" /> < br /> < input type = "submit" value = "登陸" /> </ form > </ body > </ html > |
ServletEncoding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package cn.hncu.servlet_2; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class ServletEncoding implements Servlet { private String charSet= null ; @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null ; } @Override public String getServletInfo() { return null ; } @Override public void init(ServletConfig config) throws ServletException { charSet=config.getInitParameter( "char" ); System.out.println( "編碼:" +charSet); } /* * 解決中文亂碼 * 1)更改tomcat平臺中字符編碼(server.xml-connector的屬性) * 而tomcat是所有項目的公共平臺,所以不要該,盡量不改 * 2)反查ISO8859-1編碼:通過String中的亂碼解決方式 * 3)獲取參數之前設置:req.setCharacterEncoding("utf-8"); * 注意:此種方式必須要是POST方式提交,否則不行 * 4)獲取參數之前設置:通過在web.xml中設置字符參數方式把3)做活 * 5)用過濾器把3)做活--以后實現 */ @Override public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException { // //3)獲取參數之前設置 // req.setCharacterEncoding("utf-8"); //4)獲取參數之前設置:通過在web.xml中設置字符參數方式把3)做活 req.setCharacterEncoding(charSet); //讀取信息處理中文亂碼 String name=req.getParameter( "name" ); String pwd=req.getParameter( "pwd" ); System.out.println( "name:" +name+ ",pwd:" +pwd); // System.out.println("編碼前-name:"+name+",pwd:"+pwd); // //2)反查ISO8859-1編碼:通過String中的亂碼解決方式 // byte bs[]=name.getBytes("iso-8859-1"); // name=new String(bs,"utf-8"); // byte bs2[]=pwd.getBytes("iso-8859-1"); // pwd=new String(bs2,"utf-8"); // System.out.println("編碼后-name:"+name+",pwd:"+pwd); // //注:這種方式對于參數很多時,就不合適使用了 resp.setContentType( "text/html;charset=utf-8" ); //設置協議:IE沒問題,但是有的瀏覽器不兼容 //向客戶端應答 String str= "<html><head><title></title></head><body><font color='red'>name:" +name+ ",pwd:" +pwd+ "</font></body></head>" ; resp.getWriter().println(str); //println()帶刷緩存 } } |
配置文件web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app version = "3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee < display-name ></ display-name > < servlet > < servlet-name >encode</ servlet-name > < servlet-class >cn.hncu.servlet_2.ServletEncoding</ servlet-class > < init-param > < param-name >char</ param-name > < param-value >utf-8</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >encode</ servlet-name > < url-pattern >/encode</ url-pattern > </ servlet-mapping > < welcome-file-list > < welcome-file >index.jsp</ welcome-file > </ welcome-file-list > </ web-app > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。