본문 바로가기
개발이야기/웹_백엔드

15) Request, Response 객체 이해하기

by 효우너 2020. 7. 29.
728x90
반응형

* 이 글은 부스트코스 웹 백엔드 강의 수강 바탕으로 작성되었습니다.

Request, Response

요청과 응답

WAS는 웹 브라우저로부터 Servlet요청을 받으면,

  • 요청할 때 가지고 있는 정보를 HttpServletRequest객체를 생성하여 저장합니다.
  • 웹 브라우저에게 응답을 보낼 때 사용하기 위하여 HttpServletResponse객체를 생성합니다.
  • 생성된 HttpServletRequest, HttpServletResponse 객체를 서블릿에게 전달합니다.

 

HttpServletRequest

  • http프로토콜의 request정보를 서블릿에게 전달하기 위한 목적으로 사용합니다.
  • 헤더정보, 파라미터, 쿠키, URI, URL 등의 정보를 읽어 들이는 메소드를 가지고 있습니다.
  • Body의 Stream을 읽어 들이는 메소드를 가지고 있습니다.

 

HttpServletResponse

  • WAS는 어떤 클라이언트가 요청을 보냈는지 알고 있고, 해당 클라이언트에게 응답을 보내기 위한 HttpServleResponse객체를 생성하여 서블릿에게 전달합니다.
  • 서블릿은 해당 객체를 이용하여 content type, 응답코드, 응답 메시지등을 전송합니다.

실습코드

HeaderServlet.java

package examples; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Enumeration; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** * Servlet implementation class HeaderServlet */

@WebServlet("/header") 
public class HeaderServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 

/** * @see HttpServlet#HttpServlet() */ 
public HeaderServlet() { 
super(); 
// TODO Auto-generated constructor stub 
} 

/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
out.println("<html>"); 
out.println("<head><title>form</title></head>"); 
out.println("<body>"); 
Enumeration<String> headerNames = request.getHeaderNames(); 
while(headerNames.hasMoreElements()) { 
String headerName = headerNames.nextElement(); 
String headerValue = request.getHeader(headerName); 
out.println(headerName + " : " + headerValue + " <br> "); 
} out.println("</body>"); out.println("</html>"); } 

/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
// TODO Auto-generated method stub doGet(request, response); 
} 
}

실습코드

ParameterServlet.java

package examples; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** * Servlet implementation class ParameterServlet */ 
@WebServlet("/param") 
public class ParameterServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 

/** * @see HttpServlet#HttpServlet() */ 
public ParameterServlet() { 
super(); 
// TODO Auto-generated constructor stub } 

/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
out.println("<html>"); 
out.println("<head><title>form</title></head>"); 
out.println("<body>"); 
String name = request.getParameter("name"); 
String age = request.getParameter("age"); 
out.println("name : " + name + "<br>"); 
out.println("age : " +age + "<br>"); 
out.println("</body>"); 
out.println("</html>"); 
} }

실습코드

InfoServlet.java

package examples; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** * Servlet implementation class InfoServlet */ 
@WebServlet("/info") 
public class InfoServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 

/** * @see HttpServlet#HttpServlet() */ 
public InfoServlet() { 
super(); 
// TODO Auto-generated constructor stub 
} 

/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
out.println("<html>"); 
out.println("<head><title>info</title></head>"); 
out.println("<body>"); 
String uri = request.getRequestURI(); 
StringBuffer url = request.getRequestURL(); 
String contentPath = request.getContextPath(); 
String remoteAddr = request.getRemoteAddr(); 
out.println("uri : " + uri + "<br>"); 
out.println("url : " + url + "<br>"); 
out.println("contentPath : " + contentPath + "<br>"); 
out.println("remoteAddr : " + remoteAddr + "<br>"); 
out.println("</body>"); out.println("</html>"); 
} }
728x90
반응형

'개발이야기 > 웹_백엔드' 카테고리의 다른 글

17) JSP 라이프싸이클  (0) 2020.08.02
16) JSP란?  (0) 2020.07.30
14) Servlet 라이프 싸이클  (0) 2020.07.26
13) Servlet 이란?  (0) 2020.07.24
12) WAS  (0) 2020.07.24

댓글