1.request对象
是当客户端向服务器端发送请求时,服务器为本次请求创建request对象,并在调用Servlet的service方法时,将该对象传递给service方法。Request对象中封装了客户端发送过来的所有的请求数据。

①:doGet()方法接收request数据
编写html
Insert title here
编写doGet()方法
@WebServlet("/regist")
public class RegistServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubString username = req.getParameter("username");String password = req.getParameter("password");System.out.println(username + " " + password);}}
②:doPost()方法接收request数据
编写html
Insert title here
编写doPost()方法
@WebServlet("/regist")
public class RegistServlet extends HttpServlet{@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubString username = req.getParameter("username");String password = req.getParameter("password");System.out.println(username + " " + password + "dopost");}}
③:请求乱码问题
由于request是接收来自用户的请求,服务器会根据编码格式将请求转换。服务器端默认的编码格式为ISO-8859-1(此编码不支持中文),而我们用户浏览器默认是utf-8的编码格式,所以往往会产生乱码。要想解决乱码问题,需要设置request当中的编码格式,告诉服务器以何种形式来解析数据。或者在接收到乱码以后,通过何种编码格式进行还原
方式一:
request.setCharacterEncoding("UTF-8");
该方法只针对POST有效(必须在接收数据之前设定)
方式二:
new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8"))
借助String对象的方法,该种方式对任何请求有效,都是通用的。
Tomcat8以后的get请求时不会出现乱码的。