前面我们提到了Response对象中出现乱码问题及相应的解决措施,那么在Request中如何解决编码问题呢?
下面来看具体的例子:
例如我想将一个form表单中的信息提取到并在控制台输出如图:
假设在服务端并未Request对象给指定编码时那么你在客户端添加信息如:
填入的信息有中文
假设设置表单的提交方式为post方式提交
则在服务端输出如下:
代码如下:
Register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action ="RequestLogin" method ="post">
用户名:<input type="text" name = "username" ><br/>
密码: <input type="password" name = "password"><br/>
性别:<input type="radio" name = "sex" value ="male">男
<input type="radio" name = "sex" value ="female">女<br/>
籍贯: <select name ="city">
<option value ="HeBei">河北</option>
<opton value ="HuBei">湖北</opton>
<option value ="ShanXi">山西</option>
</select><br/>
简历:<br/>
&nsp;<textarea rows="5" cols="20" name ="intro"></textarea>
<br/>
爱好:<br/>
<input type="checkbox" name ="hobbies" value ="sing"/>唱歌
<input type="checkbox" name ="hobbies" value ="dance"/>跳舞
<input type="checkbox" name ="hobbies" value ="readbook"/>读书
<input type="checkbox" name ="hobbies" value ="readnewspaper"/>看报<br/>
上传头像:<br/>
<input type="file" value ="image" name ="browser"><br/>
<input type="submit" value ="提交"/>
</form>
</body>
</html>
RequestLogin.java
package net.csdn.request;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestLogin extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
getInformation(request);
}
private void getParameter(HttpServletRequest request)
throws UnsupportedEncodingException
{
private void getInformation(HttpServletRequest request)
throws UnsupportedEncodingException {
//取出参数值
String name = request.getParameter("username");
String pass = request.getParameter("password");
String sex = request.getParameter("sex");
String city = request.getParameter("city");
String intro = request.getParameter("intro");
String [] hobbies = request.getParameterValues("hobbies");
String hobby ="";
//hobbies!=null对所取值为空时进行设置
for(int i=0;hobbies!=null&&i<hobbies.length;i++)
{ String hovalue = hobbies[i]; hobby += hovalue; }
//获取头像信息 // String image = request.getParameter("image");
System.out.println("username:"+name);
System.out.println("password:"+pass);
System.out.println("sex:"+sex); System.out.println("city:"+city);
System.out.println("intro:"+intro); System.out.println("hobby:"+hobby);}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doGet(request, response);}}
这是因为你在RegisterLogin.java中并没有给Request对象设置编码集,而Request对象的默认编码集是ISO8859-1是不支持汉字的,所以你只需要在此类中为其指明相应的编码即可:
改正:
request.setCharacterEncoding("utf-8");
即可输出:
但是这种方式只在提交方式为post时有效当提交方式为get时是不起作用的。
即;
<form action ="RequestLogin" method ="get">
时即便在
RequestLogin.java
中再设置
request.setCharacterEncoding("utf-8");
也不会起任何作用了如图:
这时就需要在
RequestLogin.java
中的含有中文的地方进行如下设置了即:
String username = new String(name.getBytes("iso8859-1"),"utf-8");
String introduction = new String(intro.getBytes("iso8859-1"),"utf-8");
System.out.println("username:"+username);
System.out.println("password:"+introduction);
此时再度测试时就OK了!如图