package servlet;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
/**
*获取请求参数
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Get:"+request.getParameter("xxx"));
System.out.println("Get:"+request.getParameter("yyy"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user=request.getParameter("username");
String psw=request.getParameter("psw");
System.out.println("username:"+user);
System.out.println("passward:"+psw);
String [] str=request.getParameterValues("hobby"); //获取多值参数
System.out.println("hobby:"+Arrays.toString(str));
Enumeration names=request.getParameterNames(); //获取参数名称
while(names.hasMoreElements())
{
System.out.println(names.nextElement());
}
Map <String ,String []> map=request.getParameterMap(); //获取键值对
for(String name:map.keySet()){
String []value=map.get(name);
System.out.println(name+":"+Arrays.toString(value));
}
}
}
<pre name="code" class="html">>
<h1>测试请求参数</h1>
<a href="/day1119_1/AServlet?xxx=XXX&yyy=YYY">点击这里</a>
<form action="/day1119_1/AServlet" method="post">
用户名:<input type="text" name="username"/><br />
密 码:<input type="password" name="psw" /> <br />
爱 好:<input type="checkbox" name="hobby" name="cf" />吃饭
<input type="checkbox" name="hobby" name="sj" />睡觉
<input type="checkbox" name="hobby" name="dddd" />打豆豆<br />
<input type="submit" value="提交" />
</form>
</body>
</html>