获取前端数据
请求转发
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%--${pageContext.request.contextPath}获取当前项目路径,需要导入jsp servlet的依赖包--%>
<form action="/s3/req" method="get">
用户名:<input name="username" type="text"><br>
密 码:<input name="password" type="password"><br>
爱好:
<input type="checkbox" name="hobbys" value="泡妞">泡妞
<input type="checkbox" name="hobbys" value="K歌">K歌
<input type="checkbox" name="hobbys" value="赛车">赛车
<input type="submit">
</form>
</body>
</html>
public class Req extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("username");
String pwd = req.getParameter("password");
System.out.println(name+":"+pwd);
String[] hobbys = req.getParameterValues("hobbys");
System.out.println(Arrays.toString(hobbys));
//请求转发
req.getRequestDispatcher("login.jsp").forward(req,resp);
// String a = req.getContextPath();
// System.out.println(a);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
3.注册servlet