package com.yunqing.servlet; import javax.servlet.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map; public class LoginServlet implements Servlet { @Override
public void init(ServletConfig servletConfig) throws ServletException { } @Override
public ServletConfig getServletConfig() {
return null;
} @Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//1.
System.out.println(servletRequest.getParameter("user") + "," + servletRequest.getParameter("password"));
//2.
String[] arr = servletRequest.getParameterValues("interesting");
for (String s : arr) {
System.out.println("--------" + s);
}
//3.
Enumeration<String> names = servletRequest.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String value = servletRequest.getParameter(name);
System.out.println(name + "," +value);
}
//4.
Map<String,String[]> map = servletRequest.getParameterMap();
for (Map.Entry<String, String[]> en : map.entrySet()) {
System.out.println(en.getKey() + ":" + Arrays.asList(en.getValue()));
}
} @Override
public String getServletInfo() {
return null;
} @Override
public void destroy() { }
}
前台页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="LoginServlet" method="post">
user:<input type="text" name="user">
password:<input type="password" name="password">
<br><br>
interesting:
<input type="checkbox" name="interesting" value="reading">Read
<input type="checkbox" name="interesting" value="game">game
<input type="checkbox" name="interesting" value="football">football
<input type="checkbox" name="interesting" value="learn">learn
<input type="checkbox" name="interesting" value="other">other
<input type="submit" value="submit">
</form> </body>
</html>
结果图: