Java中Servlet的Get和Post乱码问题

 

Java中Servlet的Get和Post乱码问题


提交有两种方式,doGet()和doPost()两种

出现乱码的原因

因为容器(Tomcat)采用的是ISO8859-1编码方式.里面没有对中文进行解析.所以当从浏览器传过来数据中有中文时候,就应该考虑是否会出现乱码问题!

其中GET和POST两种方式不同.因为GET方式请求,容器会将数据信息封装到请求头中,而POST方式请求,容器会将数据信息封装到请求体中!

POST方式解决

只需要将容器中的编码方式ISO8859-1用UTF-8进行编码

requset.setCharacterEncoding("UTF-8");

GET方式


代码如下

Index.jsp代码

[java]  view plain copy print ?
  1. <span style="font-size:18px;"><%@page import="java.net.URLEncoder"%>  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%@ page import = "java.net.URLEncoder" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>登陆界面</title>  
  15.       
  16.   </head>  
  17.     
  18.   <body>  
  19.     <form action="RequestDemo" method="post">  
  20.     <table border="1" align="center" cellspacing="0">  
  21.         <caption>用户注册</caption>  
  22.         <tr>  
  23.             <th>用户名:</th>  
  24.             <td><input type="text" name="username"/></td>  
  25.         </tr>  
  26.         <tr>  
  27.             <th>密 码:</th>  
  28.             <td><input type="password" name="pwd"/></td>  
  29.         </tr>  
  30.         <tr>  
  31.             <th>性 别:</th>  
  32.             <td><input checked type="radio" name="gender" value=male/>男  
  33.                 <input  type="radio" name="gender" value=female/>女  
  34.             </td>  
  35.         </tr>  
  36.         <tr>  
  37.          <th>爱 好:</th>  
  38.          <td>  
  39.             <input type="checkbox" value="sing" name="likes">唱歌  
  40.             <input type="checkbox" value="dance" name="likes">跳舞  
  41.             <input type="checkbox" value="play" name="likes">打球  
  42.             <input type="checkbox" value="internet" name="likes">上网  
  43.          </td>  
  44.         </tr>  
  45.         <tr>  
  46.             <th>你的靓照:</th>  
  47.             <td>  
  48.                 <input type="file" name="picture">  
  49.             </td>  
  50.         </tr>  
  51.         <tr>  
  52.         <th>你所在的城市</th>  
  53.         <td>  
  54.             <select name="city">  
  55.                 <option value="北京">北京</option>  
  56.                 <option value="上海">上海</option>  
  57.                 <option value="天津">天津</option>  
  58.                 <option value="广州">广州</option>  
  59.                 <option value="深圳">深圳</option>  
  60.             </select>  
  61.         </td>  
  62.         </tr>  
  63.         <tr>  
  64.         <th>简 介:</th>  
  65.         <td>  
  66.             <textarea rows="12" cols="25" name="message"></textarea>  
  67.         </td>  
  68.           
  69.         </tr>  
  70.         <tr>  
  71.         <td colspan="3" align="center">  
  72.             <input type ="submit" value="提交"/>                  
  73.             <input type ="reset" value="重置"/>  
  74.         </td>  
  75.         </tr>  
  76.           
  77.         <tr>  
  78.         <td><a href="/servletDay/RequestDemo?username=<%= URLEncoder.encode("张三","utf-8") %>">测试</a></td>  
  79.         </tr>  
  80.     </table>  
  81.     </form>  
  82.      
  83.   </body>  
  84. </html></span>  


Servlet代码

[java]  view plain copy print ?
  1. <span style="font-size:18px;">package servlet.request;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. public class RequestDemo extends HttpServlet{  
  11.       
  12.   
  13.     //get方式是将username封装到请求头中  
  14.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  15.             throws ServletException, IOException {  
  16.           
  17.         String name = req.getParameter("username");  
  18.           
  19.         resp.setContentType("text/html;charset=utf-8");  
  20.         resp.getWriter().write(new String(name.getBytes("iso8859-1"),"utf-8"));  
  21.           
  22.           
  23.     }  
  24.   
  25.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  26.             throws ServletException, IOException {  
  27.       
  28.         //doPost设置编码防止乱码  
  29.         req.setCharacterEncoding("UTF-8");  
  30.           
  31.         String name = req.getParameter("username");  
  32.         String pwd = req.getParameter("pwd");  
  33.         String likes =  req.getParameterValues("likes").toString();  
  34.         String like = likes.substring(1, likes.length()-2);  
  35.         String pic = req.getParameter("picture");  
  36.         String city = req.getParameter("city");  
  37.         String message = req.getParameter("message");  
  38.         Person p = new Person(name,pwd,like,message,pic,city);  
  39.         System.out.println(p);  
  40.         /* 
  41.          *  text/html;charset=UTF-8 
  42.          *  resp.setCharacterEncoding("utf-8"); 等价于 
  43.          *  resp.setContentType("text/html;charset=UTF-8"); 
  44.          */  
  45.           
  46.         resp.setContentType("text/html;charset=UTF-8");  
  47.         resp.getWriter().write(p.toString());  
  48.           
  49.           
  50.           
  51.           
  52.     }  
  53.       
  54.   
  55. }</span>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值