request应用
- HttpServletRequest代表客户端的请求
- 用户通过Http协议访问服务器,http请求中的所有信息会被封装到HttpServletRequest
- 通过HttpServletRequest的方法获得客户端的所有信息
获取前端传送的数据,请求转发
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<div style="text-align: center">
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input name="username" type="text" ><br>
密码: <input name="password" type="password" ><br>
爱好:
<input name="hobby" type="checkbox" value="sleep">睡觉
<input name="hobby" type="checkbox" value="sports">运动
<input name="hobby" type="checkbox" value="sing">唱歌
<input type="submit">
</form>
</div>
</body>
</html>
LoginServlet
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
//请求转发
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
浙公网安备 33010602011771号