본문 바로가기

프로잭트(일기장)/회사 공부 자료

[java] HttpServletRequest req, HttpServletResponse res

* HttpServletRequest req 요청 

- 요청 받는 방법 :  req.getParameter("name");

 

* HttpServletResponse res / 응답 (http 프로토콜 통신(cliant <-> server ))

 

res.setCharacterEncoding("UTF-8"); - 인코딩

 

res.setContentType("application/json"); - type

 

res.getWriter().write(sb.toString()); -여기서 종료  (cliant <-> server 종료)

 

============================================================================

 

웹브라우저에 url을 입력하고 엔터를 치면 도메인과 포트번호를 이용해 서버에 접속

 

패스정보, 클라이언트의 다양한 정보를 포함한 요청정보를 서버에 전달한다.

 

클라이언트로부터 요청이 들어오면 WAS는 HttpServletRequest, HttpServletResponse 객체를 생성

요청정보에 있는 패스로 매핑된 서블릿에게 전달한다.

이렇게 전달받은 내용들을 파라미터로 doGet, doPost등에 전달된다.

 

 

요청과 응답

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

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

 

HttpServletRequest

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

- request 객체는 웹 브라우저에서 JSP 페이지로 전달되는 정보의 모임으로 HTTP 헤더와 HTTP 바디로 구성되어 있다.

- 웹 컨테이너는 요청된 HTTP 메시지를 통해 HttpServletRequest 객체를 얻어내서, 이로부터 사용자의 요구사항을얻는다

- request 내부 객체의 요청 파라미터 관련 메소드

 

 메소드

설명 

String getParameter(name) 

파라미터 변수 name에 저장된 변수를 얻어내는 메소드로, 이때 변수의 값은 String으로 리턴된다. 

String[] getParameterValues(name) 

파라미터 변수 name에 저장된 모든 변수값을 얻어내는 메소드로, 이때 변수의 값은 String 배열로 리턴된다. checkbox에서 주로 사용된다.

Enumeration getParameterNames() 

요청에 의해 넘어오는 모든 파라미터 변수를 java.util.Enumeration 타입으로 리턴한다. 

- request 내장 객체의 웹 브라우저, 웹 서버 및 요청 헤더의 정보 관련 메소드

메소드

설명 

String getProtocol() 

 웹 서버로 요청 시, 사용 중인 프로토콜을 리턴한다.

String getServerName() 

웹 서버로 요청 시, 서버의 도메인 이름을 리턴한다. 

String getMethod() 

웹 서버로 요청 시, 요청에 사용된 요청 방식(GET, POST, PUT 등)을 리턴한다. 

String getQueryString() 

웹 서버로 요청 시, 요청에 사용된 QueryString을 리턴한다. 

String getRequestURI() 

웹 서버로 요청 시, 요청에 사용된 URL 로부터 URI 값을 리턴한다. 

String getRemoteAddr() 

웹 서버로 정보를 요청한 웹 브라우저의 IP주소를 리턴한다. 

int getServerPort() 

웹 서버로 요청 시, 서버의 Port번호를 리턴한다. 

String getContextPath() 

해당 JSP 페이지가 속한 웹 어플리케이션의 콘텍스트 경로를 리턴한다. 

String getHeader(name) 

웹 서버로 요청 시, HTTP 요청 헤더(header)의 헤더 이름인 name에 해당하는 속성값을 리턴한다. 

Enumeration getHeaderNames() 

웹 서버로 요청 시, HTTP 요청 헤더(header)에 있는 모든 헤더 이름을 리턴한다. 

 

- HttpServletResponse

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

 

ServletResponse interface

(javax.servlet.ServletResponse)

  • getBufferSize()
  • setContentType()
  • getOutputStream()
  • getWriter()
  • setContentLength()
  • 기타 메소드...

HttpServletResponse interface

(javax.servlet.http.HttpServletResponse)

  • addCookie()
  • addHeader()
  • encodeURL()
  • sendError()
  • setStatus()
  • sendRedirect()
  • 기타 메소드...

- ServletRequest interface 객체

https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletRequest.html

 

ServletRequest (Servlet API Documentation)

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher. Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and

tomcat.apache.org

- HttpServletRequest interface 객체

https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequest.html

 

HttpServletRequest (Servlet API Documentation)

Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters. Because this method returns a StringBuffer, not a string, you can mo

tomcat.apache.org

 

- HttpServletResponse interface 객체

https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html

 

HttpServletResponse (Servlet API Documentation)

 void setStatus(int sc, java.lang.String sm)           Deprecated. As of version 2.1, due to ambiguous meaning of the message parameter. To set a status code use setStatus(int), to send an error with a description use sendError(int, String). Sets the statu

tomcat.apache.org

 

- ServletResponse interface 객체

https://docs.oracle.com/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/ServletResponse.html

 

: Interface ServletResponse

Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize. A larger buffer allows more content to be written be

docs.oracle.com

 

- 클라이언트의 요청과 관련된 정보와 동작을 가지고 있는 객체

https://m.blog.naver.com/PostView.nhn?blogId=islove8587&logNo=220349614028&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

HTTPServletRequest 메소드 정리

설명- 클라이언트의 요청과 관련된 정보와 동작을 가지고 있는 객체 1. local의 기본 정보(IP, Name, P...

blog.naver.com