WebProgramming/JSP
Form 전송으로 Servlet 값 받는 법
ChrisMare
2018. 10. 17. 14:09
Servlet에서 값을 받는 메소드 3가지
HttpServletRequest 객체를 이용하여, Parameter 값을 얻는다.
<관련 메소드>
- request.getParameter("name"); --> 전송한 값들 중 지정한 "name"을 String 값으로 가져온다.
- request.getParameterValues("name"); --> checkbox와 같은 이름들 중 선택한 값들을 가져온다. return String[ ]
- request.getParameterNames( ); --> 전송한 모든 name을 가져온다.
Form 태그
- input 태그들의 값을 서버로 전송하기 위한 정보를 담고 있다.
<form action="FormEx" method="post">
- action의 값으로는 요청하는 컴포넌트 이름 ( ex> login.jsp, info.html, HWorld[맵핑된 이름] )
- method의 값으로는 요청을 처리하는 방식 ( ex> get, post )
- Get : http://IP주소:port번호/컨텍스트/path/MemberJoin?id="admin"&name="홍길동" -> doGet() 호출 [ 경로노출, 보안 위험 ]
- Post : http://IP주소:port번호/컨텍스트/path/MemberJoin -> doPost() 호출 [header에 정보가 담겨서 서버에 전송]
ex)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>FormEx</title> </head> <body> <form action="ExServlet" method="post"> <label>id : <input type="text" name="id_info" size="10em"></label><br> <label>password : <input type="password" name="pw_info" size="10em"></label><br> <label>name : <input type="text" name="name_info"></label><br> <input type="checkbox" name="hobby" value="축구">축구 <input type="checkbox" name="hobby" value="야구" checked="checked">야구 <input type="checkbox" name="hobby" value="농구">농구 <input type="checkbox" name="hobby" value="테니스">테니스<br> <input type="radio" name="sex" value="남">남 <input type="radio" name="sex" value="여">여 <br> <input type="submit" value="전송"> </form> </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.servletlec.ex; import java.io.IOException; import java.util.ArrayList; 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; /** * Servlet implementation class ServletParameterEx */ @WebServlet("/ExServlet") public class ServletParameterEx extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletParameterEx() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); request.setCharacterEncoding("EUC-KR"); String id = request.getParameter("id_info"); String pw = request.getParameter("pw_info"); String name = request.getParameter("name_info"); String[] hobby = request.getParameterValues("hobby"); String sex = request.getParameter("sex"); System.out.println("id : " + id); System.out.println("pw : " + pw); System.out.println("name : " + name); System.out.println("sex(남/여) : " + sex); System.out.println("hobby : " + Arrays.toString(hobby)); } } | cs |
결과값 :
한글이 꺠질 경우 처리 방법 : http://chrismare.tistory.com/31?category=1019271