웹브라우저에서 웹서버로 요청을 보내는 일반적인 방법은 두가지가 있습니다. GET방식과 POST방식이 그것이죠.
RESTFUL에서는 더 많은 방식이 있지만 여기서는 GET 과 POST에 대해서만 JSP로 핸들링 하는 방법에 대해 알아보도록 하겠습니다.
웹브라우저에서 form데이타를 이용해서 요청을 보내면 JSP에서 그 form 데이터를 읽을 수 있습니다. 이때 사용할 수 있는 메소드들은 아래와 같습니다.
getParameter(): You call request.getParameter() method to get the value of a form parameter.
getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
getParameterNames(): Call this method if you want a complete list of all parameters in the current request.
getInputStream(): Call this method to read binary data stream coming from the client.
아래 URL은 get방식으로 데이터를 웹서버로 넘겨주고 있습니다. first_name이라는 변수에 "길동"라는 값이 들어있고 "홍"이라는 변수에 ALI라는 값이 들어있다고 생각하시면 됩니다.
http://localhost:8080/main.jsp?first_name=길동&last_name=홍
그리고 아래는 main.jsp 의 소스입니다. getParameter() 메소드를 이용해서 브라우저에서 넘겨준 데이터를 받아오는 기능입니다.
<html> <head> <title>Using GET Method to Read Form Data</title> </head> <body> <center> <h1>Using GET Method to Read Form Data</h1> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>
이제 웹브라우저에서 http://localhost:8080/main.jsp?first_name=길동&last_name=홍 을 타이핑해서 웹서버로 요청을 해보세요. 아래와 같은 결과가 나올 것입니다.
Using GET Method to Read Form Data
|
아래는 HTML 태그 중에서 form태그를 이용한 get방식 요청 예제입니다.
<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>
위 소스를 Hello.htm파일에 작성하고 <Tomcat-installation-directory>/webapps/ROOT 디렉토리에 넣어주세요.
http://localhost:8080/Hello.htm 를 웹브라우저에서 요청하면 아래처럼 화면에 보여집니다.
이제 빈박스에 이름을 입력하고 submit버튼을 눌러보세요. 버튼을 누르면 main.jsp에 데이터를 보내고 그 데이터를 main.jsp에서 처리합니다
처리된 결과는 위에서 실행한 결과와 동일하게 나올 것입니다.
Reference : http://www.tutorialspoint.com/jsp/jsp_form_processing.htm
[JSP] Cookies ( 쿠키 세팅, 읽기, 삭제하기) (0) | 2019.02.15 |
---|---|
[JSP] Filters ( 필터 사용하기 ) (0) | 2019.02.15 |
[JSP] Response Object ( Server Response, Auto Refresh ) (0) | 2019.02.15 |
[JSP] Request Object ( Client Request ) (0) | 2019.02.15 |
[JSP] Implicit Objects ( 묵시적 객체들 ) (0) | 2019.02.15 |