Servlet--中文乱码原理
来源:互联网 发布:java时钟代码 编辑:程序博客网 时间:2024/06/06 18:12
Servlet请求乱码有两种方式分别是get和post请求。
一、Get请求乱码原理
1、在项目页面点击超链接按钮发起get请求,(这个页面index.jsp 设置的编码格式为UTF-8编码格式)--------到tomcat容器
超链接页面发送get信息如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> This is my JSP page. <br> <a href="userservlet?name=wanghong&sex=nan">获取get请求参数</a><br/> </body></html>
对应的编码变成乱码)。
3、Servlet接收到tomcat信息后,用默认的ISO-885-1编码表进行解码得到的是乱码,然后发送给客户端展示页面也就是乱码。
二、Get请求解决方式
1、在项目页面点击超链接按钮发起get请求,(index.jsp页面进行浏览器编码)--------到tomcat容器。
2、tomcat接受到浏览器进行的编码后,传递给请求的Servlet类处理。
3、Servlet接收到tomcat信息后,设置格式为UTF-8编码进行解码。返回给客户端。(servlet接受请求设置get编码为UTF-8如下)
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UserServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置一个变量name 来接受用户请求的参数String name = request.getParameter("name");//接收到的参数先用ISO-8859-1编码进行解码,在用UTF-8进行编码name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//设置响应response.setContentType("text/html;charset=UTF-8");response.getWriter().print(name);}}
三、Get响应乱码原理
1、主要是因为服务器输出的字符串的编码和客户端显示字符串的编码不一致。导致乱码问题。所以我们只需要设置服务器和客户端的编码相同就可以解决这个问题。
四、Get响应乱码解决方式
1、设置服务器输出的编码为UTF-8. response.setCharacterEncoding("UTF-8")
2、设置客户端浏览器的查看方式为UTF-8 response.setContentType("text/html;charset=UTF-8")
3、具体设置如下:
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UserServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String name = request.getParameter("name");name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//设置响应response.setCharacterEncoding("UTF-8"); //设置服务器输出的编码为UTF-8response.setContentType("text/html;charset=UTF-8"); //设置客户端浏览器的查看方式为UTF-8response.getWriter().print(name);}}
一、Post请求乱码原理
1、在项目页面点击超链接按钮发起post请求,(浏览器会为post请求自动编码,不会出现get请求需要手动设置浏览器编码)--------到tomcat容器
超链接页面发送post信息如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> This is my JSP page. <br> <a href="userservlet?name=wanghong&sex=nan">获取get请求参数</a><br/> <!-- form表单提交方式获取post请求参数 --> <form action="userservlet" method="post"> 名字:<input type="text" name="name" value="王红"/><br/> 性别:<input type="text" name="sex" value="男"/><br/> <input type="submit" value="提交"/> </form> </body></html>
2、tomcat接收到经过浏览器UTF-8编码的请求,传递给请求的Servlet类处理。(此时请求的编码表是UTF-8编码)
3、Servlet接收到tomcat信息后,用默认的ISO-885-1编码表进行解码得到的是乱码,然后发送给客户端展示页面也就是乱码。
二、Post请求乱码解决方式
1、在Servlet中设置接收编码方式为UTF-8,在发送给客户端。
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UserServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置接收到的数据是utf-8编码。//如果没有给request设置utf-8编码,则默认为iso-8859-1request.setCharacterEncoding("UTF-8");//对get没有效response.setContentType("text/html;charset=UTF-8");//设置响应的PrintWriter out = response.getWriter();String name = request.getParameter("name");//这是接收用户的参数String age = request.getParameter("age");//显示out.print("POST>你的名称是:"+name+",年龄是:"+age);}}
三、Post响应乱码原理
1、主要是因为服务器输出的字符串的编码和客户端显示字符串的编码不一致。导致乱码问题。所以我们只需要设置服务器和客户端的编码相同就可以解决这个问题。
四、Post响应乱码解决方式
1、设置服务器输出的编码为UTF-8. response.setCharacterEncoding("UTF-8")
2、设置客户端浏览器的查看方式为UTF-8 response.setContentType("text/html;charset=UTF-8")
public class UserServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置接收到的数据是utf-8编码。//如果没有给request设置utf-8编码,则默认为iso-8859-1request.setCharacterEncoding("UTF-8");//对get没有效//设置响应response.setCharacterEncoding("UTF-8"); //设置服务器输出的编码为UTF-8response.setContentType("text/html;charset=UTF-8"); //设置客户端浏览器的查看方式为UTF-8PrintWriter out = response.getWriter();String name = request.getParameter("name");//这是接收用户的参数String age = request.getParameter("age");//显示out.print("POST>你的名称是:"+name+",年龄是:"+age);}}
- Servlet--中文乱码原理
- Servlet 传输中文乱码原理及解决方法
- Servlet + Tomcat 中文乱码的原理和解决方法
- Servlet + Tomcat 中文乱码的原理和解决方法
- Servlet + Tomcat 中文乱码的原理和解决方法
- Servlet 中文乱码解决
- Servlet 中文乱码解决
- Servlet 中文乱码解决
- Servlet 中文乱码解决
- Servlet 中文乱码解决
- Servlet 中文乱码解决
- Servlet中文乱码解决方案
- servlet 中文乱码问题:
- servlet中文乱码
- servlet中文乱码问题
- servlet中文乱码问题
- servlet gzip 中文乱码
- servlet中文乱码问题
- 黑盒测试和白盒测试区别
- pairs和make_pair的使用
- Spring learn note 2
- Tempter of the Bone
- scala合并有序集合
- Servlet--中文乱码原理
- C++ vector容器中如何查找一个值是否在这个容器中
- 强化学习
- ES2017 新特性:Async Functions (异步函数)
- Linux--软件的升级和查询
- std::string 类中find_first_not_of ()函数
- Radar
- java高级特性之--集合
- The experience of the tenth ciscn!