카테고리 없음
11강-JSP본격3(request, response객체)
쩔쩔
2020. 6. 22. 01:08
1.request객체
웹 브라우저를 통해 서버에 어떤 정보를 요청하는 객체.
-request객체 관련 메소드
<body>
<%
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 />");
%>
</body>
더보기
서버 : localhost
포트번호 : 8181
요청방식 : GET
프로토콜 : HTTP/1.1
URL : http://localhost:8181/ex8/declaration.jsp
URI : /ex8/declaration.jsp
-parameter 메소드
JSP페이지를 제작하는 목적이 데이터 값을 전송하기 위함 이므로, parameter관련 메소드들이 중요함.
getParameter(String name) : name에 해당하는 파라미터 값을 구함.
getParameterName() : 모든 파라미터 이름을 구함.
getParameterValues(String name) : name에 해당하는 파라미터값들을 구함.
예제)
<body>
<form action = "active.jsp" method="post">
이름 : <input type = "text" name = "name"><br />
아이디 : <input type = "text" name = "id"><br />
비밀번호 : <input type = "password" name = "password"><br />
취미 : <input type = "checkbox" name = "hobby" value="cook">요리
<input type = "checkbox" name = "hobby" value="book">독서
<input type = "checkbox" name = "hobby" value="jog">조깅<br />
전공 : <input type = "radio" name = "major" value = "kor">국어
<input type = "radio" name = "major" value = "eng" checked="checked">영어
<input type = "radio" name = "major" value = "math" >수학<br />
<select name="protocol">
<option value = "http">http</option>
<option value = "ftp" selected="selected">ftp</option>
</select>
<input type = "submit" value="전송">
<input type = "reset" value="초기화">
</form>
</body>
<body>
<%!String name, id, password, major, protocol;
String[] hobbys;%>
<%
request.setCharacterEncoding("EUC-KR");
name = request.getParameter("name");
id = request.getParameter("id");
password = request.getParameter("password");
major = request.getParameter("major");
protocol = request.getParameter("protocol");
hobbys = request.getParameterValues("hobby");
%>
이름 : <%=name %><br />
아이디 : <%=id %><br />
비밀번호 :<%=password %><br />
취미 :<%=Arrays.toString(hobbys) %><br />
전공 :<%=major %><br />
프로토콜 :<%=protocol %><br />
</body>
2.response 객체
-response객체 관련 메소드
getCharacterEncoding() : 응답할 때 문자의 인코딩 형태를 구함.
addCookie(Cookie) : 쿠키를 지정
sendRedirect(URL) : 지정된 URL 이동
예제)
<body>
<form action="request_send.jsp" method="post">
나이 : <input type="text" name="age"><br />
<input type="submit" value="제출">
</form>
</body>
request_send.jsp
<body>
<% String age;
int age_int;
age = request.getParameter("age");
age_int = Integer.parseInt(age);
if(age_int >= 20)
response.sendRedirect("pass.jsp?age="+age);
else
response.sendRedirect("ng.jsp?age="+age);
%>
</body>
<body>
<%
String age = request.getParameter("age");
%>
나이가 <%=age %>이므로 주류 구매가 가능함.
<a href="requestex.html">처음으로 이동</a>
</body>