-
[JSP] 내장객체back end 2022. 3. 5. 15:25
내장 객체
: 변수 선언 및 객체 생성과정 없이 바로 사용할 수 있는 객체를 의미한다.
내장 객체를 사용할 경우 프로그래밍을 최대한 단순화시킬 수 있다.
입출력 관련 내장 객체
- request : javax.servlet.http.HttpServletRequest (브라우저 -> jsp로 요청할 때 전달되는 데이터를 리턴)
- response : javax.servlet.http.HttpServletResponse
- out : javax.servlet.jsp.JspWrite ( Jsp 페이지의 결과를 브라우저로 리턴하는 출력 스트림 )
HttpServletRequest 객체 API
https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html
HttpServletRequest (Servlet {servlet.spec.version} API Documentation - Apache Tomcat 9.0.59)
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
CRUD 할 수 있는 메서드는 찾아놓는게 좋다!
s가 붙은 메서드는 배열로 리턴해준다.
실습예제) Web05/test01/requestPath.jsp, requestHeader.jsp
HttpServletResponse
목적: 현재 페이지의 제어권(request, response)을 다음 페이지로 넘길 때 사용한다. 서블릿에서 많이 사용.
sendError(), sendRedirect()
실습예제) Web05/test01/redirectex.jsp
HttpServletResponse API
https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html
HttpServletResponse (Servlet {servlet.spec.version} API Documentation - Apache Tomcat 9.0.59)
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the loca
tomcat.apache.org
Out
: Jsp 페이지의 결과를 브라우저로 리턴하는 출력 스트림
out.clearbuffer(), out.flush()
실습예제)Web05/test01/outprint.jsp
JSP 외부 환경 정보 관련 내장 객체
: 웹 어플리케이션에서 수행되는 웹페이지 등의 환경정보를 구성
- session : javax.servlet.http.HttpSession (서버에 클라이언트 정보를 저장하지 위한 객체)
- application : javax.servlet.ServletContext (웹 어플리케이션 자체)
- pageContext: javax.servlet.jsp.PageContext (현재 요청된 JSP의 Context를 의미)
Session
클라이언트 정보를 세션이 관리한다. 지정한 시간동안 정보를 저장해놓는다.
실습예제) Web05/test01/sessionres.jsp, sessionresult.jsp, sessioninput.jsp
<% if(request.getMethod().equals("POST")) { if(session.isNew() || session.getAttribute("memberid")==null) { String id=request.getParameter("id"); String pw=request.getParameter("pw"); session.setAttribute("memberid",id); // 세션 객체에 담았다. session.setAttribute("memberpw",pw); session.setMaxInactiveInterval(60); // 60초 동안 가지고 있는다. %> 회원 인증 처리되었습니다 <br> <%=session.getAttribute("memberid") %> 님 안녕하세요<br> <% }else{ %> 회원인증할 필요가 없습니다 <br> 이미 인증 하셧습니다 <br> <% }} %>
setAttribute로 id, pw 저장하고, getAttribute로 출력한다.
60초 내에 다시 로그인하면 그 정보가 남아있어 인증을 할 필요가 없다는 문구를 출력해준다.
Application
실습예제) Web05/test01/applicationres.jsp
session을 이용한 페이지 접속 방문수는 해당 브라우저로 접속한 횟수를 말한다.
application을 이용한 페이지 접속 방문수는 이 화면의 총 방문수를 말한다.
Page Servlet에 관련된 내장객체
- config : javax.servlet.ServletConfig ( 서블릿의 초기 설정데이터_ 필터)
- page : javax.servlet.jsp.JspPage
- exception : javax.lang.Throwable (page 지시자 isErrorPage=true 속성으로 지정된 페이지에서만 사용할 수 있는 객체)
exception API
JVM -> a.class -> 중단 Throwable 객체 생성 ->
-> 중단된 class로 와서 NumFormatException을 줘서 코드 수정할 수 있게 한다?
객체의 범위
page, request, session, application으로 나누어 진다.
page 영역 : 서블릿 인스턴스의 _jspService() 메소드가 실행되는 동안에 유효한 영역
request 영역 : 클라이언트의 요청이 처리되는 동안에 유효한 영역
session 영역 : 세션이 유지되는 동안에 유효한 영역
application : 해당 context 가 유지되는 영역
'back end' 카테고리의 다른 글
[MVC] WebScore 프로젝트 (0) 2022.03.09 [MVC 패턴] DB연동 (0) 2022.03.08 [JSP] JSP 엔진, 지시어 (0) 2022.03.04 [Servlet] URL 맵핑, Servlet 객체, 생명주기 (0) 2022.03.03 [Error] HTTP Status 404 - Not Found (0) 2022.03.01