JavaWeb入门—解决Post请求中文乱码问题
2019-02-22 19:08
357 查看
版权声明:. https://blog.csdn.net/WildestDeram/article/details/87876435
解决Post请求中文乱码问题
1.乱码的由来
Tomcat默认使用字符集为ISO-8859-1,属于西欧字符集,如果输入的字符不属于西欧字符集,将出现乱码。
2.解决乱码
解决乱码的核心思路是将ISO-8859-1转换为UTF-8,Servlet中请求与响应都需要设置UTF-8字符集。
3.代码展示
charset_form.html
[code]<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/SerlvetAndJSP/CharsetServlet" method="post"> 姓名:<input name="ename"><br><br> 地址:<input name="address"><br><br> <input type="submit" value="提交"> </form> </body> </html>
CharsetServlet.java
[code]package com.imooc.Servletcharset; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CharsetServlet */ @WebServlet("/CharsetServlet") public class CharsetServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CharsetServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String ename = request.getParameter("ename"); String address = request.getParameter("address"); //将指定的字符进行转换 String utf8Ename = new String(ename.getBytes("ISO-8859-1"),"UTF-8"); String utf8Address = new String(address.getBytes("iso-8859-1"),"utf-8"); System.out.println(utf8Ename); System.out.println(utf8Address); } }
但是在实际开发中可能涉及到大量字符编码,一个一个转换显得麻烦,这里提供一个统一修改字符集的方法
[code]package com.imooc.Servletcharset; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CharsetServlet */ @WebServlet("/CharsetServlet") public class CharsetServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CharsetServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //用于将修改请求体的字符集 request.setCharacterEncoding("UTF-8"); String ename = request.getParameter("ename"); String address = request.getParameter("address"); System.out.println(ename); System.out.println(address); } }
注意:该方法只能在doPost()中使用,因为该方法是修改"请求体"中的字符集,doGet()是Get请求的方法,Get是没有请求体的,只有Post请求才有。
相关文章推荐
- 解决问题:在使用springmvc的web.xml配置乱码拦截器之后接收的get或者post请求中文还是乱码
- 解决servlet中文乱码问题及get、post请求方式的讲解
- 解决Get和Post请求中文乱码的问题
- Struts2中解决POST请求和GET请求中的中文乱码问题
- Struts2中解决POST请求和GET请求中的中文乱码问题
- Get,Post请求中文乱码问题有效解决方法
- Get,Post请求中文乱码问题有效解决方法
- Http Post请求提交json格式数据工具类,解决请求参数中文乱码问题
- Get,Post请求中文乱码 4000 问题有效解决方法
- Get,Post请求中文乱码问题有效解决方法
- 完美解决Get和Post请求中文乱码的问题
- 关于Get,Post请求中文乱码问题有效解决方法
- 解决在Unity3D中通过Post向服务器提交请求,表单中带有中文导致的乱码问题!
- 解决spring3.0.5使用RestTemplate发送post请求响应中文乱码问题
- Spring MVC 3 表单中文提交post请求和get请求乱码问题的解决方法
- 解决Get和Post请求中中文乱码问题
- HTTP POST请求中文乱码问题解决
- 解决Get和Post请求中中文乱码问题 (2013-01-26 23:59:39)转载▼ 标签: javaee it 分类: JavaEE 对于Post请求,只需在Servlet或者jsp中写入如下
- 解决Sping Mvc中post、get请求中文乱码问题
- Http请求post方法,请求和响应中文乱码问题的一点解决方法