获取多个相同参数
package demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Xingqaihao
*/
@WebServlet("/index.jsp")
public class Xingqaihao extends HttpServlet {
private static final long serialVersionUID = 1L;
//构造方法
public Xingqaihao() {
super();
// TODO Auto-generated constructor stub
}
//获取
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();//取得响应输出对象
out.println("<html>");
out.println("<head>");
out.println("<title>兴趣爱好</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action='index.jsp'>");
out.println("<p>Please select your favorite music service(s):");
out.println("<br />");
out.println("<input type='checkbox' name='service' value='看电视' checked='checked' />看电视");
out.println("<input type='checkbox' name='service' value='打游戏'/>打游戏 ");
out.println("<input type='checkbox' name='service' value='睡觉'/>睡觉");
out.println("<input type='submit' name='login' value='提交'/>");
out.println("</p>");
String[] Hobby=request.getParameterValues("service");//获取参数值
out.println("你选择的兴趣如下"+"<br>");//输出
//循环遍历,打印
for( int i=0; i<Hobby.length; i++) { 知识点:1.运用了HTML中的复选框
out.println(Hobby[i]+"<br>"); 2.运用了getParameterValues方法,来获取多个相同参数
3.运用了java中的for循环,来遍历输出
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
}