servlet应用***
1.request对象
 是当客户端向服务器端发送请求时,服务器为本次请求创建request对象,并在调用Servlet的service方法时,将该对象传递给service方法。Request对象中封装了客户端发送过来的所有的请求数据。
 
 ①:doGet()方法接收request数据
 编写html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/SecondServlet/regist" method="get"> 
		用户名:<input type="text" name="username"/>
		密码:<input type="text" name="password"/>
		<input type="submit" value="注册">
	</form>
</body>
</html>
 
编写doGet()方法
@WebServlet("/regist")
public class RegistServlet  extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		System.out.println(username + " " + password);
	}
}
 
②:doPost()方法接收request数据
 编写html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/SecondServlet/regist" method="post"> 
		用户名:<input type="text" name="username"/>
		密码:<input type="text" name="password"/>
		<input type="submit" value="注册">
	</form>
</body>
</html>
 
编写doPost()方法
@WebServlet("/regist")
public class RegistServlet  extends HttpServlet{
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String 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请求时不会出现乱码的。









![[附源码]计算机毕业设计springboot新冠疫苗接种预约系统](https://img-blog.csdnimg.cn/f811eb82f9a7496e9b996ef514694fd1.png)




![[附源码]Python计算机毕业设计Django基于微信小程序的网络办公系统](https://img-blog.csdnimg.cn/8508705e9e5a4720b5cdbb5551cb684a.png)


![[附源码]Python计算机毕业设计Django基于人脸识别的社区防疫管理系统](https://img-blog.csdnimg.cn/1943ed69dbfc4d1e92e2b8d28b1f4db2.png)

