目录
一、get请求和post请求的区别
二、Request对象的应用
1、request主要方法
2、request获取数据
3、设置请求的编码格式
三、解决get请求收参乱码问题
四、解决post请求中文乱码问题
一、get请求和post请求的区别
在Servlet中用来处理客户端请求需要用doGet()方法或doPost()方法中的request对象。
get请求:
get提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连
get方式明文传递,数据量小,不安全
效率高,浏览器默认请求方式为get请求
对应的Servlet的方法是doGet()
post请求:
post方法是把提交的数据放在HTTP包的body中
密文传递数据,数据量大,安全
效率没有get高
对应的Servlet的方法是doPost()
二、Request对象的应用
1、request主要方法
2、request获取数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取数据</title>
</head>
<body>
<form action="RequestServlet" method="post">
<p><label>账号:</label><input type="text" name="user"></p>
<p><label>密码:</label><input type="password" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
</html>
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
System.out.println("user:"+user+"\npassword:"+password);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3、设置请求的编码格式
控制台无法获取中文
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String user = request.getParameter("user");
String password = request.getParameter("password");
System.out.println("user:"+user+"\npassword:"+password);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
三、解决get请求收参乱码问题
get请求发送后,在服务器端获取中文数据产生乱码,是因为服务器和客户端沟通的编码不一致造成的,所以解决办法是在客户端和服务器之间设置一个统一的编码,之后就按照设置的编码进行数据的传输和接收。
在Tomcat7及以下版本,客户端以UTF-8的编码传输数据到服务器端,而服务器端的request对象使用的是ISO8859-1这个字符编码来接收数据,服务器和客户端沟通的编码不一致,因此会产生中文乱码。
解决办法:在接收到数据后,先获取request对象以ISO8859-1字符编码接收到的原始数据的字节数组,然后通过字节数组以指定的编码构建字符串,解决乱码问题。
Tomcat8的版本中get不会出现乱码了,因为服务器对url的编码格式可以自动转换
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
user = new String(user.getBytes("ISO8859-1"),"utf-8");
String password = request.getParameter("password");
System.out.println("user:"+user+"\npassword:"+password);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
四、解决post请求中文乱码问题
由于客户端是以UTF-8字符编码将表单数据传输到服务器端的,服务器端的字符编码格式为ISO8859-1,因此服务器端也需要设置以UTF-8字符编码进行接收。
解决方法:使用从ServletRequest接口继承而来的setCharacterEncoding(charset)方法进行统一的编码设置
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String user = request.getParameter("user");
String password = request.getParameter("password");
System.out.println("user:"+user+"\npassword:"+password);
}
}