本文实例为大家分享了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 > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。