본문으로 바로가기

request 객체의 이해



웹브라우저를 통해 서버에 어떤 정보를 요청하는 것을 request라고 합니다. 

이러한 요청되어지는 정보request객체가 관리 한다.



request

웹브라우저 ---------> 서버

               <--------- 

response



              


request객체 관련 메소드()

getContextPath() : 웹어플리케이션의 컨텍스트 패스를 얻습니다.(프로젝트만)


http://localhost/Hworld/hworld.jsp인경우

/Hworld 경로만 얻는다. 



getMethod() : get방식과 post방식을 구분할 수 있습니다.

getSession() : 세션 객체를 얻습니다.

getProtocol() : 해당 프로토콜을 얻습니다.

getRequestURL() : 요청 URL를 얻습니다.

getRequestURI() : 요청 URI를 얻습니다. (프로젝트와 파일경로까지)

http://localhost/Hworld/hworld.jsp인경우 

/Hworld/hworld.jsp 경로 




ex) 
<%

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 />");



%>



parameter 메소드


앞에서 살펴본 요청관련 메소드 보다 실제 많이 쓰이는 메소드는 parameter와 관련된 메소드들입니다.

jsp페이지를 제작하는 목적이 데이터 값을 전송하기 위해서 이므로, parameter 관련 메소드는 중요하다.



getParameter(String name) :  name에 해당하는 파라미터 값을 구함

getParameterNames() : 모든 파라미터 이름을 구함. 

getParameterValues(String name) :  name에 해당하는 파라미터값을 구함. 스트링 배열로 배열 값을 출력가능


html파일에서 jsp파일로 넘어간 값들


response 객체 관련 메소드


웹브라우저 요청에 응답하는것을 response라고 하며, 이러한(response)정보를 가지고 있는 객체를(response)객체 라고 한다. 



getCharacterEncoding() : 응답할때 문자의 인코딩 형태를 구합니다.

addCookie(Cookie) : 쿠키를 지정 합니다.

sendRedirect(URL) : 지정한 URL로 이동합니다.



requestex.html

<form action="request_send.jsp">

당신의 나이는 :<input type = "text", name = "age", size="5" > </br>

<input type = "submit", value="전송">

</form>


request_send.jsp


<%@page import="org.omg.PortableServer.RequestProcessingPolicyValue"%>

<%@page import="jdk.nashorn.internal.ir.RuntimeNode.Request"%>

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<%!

int age;

%>


<%

String str = request.getParameter("age");

age = Integer.parseInt(str);



if(age>=20){

response.sendRedirect("pass?jsp=" + age);

}

else

{

response.sendRedirect("ng?jsp=" + age);

}



%>

</body>

</html>



pass.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>


<%!

int age;

%>


<%

String str = request.getParameter("age");

age = Integer.parseInt(str);

%>


성인 입니다. 주류구매가 가능 합니다.


<a href="requestex.html">처음으로 이동</a>   // 이동할 페이지 주소

</body>

</html>



ng. jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>


<%!

int age;

%>


<%

String str = request.getParameter("age");

age = Integer.parseInt(str);

%>


미성년자 입니다. 주류구매가 불가능 합니다.


<a href="requestex.html">처음으로 이동</a>

</body>

</html>