본문 바로가기

Programming/Servlet & JSP

체크박스(checkbox) 값 servlet으로 전달

PrintCheckbox.java

package com;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ResultInfo")
public class ResultInfo extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("EUC-KR");

		String gender = request.getParameter("gender");
		String[] sports = request.getParameterValues("sports");
		System.out.println(gender);
		System.out.println(Arrays.toString(sports));
	}

}

 

submitCheckbox.html

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="ResultInfo" method="post">
		성별<br />
		남<input type="radio" value="남" name="gender">
		여<input type="radio" value="여" name="gender"><br /><br />
		취미<br />
		축구<input type="checkbox" value="축구" name="sports">
		야구<input type="checkbox" value="야구" name="sports">
		농구<input type="checkbox" value="농구" name="sports"><br /><br />
		<input type="submit" value="전송">
	</form>
</body>
</html>