본문 바로가기
Today I learned

HttpServletXxx 관련 메서드 Cheet Sheat

by soheemon 2019. 7. 16.
HttpServletXxx 메서드 CheetSheat

* HTTP Header를 읽기위한 메서드의 종류
HttpServletRequest객체의 메서드들이다.
1) Cookie[] getCookies()
Client가 Request객체에 포함시킨 쿠키배열을 반환한다.
2) Enumeration getAttributeNames()
Client가 Request객체에 포함시킨 attributes들을 반환한다.
3) Enumeration getHeaderNames()
Request객체에 포함된 header들의 이름을 반환한다.
4) Enumeration getParameterNames()
Client가 Request객체에 포함시킨 Parameter들의 이름을 반환한다.
5) HttpSession getSession()
Request가 연관된 session객체를 반환한다. Request가 Session을 가지고 있지 않다면 생성후 반환한다.
6) HttpSession getSession(boolean create)
Request가 연관된 session객체를 반환한다. Request가 Session을 가지고 있지 않거나, 매개변수가 true라면 새로운 세션을 반환한다.
7) Locale getLocale()
Request header중 Accept-Language header를 참고하여, 적절한 Locale을 반환한다(?)
8) Object getAttribute(String name)
*밑에 참고

9) ServletInputStream getInputStream()
ServletInputStream을 사용하여 Request Body를 BinaryData로 가져옵니다.
10) String getAuthType()
servlet에서 사용중인 보안scheme의 이름을 반환한다. BASIC or SSL 혹은 사용하지 않을경우엔 null
11) String getCharacterEncoding()
Request의 Body에서 사용중인 인코딩 방식을 반환한다.
12) String getContentType()
Request body의 MIME Type을 전송한다. 알수 없다면(헤더에서 빠져있다면?) null을 반환한다.
13) String getContextPath()
요청경로중에서, host와 fileName을 제외한 Path만을 반환한다.
예) https://example.com/list/test.jsp -> /list
14) String getHeader(String name)
Request헤더 중 key값이 name에 해당하는 헤더값을 반환한다.
15) String getMethod()
Request가 지정한 HTTP Method를 반환한다.
16) String getParameter(String name)
Request에서 name에 해당하는 파라미터를 반환한다. 없으면 null
17) String getPathInfo()
서블릿 Foo가 URI '/Foo'에 매핑되는경우..
Request URI가 /Foo/Path/To/Resource라면..
PathInfo는.. 서블릿과 맵핑된 /Foo이후의 Path 즉, /Path/to/Resource만을 반환한다.

* HTTP Response Header를 읽기위한 메서드의 종류
HttpServletResponse객체의 메서드들이다.
1) String encodeRedirectURL(String url)
SendRedirect메서드의 인자로 사용하며 Redirect 할때 URL을 인코딩한다.
2) String encodeURL(String url)
Session ID를 포함하여 URL을 인코딩한다.
3) boolean containsHeader(String name)
Response객체에 name이라는 헤더가 있는지 없는지를 반환한다.
4) boolean isCommitted()
Response객체가 Client쪽으로 전송됐는지 여부를 반환한다.
5) void addCookie(Cookie cookie)
Response객체에 쿠키를 add한다.
6) void addDataHeader(String name, long date)
헤더에 name과 data-value를 더한다.
7) void AddIntHeader(String name, int value)

* 참고
Parameter와 Attribute의 차이
0)데이터 전송 방향
Parameter : Client데이터를 Server로 전송하는데 사용
Attribute : 양방향 전부 가능

1)데이터 타입
parameter : String
Attribute : Object (따라서, 캐스팅 필요.)

추측하기로는 parameter는 String이기때문에 타입캐스팅 필요없이 바로 Map으로 파싱하여 사용할 수 있는 반면...
Attribute는 Object이기때문에 내부에서 직렬화 작업이 이루어지고, 그래서 타입캐스팅이 필요한듯 하다.
아마 String데이터를 Attribute로 전송시 Request메시지를 열어보면... 직렬화로 인해 바이너리형태로 알아 볼 수 없겠지만...
같은데이터를 Parameter로 전송한다면 Request메시지의 Body(혹은 쿼리스트링영역)에서 key & Map 으로 알아볼 수 있는 형태일듯 하다.
(따라서 원래의 설계 목적대로라면 Client에서 Server로 보내는 String 데이터는 Parameter를 사용하는게 자연스러운듯 하다.)

2)Scope
Parameter : Request
Attribute : application, session, request, page

* 메서드 실행 결과
출처:
https://stackoverflow.com/questions/4931323/whats-the-difference-between-getrequesturi-and-getpathinfo-methods-in-httpservl
Servlet is mapped as /test%3F/* and the application is deployed under /app.
example)

http://30thh.loc:8480/app/test%3F/a%3F+b;jsessionid=S%3F+ID?p+1=c+d&p+2=e+f#a

Method              URL-Decoded Result          
----------------------------------------------------
getContextPath()        no      /app
getLocalAddr()                  127.0.0.1
getLocalName()                  30thh.loc
getLocalPort()                  8480
getMethod()                     GET
getPathInfo()           yes     /a?+b
getProtocol()                   HTTP/1.1
getQueryString()        no      p+1=c+d&p+2=e+f
getRequestedSessionId() no      S%3F+ID
getRequestURI()         no      /app/test%3F/a%3F+b;jsessionid=S+ID
getRequestURL()         no      http://30thh.loc:8480/app/test%3F/a%3F+b;jsessionid=S+ID
getScheme()                     http
getServerName()                 30thh.loc
getServerPort()                 8480
getServletPath()        yes     /test?
getParameterNames()     yes     [p 2, p 1]
getParameter("p 1")     yes     c d

* 유익한 stackoverflow 링크 정리
- 서블릿 동작
https://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-sessions-shared-variables-and-multithreadi

댓글