Javaweb接收表单数据并处理中文乱码

2025-05-29 0 93

前端表单数据

常见的表单项的传值,如:

  • 普通input
  • 单选radio
  • 多选checkbox
  • select下拉选择
  • textarea文本域

普通 input : name属性值为后台接收时的参数值。

用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>

单选 radio :单选按钮的 name 值相同才能实现只能点击一个。

性别:

<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女

多选checkbox :name值相同。

爱好:

<input type="checkbox" name="hobby" value="唱">唱
<input type="checkbox" name="hobby" value="跳舞">跳舞
<input type="checkbox" name="hobby" value="rap">rap
<input type="checkbox" name="hobby" value="篮球">篮球

select下拉选择 :后台通过degree作为参数,获取选中的那个option的value值。

下拉选择:

<select name="degree">
<option value="">—请选择—</option>
<option value="大一">大一</option>
<option value="大二">大二</option>
<option value="大三">大三</option>
<option value="大四">大四</option>
</select>

textarea文本域 :rows定义显示的行数,cols定义的是显示的列数。

文本域:<br><textarea name="other" rows="10" cols="30"></textarea><br>

后台接收数据

接收表单数据

String 表单name= request.getParameter(表单name);

普通input、单选radio、select下拉选择、textarea文本域可通过此方法获取。

String[] hobbies = request.getParameterValues("hobby");

多选checkbox可通过此方法获取。

中文乱码处理

GET方式提交的数据

先通过 String username = request.getParameter(username) 获得该表单的值,此时是乱码的。

使用String new_username = new String(username.getBytes("iso8859-1"), "utf-8") 进行编码转换

相关APi :

String(byte[] bytes, Charset charset) 构造一个新的String,由指定的字节的数组转化为指定编码的字节数组。

getBytes(Charset charset)使用指定的编码方式将该String编码为字节序列,将结果存储到新的字节数组中。

解释:通过get方式提交的数据的编码方式为iso8859-1, 先获取该编码方式的字节数组,再将该字节数组转化为utf-8编码的字节数组,然后将该字节数组转换为字符串。

POST方式提交的数据

request.setCharacterEncoding("utf-8");

服务器端向客户端发送的数据

response.setContentType("text/html;charset=utf-8");

以下是全部代码:

GET提交方式:

?

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
@WebServlet(name = "RegisterServlet",urlPatterns = "/register")

public class RegisterServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//get提交方式处理中文乱码

String username = request.getParameter("username");

String new_username = new String(username.getBytes("iso8859-1"), "utf-8");

String password = request.getParameter("password");

String new_password = new String(password.getBytes("iso8859-1"), "utf-8");

String gender = request.getParameter("gender");

String new_gender = new String(gender.getBytes("iso8859-1"), "utf-8");

String[] hobbies = request.getParameterValues("hobby");

for (int i = 0; i < hobbies.length; i++) {

hobbies[i]=new String(hobbies[i].getBytes("iso8859-1"), "utf-8");

}

String degree = request.getParameter("degree");

String new_password = new String(password.getBytes("iso8859-1"), "utf-8");

String other = request.getParameter("other");

String new_password = new String(password.getBytes("iso8859-1"), "utf-8");

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request, response);

}

}

POST提交方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
@WebServlet(name = "RegisterServlet",urlPatterns = "/register")

public class RegisterServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//post提交方式的中文乱码解决方法

request.setCharacterEncoding("utf-8");

String username = request.getParameter("username");

String password = request.getParameter("password");

String gender = request.getParameter("gender");

String[] hobbies = request.getParameterValues("hobby");

String degree = request.getParameter("degree");

String other = request.getParameter("other");

//如果服务器端需要向客户端发送的数据

response.setContentType("text/html;charset=utf-8");

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request, response);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/nieaojie625/p/13268333.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Javaweb接收表单数据并处理中文乱码 https://www.kuaiidc.com/119336.html

相关文章

发表评论
暂无评论