Servlet--中文乱码原理

举报
brucexiaogui 发表于 2021/12/30 01:56:09 2021/12/30
2k+ 0 0
【摘要】 Servlet请求乱码有两种方式分别是get和post请求。 一、Get请求乱码原理 1、在项目页面点击超链接按钮发起get请求,(这个页面index.jsp 设置的编码格式为UTF-8编码格式)--------到tomcat容器 超链接页面发送get信息如下: <%@ page language="jav...

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>
  
 





二、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-8
      		response.setContentType("text/html;charset=UTF-8"); //设置客户端浏览器的查看方式为UTF-8
      		response.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-1
      		request.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-1
      		request.setCharacterEncoding("UTF-8");//对get没有效
     		//设置响应
      		response.setCharacterEncoding("UTF-8");  //设置服务器输出的编码为UTF-8
      		response.setContentType("text/html;charset=UTF-8"); //设置客户端浏览器的查看方式为UTF-8
     		PrintWriter out = response.getWriter();
     		String name = request.getParameter("name");//这是接收用户的参数
     		String age = request.getParameter("age");
     		//显示
      		out.print("POST>你的名称是:"+name+",年龄是:"+age);
      	}
      }
  
 


文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。

原文链接:brucelong.blog.csdn.net/article/details/76481787

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。