首先,如何设置web启动时的页面
在web.xml中
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="na"><br>
密码: <input type="password" name="passwd"><br>
爱好:<br>
<input type="checkbox" name="hobby" value="篮球">篮球<br>
<input type="checkbox" name="hobby" value="足球">足球<br>
<input type="checkbox" name="hobby" value="羽毛球">羽毛球<br>
<input type="checkbox" name="hobby" value="乒乓球">乒乓球<br>
<input type="submit">
</form>
</body>
</html>
注意: action="${pageContext.request.contextPath}/login"
login.java
public class login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF8");
resp.setCharacterEncoding("UTF8");
String username = req.getParameter("na");
String password = req.getParameter("passwd");
String[] arr = req.getParameterValues("hobby");
System.out.println("====================");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(arr));
System.out.println("====================");
resp.sendRedirect("success.html");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}