9. JSP - Servlet 파라미터 받기
String getParameter(String name)
: name이름으로 전송되어 온 파라미터값을 반환해 주는 메소드
: HttpServletRequest에서 제공.
: 하나만 받음.
String[] getParameterValues(String paramName)
: 여러개 파라미터 값 처리
: checkbox같은 값 처리
ex>checkbox 처리.
- test01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>강아지선택</h1>
<form action="dog" method="post">
<input type="checkbox" name="dog" value="진돗개">진돗개
<input type="checkbox" name="dog" value="푸들">푸들
<input type="checkbox" name="dog" value="삽살개">삽살개
<input type="submit" value="send">
</form>
</body>
</html>
- MultiRequest.java
package test;
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 MultiRequest
*/
@WebServlet("/dog")
public class MultiRequest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//한글처리
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
String[] dog = request.getParameterValues("dog");
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<table>");
out.println("<tr>");
for(int i=0;i<dog.length;i++){
out.println("<td>");
out.println(dog[i]);
out.println("</td>");
}
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}