은은하게 코드 뿌시기

[jsp 내부객체] - request, response, out 내부객체/ HttpServlet/HttpServletRequest/HttpServletResponse 본문

웹/JSP

[jsp 내부객체] - request, response, out 내부객체/ HttpServlet/HttpServletRequest/HttpServletResponse

은은하게미친자 2022. 8. 8. 23:32
728x90

request, response, out 내부객체 는 jsp 페이지 입출력과 관련한 내부객체 입니다.

 

1-0 : HttpServlet

: HTTP 프로토콜을 사용하는 웹 브라우저에서 서블릿 기능을 수행한다.
따라서 개발자는 HttpServlet을 상속받아 많은 기능을 사용할 수 있다.
WAS가 웹브라우저로부터 Servlet 요청을 받으면
요청 받을 때 전달 받은 정보를 HttpServletRequest 객체를 생성해서 저장한다.
웹브라우저에 응답을 반환할 HttpServletResponse 객체를 생성한다. (응답을 담기 전 빈 객체)
생성된 HttpServletRequest, HttpServletResponse 객체를 Servlet에 전달한다."

 

1-1. request :  HttpServletRequest

HttpServletRequest

: HTTP 요청 정보(클라이언트 요청, 쿠키, 세션 등)를 제공하는 인터페이스
HTTP 프로토콜의 request 정보를 서블릿에게 전달하기 위한 목적으로 사용한다.
Message Body의 Stream을 읽어들이는 메서드를 가지고 있다.

 

request

: 브라우저에서 jsp페이지로 전달되는 데이터의 묶음, http 헤더와 http 바디로 구성 되어있습니다.

jsp 컨트롤러는 요청된 http 메세지를 통해 httpserveletrequest 객체 타입의 request 객체명으로 사용합니다.

 

* request 객체의 요청 메소드

메소드 설명
String getParameter(name) name에 할당된 값을 반환하며 지정된 파라미터 값이 없으면 null값을 반환
String[] getParameterValues(name) name의 모든 값을 String 배열로 반환합니다.
Enumeration getParameterNames() 요청에 사용된 모든 파라 미터 이름을 javautil.Enumeration 타입으로 반환합니다.
void setCharacterEncoding(env) post 방식으로 요청된 문자열의 character encoding을 설정합니다.

? parameter attribute 차이?

파라미터 : 클라이언트(사용자) 요청에서 넘어온 값

어트리뷰트 : 서버(개발자) 가 코딩으로 지정한 값

 

* request 내부 객체의 요청 파라미터 메소드

메소드   설명
getMethod() 요청에 사용된 요청방식(get,  post, put)을 반환합니다.
getRequestURI() 요청에 사용된 URL로 부터 URI를 반환
getQueryString() 요청에 사용된 Query 문장을 반환
getRemoteHost() 클라이언트의 호스트 이름을 반환합니다.
getRemoteAddr() 클라이언트의 주소를 반환합니다.
getProtocol() 사용중인 프로토콜을 반환합니다.
getServerName() 서버의 도메인 이름을 반환합니다.
getServerPort() 서버의 주소를 반환합니다.
getHeader(name) HTTP 요청 헤더에 지정된 name의 값을 반환합니다.

2-1. response : httpServeletResponse

httpServeletResponse

: HTTP 응답 정보(요청 처리 결과)를 제공하는 인터페이스
Servlet은 HttpServletResponse객체에 content-type, 응답 코드, 응답 메세지 등을 담아서 전송한다.
Servlet으로 들어온 요청은 텍스트(HTML)로 응답을 보내기 때문에 출력 스트림을 받기 위해 주로 response로부터 writer 객체를 얻어서 내보낸다.

reponse

: 요청을 시도한 클라이언트로 전송할 응답을 나타내는 데이터 묶음, jsp 컨테이너는 요청된 http 메세지를 통해 httpServeletResponse 객체 타입으로 사용됩니다.

 

*response 내부 객체의 메소드

메소드 설명
void setHeader(name, value) 응답에 포함될 header를 설정합니다.
void setContentType(type) 출력되는 페이지의 contentType을 설정합니다.
String getCharacterEncoding() 요청에 사용된 Query 문장을 반환합니다.
void sendResdirect(url) 지정된 URL로 요청을 재전송 합니다.

2-2. OUT

: jsp 페이지의 결과를 클라이언트에 전송주는 출력 스트림

<%
out.println("출력이됩니다")
;%>

*out 내부 객체의 메소드

메소드  설명
boolean isAutoFlush() 출력 버퍼가 다 채워져 자동으로 flush헀을 경우는 true반환, 그렇지 않은 경우는 false 반환
int getBufferSize() 출력 버퍼의 전체 크기를 바이트 단위로 반환합니다.
int getRemaining() 출력 버퍼의남은 양을 바이트 단위로 반환합니다.
void clearBuffer() 현재 출력 버퍼에 저장된 내용을 취소합니다.
String println(string) string을 브라우저에 출력합니다.
void flush() 현재 출력 버퍼의 내용을 flush하여 클라이언트로 전송합니다.
void close() 춝력 버퍼의 내용을 flush하고 스트림을 닫습니다.

 

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
<form action="practice_request_02.jsp" method="get">
 
<% 
    request.setAttribute("pgname", "페이지이름");
    request.setAttribute("attr", "어트리뷰트");
    request.setAttribute("msg", "꿈은이루어진다");
    request.setAttribute("msg2", "hellohello");
    
    request.setCharacterEncoding("utf-8");
%>
 
<%=request.getAttribute("msg")%>
${msgFFFF}<br>
${pgname}<br>
${attr}<br>
${msg}<br>
${msg2}<br>
<br>
    <input type="text" name="hello1"><br>
    <input type="text" name="hello2"><br>
    <input type="submit" value="보내기"><br>
</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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<% 
    request.setCharacterEncoding("utf-8");
%>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
받은 페이지
<% out.println("뿅"); %><br>
 
<br>
 
 
${param.hello1}<br>
${param.hello2}<br>
<br>
<br>
 
 
</body>
</html>
cs

 

 

 

728x90
Comments