HTTPServletRequest
HTTPServletRequest
- HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息;
获取参数,请求转发
- 
获取参数的两个常用方法   
- 
编写登陆的类 @WebServlet("/login") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String[] hobbies = req.getParameterValues("hobby"); System.out.println("================================"); System.out.println(username); System.out.println(password); System.out.println(Arrays.toString(hobbies)); System.out.println("================================"); resp.setCharacterEncoding("utf-8"); req.getRequestDispatcher("success.jsp").forward(req,resp); } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径,需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath}代表当前的项目--%>
<form action="${pageContext.request.contextPath}/login" method="post">
    用户名:<input type="text" name="username"> <br>
    密码:<input type="password" name="password"> <br>
    爱好:
    <input type="checkbox" name="hobby" value="打球">打球
    <input type="checkbox" name="hobby" value="游泳">游泳
    <input type="checkbox" name="hobby" value="睡觉">睡觉
    <br>
    <input type="submit">
</form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号