1. Request 객체
- request: 웹 브라우저를 통해 서버에 어떤 정보를 요청하는 것
- request 객체 : 요청(request) 정보를 가지고 있는 객체

1) 요청 관련 메소드

① JSP 파일
<%
out.println("서버 : " + request.getServerName() + "<br />");
out.println("포트 번호 : " + request.getServerPort() + "<br />");
out.println("요청 방식 : " + request.getMethod() + "<br />");
out.println("프로토콜 : " + request.getProtocol() + "<br />");
out.println("URL : " + request.getRequestURL() + "<br />");
out.println("URI : " + request.getRequestURI() + "<br />");
%>
② 웹 브라우저 출력 화면
서버 : localhost
포트 번호 : 8181
요청 방식 : GET
프로토콜 : HTTP/1.1
URL : http://localhost:8181/jsp_11_1_ex1_requestobj/requestobj.jsp
URI : /jsp_11_1_ex1_requestobj/requestobj.jsp
2) Parameter 메소드
- JSP 파일의 목적인 데이터 전송을 담당
- 제일 많이 쓰임

① HTML 파일의 FORM 구성
<form action="requestparam.jsp" method="post">
이름 : <input type="text" name="name" size="10"><br/>
아이디 : <input type="text" name="id" size="10"><br/>
비밀번호 : <input type="password" name="pw" size="10"><br/>
취미 : <input type="checkbox" name="hobby" value="read">독서
<input type="checkbox" name="hobby" value="cook">요리
<input type="checkbox" name="hobby" value="run">조깅
<input type="checkbox" name="hobby" value="swim">수영
<input type="checkbox" name="hobby" value="sleep">취침<br/>
전공 : <input type="radio" name="major" value="kor">국어
<input type="radio" name="major" value="eng" checked="checked">영어
<input type="radio" name="major" value="mat" >수학
<input type="radio" name="major" value="des" >디자인<br/>
<select name="protocol">
<option value="http">http</option>
<option value="ftp" selected="selected">ftp</option>
<option value="smtp">smtp</option>
<option value="pop">pop</option>
</select><br/>
<input type="submit" value="전송">
<input type="reset" value="초기화">
</form>
② HTML 파일의 웹 브라우저 출력 화면

③ FORM에서 데이터를 받는 JSP 파일
<%@page import="java.util.Arrays"%>
<%!
String name, id, pw, major, protocol;
String[] hobbys;
%>
<%
request.setCharacterEncoding("EUC-KR");
name = request.getParameter("name");
id = request.getParameter("id");
pw = request.getParameter("pw");
major = request.getParameter("major");
protocol = request.getParameter("protocol");
hobbys = request.getParameterValues("hobby");
%>
이름 : <%= name %><br />
아이디 : <%= id %><br />
비밀번호 : <%= pw %><br />
취미 : <%= Arrays.toString(hobbys) %><br />
전공 : <%= major %><br />
프로토콜 : <%= protocol %><br />
④ JSP 파일의 웹 브라우저 출력 화면
이름 : 김파람
아이디 : abcde
비밀번호 : 12334
취미 : [read, run]
전공 : des
프로토콜 : http
2. Reponse 객체
- response : 웹 브라우저의 요청에 응답하는 것
- response 객체 : 응답(response) 정보를 가지고 있는 객체


① requestex.html
<form action="request_send.jsp">
당신의 나이는 : <input type="text" name="age" size="5">
<input type="submit" value="전송">
</form>

② request_send.jsp
<%!
int age;
%>
<%
String str = request.getParameter("age");
age = Integer.parseInt(str);
if( age >= 20){
response.sendRedirect("pass.jsp?age=" + age);
} else {
response.sendRedirect("ng.jsp?age=" + age);
}
%>
<%= age %>
③ pass.jsp (입력값이 20살 이상인 경우)
<%!
int age;
%>
<%
String str = request.getParameter("age");
age = Integer.parseInt(str);
%>
성인 입니다. 주류구매가 가능 합니다.
<a href="requestex.html">처음으로 이동</a>

③ ng.jsp (입력값이 20살 미만인 경우)
<%!
int age;
%>
<%
String str = request.getParameter("age");
age = Integer.parseInt(str);
%>
미성년자 입니다. 주류구매가 불가능 합니다.
<a href="requestex.html">처음으로 이동</a>

'강의 정리 > JSP & Servlet (Seoul Wiz)' 카테고리의 다른 글
13. 쿠키 (0) | 2019.08.06 |
---|---|
12. 액션태그 (0) | 2019.08.06 |
10. JSP (2) (0) | 2019.08.05 |
9. JSP (1) (0) | 2019.08.04 |
8. Servlet (4) (0) | 2019.08.03 |