servlet获取复选框内容

         先通过String[] hobbys=request.getParameterValues("hobby");获取复选框的内容,并将其放入数组中,然后遍历数组输出就可以了。

servlet代码:

package com.neusoft.servlet;

import java.io.IOException;
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 IpServlet
 */
@WebServlet("/InfoServlet")
public class InfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		int age=Integer.valueOf(request.getParameter("age"));
		response.getWriter().print(age);
		//复选框接受一个数组
		String[] hobbys=request.getParameterValues("hobby");
		for(String s:hobbys){
			response.getWriter().print(s);
		}
	}

}

html代码:





Insert title here


	
age=
hobby=aa bb cc

 

你可能感兴趣的:(servlet获取复选框内容)