JSP 내장객체, 요청객체, 응답객체, 페이지 디렉티브의 주요속성, 요청방식별 값 전송방법

2019. 4. 29. 19:39웹(JSP)

-----------------------------------JSP 내장객체------------------------------

JSP 내장객체   (*)-중요
            - pageContext
                        PageContext
                        JSP 페이지에 대한 정보를 담고 있는 객체 <-- 디렉티브(지시어)로 설정한 정보를 담고 있다.
                        request와 response를 제외한 나머지객채는 pageContext로부터 얻어낸다.
            - (*)request
                        HttapServletRequest
                        클라이언트가 서버로 전송한 요청 메세지를 보관하는 객체다.
                        요청메세지 정보를 조회할 수 있는 다양한 메소드를 제공한다.
            - (*)response
                        HttpServletResponse
                        서버가 클라이언트로 보내는 응답 메세지를 담당하는 객체다.
                        응답메세지를 설정하기위한 다양한 메소드를 제공한다.
            - (*)session
                        HttpSession
                        연결을 요청한 클라이언트를 식별하기 위한 정보를 보관하는 객체다.
                        로그인처리와 관련된 작업에 활용되는 객체다.
            - application
                        ServletContext
                        웹 어플리케이션에 대한 정보를 보관하는 객체다.
                        웹 어플리케이션마다 하나씩 생성되는 객체다.
            - out
                        JspWriter
                        JSP페이지에서 생성한 동적컨텐츠 (HTML문서)를 클라리언트로 출력할 때 사용하는 스트임이다.
            - config
                        ServletConfig
                        JSP페이지와 관련된 초기화파라미터 값을 보관하는 객체다.
            - page
                        Object
                        JSP를 자바클래스로 변환한 후 생성한 객체가 보관되어 있다.
            - exception
                        java.lang.Throwable
                        에러정보를 보관하는 객체다.
                        에러페이지로 지정된 JSP에만 내장되어 있다.


요청객체
            HttpServletRquest
            주요 메소드
                        요청파라미터 조회
                                    String                        getParameter(String name)
                                                                   지정된 이름의 요청 파라미터값 조회
                                    String[]                      getParametervalues(String name)
                                                                   지정된 이름의 요청 파라미터값을 전부 조회
                                    Map    getParameterMap()
                                                                    요청파라미터정보를 Map에 담아서 제공
                        요청 정보 조회
                                    String                        getMethod()
                                                                   요청방식 (GET, POST)을 조회
                                    String                        getRequestURI()
                                                                   요청 URI를 조회한다.
                                                                   예) http://localhost/hta/board/detail.jsp?no=23
                                                                        요청 URI => /jta/board/detail.jsp
                        클라이언트에 대한 정보 조회
                                    String                        getHeader(String name)
                                                                    클라이언트의 ip주소를 조회
                                    String                        getContectType()
                                                                    클라이언트가 서버로 보낸 컨텐츠(message body에 포함된)의 타입을 조회
                                    long                          getContentLength()
                                                                    클라이언트가 서버로 보낸 컨텐츠의 길이를 조회
                                    Locale                        getLocale()
                                                                    클라리언트의 지역화정보를 조회
                        기타 (좀 기억해두기)
                                    void                          setCaracterEncoding(String charset)
                                                                   요청파라미터의 문자인코딩 방식을 지정한다.
                                    requestDispatcher         getRequestDispatcher(Streing path)
                                                                   클라이언트의 요청을 지정된 경로와 맵핑된 다른
                                                                   웹애플리케이션으로 이동시키는 객체를 반환한다.
응답객체
            HttpServletResponse
                        응답컨텐크 관련 메소드
                                    void                        setContentType(String contentType)
                                                                 응답 컨텐츠의 타입을 설정한다.
                                    void                        setContentLength(long length)
                                                                 응답 컨텐츠의 길이를 설정한다.
                                    void                        setStatus(int statusCode)
                                                                 HTTP 응탑코드를 설정한다. 예), 404, 500 등
                        응답헤더 관련 메소드
                                    void                        setHeader(String name, String value)                                    
                                                                 응답헤더정보를 설정한다.
                        기타
                                    void                        sendRedirect(String path)
                                                                 브라우져에게 재요청할 경로를 응답으로 보낸다.


페이지 디렉티브의 주요속성
            contentType
                        <%@ page contentType="text/html;charset=utf-8" %>
                        jsp 실행경과로 생성된 동적 컨텐츠의 타입을 정의한다.
                        응답으로 제공되는 텍스트의 문자 인코딩방식도 같이 지정한다.

            pageEncoding                        
                        <%@ page pageEncoding="utf-8" %>
                        jsp 소스파일을 저장할 때 문자 인코딩방식을 지정한다.
            
            import
                        <%@ page import="java.util.ArrayList" %>
                        <%@ page import="java.util.ArrayList, java.util.HashSet" %>
                        <%@ page import="kr.co.hta.vo.*" %>
                        jsp 페이지에서 사용할 자바클래스를 지정한다.                        

            isErrorPage
                        <%@ page isErrorPage="true" %>
                        해당 JSP를 에러 페이지로 지정한다.

            errorPage
                        <%@ page errorPage="error/servererror.jsp" %>
                        jsp에서 에러가 발생했을 때 표시할 jsp를 지정한다.
                        ※. 때에따라 errorPage에서 지정된 페이지는 isErrorPage가 
                            true로 설정된 페이지여야 한다.

            session
                        <%@ page session="false" %>
                        jsp페이지가 HttpSession내장객체를 사용할지 여부를 지정한다.
                        true면 사용하고, false면 사용하지 않는다.
                        
            trimDirectiveWhiteSpace
                        <%@ page trimDirectiveWhiteSpace="true" %>
                        디렉티브 선언으로 발생되는 공백을 제거할지 여부를 지정한다.






예시) 







요청방식별 값 전송방법

            GET
                        URL뒤에 쿼리스트링으로 값을 전달
                        http://localhost/detail.jsp?no=12
                        http://localhost/search.jsp?opt=title&keyword=자바

                        --------------------------------------------------------------------
                        | GET search.jsp?opt=title&keyword=자바  HTTP/1.1          |
                        | 요청헤더 . . .                                                            | Request Header
                        | 요청헤더 . . .                                                            |
                        ---------------------------------------------------------------------

                        String 번호 = request.getParameter("no");
                        String 옵션 = request.getParameter("opt");
                        String 키워드 = request.getParameter("keyword");

            POST
                        요청메세지의 body에 값을 포함시켜서 전달
                        --------------------------------------------------------------------
                        | POST add.jsp HTTP/1.1                                              .|
                        | 요청헤더 . . .                                                            | Request Header
                        | 요청헤더 . . .                                                            |
                        --------------------------------------------------------------------
                        --------------------------------------------------------------------
                        | name=홍길동&pwd=zxcv1234&add=서울특별시+종로구  .| Request Body
                        --------------------------------------------------------------------

                        String 번호 = request.getParameter("no");
                        String 옵션 = request.getParameter("opt");
                        String 주소 = request.getParameter("addr");




예시)