您的位置:首页 > 编程语言 > Java开发

Java中Servlet的Get和Post乱码问题

2014-11-15 16:51 239 查看
提交有两种方式,doGet()和doPost()两种

出现乱码的原因

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

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

POST方式解决

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

requset.setCharacterEncoding("UTF-8");

GET方式



代码如下

Index.jsp代码

<span style="font-size:18px;"><%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import = "java.net.URLEncoder" %>
<%
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>登陆界面</title>

</head>

<body>
<form action="RequestDemo" method="post">
<table border="1" align="center" cellspacing="0">
<caption>用户注册</caption>
<tr>
<th>用户名:</th>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<th>密 码:</th>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<th>性 别:</th>
<td><input checked type="radio" name="gender" value=male/>男
<input  type="radio" name="gender" value=female/>女
</td>
</tr>
<tr>
<th>爱 好:</th>
<td>
<input type="checkbox" value="sing" name="likes">唱歌
<input type="checkbox" value="dance" name="likes">跳舞
<input type="checkbox" value="play" name="likes">打球
<input type="checkbox" value="internet" name="likes">上网
</td>
</tr>
<tr>
<th>你的靓照:</th>
<td>
<input type="file" name="picture">
</td>
</tr>
<tr>
<th>你所在的城市</th>
<td>
<select name="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="天津">天津</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
</select>
</td>
</tr>
<tr>
<th>简 介:</th>
<td>
<textarea rows="12" cols="25" name="message"></textarea>
</td>

</tr>
<tr>
<td colspan="3" align="center">
<input type ="submit" value="提交"/>                
<input type ="reset" value="重置"/>
</td>
</tr>

<tr>
<td><a href="/servletDay/RequestDemo?username=<%= URLEncoder.encode("张三","utf-8") %>">测试</a></td>
</tr>
</table>
</form>

</body>
</html></span>


Servlet代码

<span style="font-size:18px;">package servlet.request;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDemo extends HttpServlet{

//get方式是将username封装到请求头中
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String name = req.getParameter("username");

resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write(new String(name.getBytes("iso8859-1"),"utf-8"));

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

//doPost设置编码防止乱码
req.setCharacterEncoding("UTF-8");

String name = req.getParameter("username");
String pwd = req.getParameter("pwd");
String likes =  req.getParameterValues("likes").toString();
String like = likes.substring(1, likes.length()-2);
String pic = req.getParameter("picture");
String city = req.getParameter("city");
String message = req.getParameter("message");
Person p = new Person(name,pwd,like,message,pic,city);
System.out.println(p);
/*
*  text/html;charset=UTF-8
*  resp.setCharacterEncoding("utf-8"); 等价于
*  resp.setContentType("text/html;charset=UTF-8");
*/

resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write(p.toString());

}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: