JSP(introduction~ 9 implicit objects)

꿈나무기록장·2021년 1월 20일
0

2021웹캠프정리

목록 보기
18/25

JSP Scripting Elements

  1. JSP scripting tag
    scripting tag는 jsp 내부에 자바 코드를 삽입하는 기능을 제공

scripting elements 종류

  • scriptlet tag
    : jsp에서 자바 소스 코드를 실행하는 데 사용

    <% 자바 소스 코드; %>
    ex) < % out.print ( "jsp에 오신 것을 환영합니다"); % >

  • expression tag
    : response의 output stream에 사용 --> out.print()를 사용할 필요가 없음

    • 주로 변수 또는 메서드의 값을 인쇄하는 데 사용
    • 주의해야 할 점: 세미콜론(;)을 사용하지 않는다

      <%= statement %>
      ex) <%= "welcome to jsp" %>

  • declaration tag
    : declaration tag는 필드나 메소드를 선언하기 위해 사용

    • declaration tag 안에 쓰여진 코드는 자동으로 생성되는 servlet의 service() 메소드 바깥에 위치한다. --> 각각의 request 마다 메모리를 차지하지 않는다.

      <%! field or method declaration %>


9 Implicit Objects

9가지의 implicit object들은 모든 jsp 페이지에 접근 가능한 웹 컨테이너에 의해서 생성된다.

1. out
: 버퍼에 데이터를 쓰기위해서 jsp는 out이라는 implicit object를 제공한다. 이것은 JspWriter의 객체

ex) <% out.print ( "오늘은 :" + java.util.Calendar.getInstance ().getTime ()); %>

2. request
: 웹 컨테이너에 의한 jsp request를 생성하기 위한 HttpServletRequest 타입의 implicit 객체

  • parameter, header information, remote address, server name, server port, content type, character encoding 등등과 같은 요청 정보를 얻기 위해 사용된다.

  • jsp request 범위에서 속성을 set, get, remove하기 위해 사용될 수 있다.

ex) String name=request.getParameter("uname");

3. response
: response는 HttpServletResponse 유형의 implicit 객체

  • HttpServletResponse의 인스턴스는 각 jsp 요청에 대해 웹 컨테이너에 의해서 생성된다.

  • 응답을 다른 리소스로 리디렉션, 오류 전송 등과 같은 응답을 추가하거나 조작하는 데에 사용할 수 있다.

ex) response.sendRedirect ( "http://www.google.com");

4. config
: config는 ServletConfig 유형의 implicit 객체

  • 특정 jsp 페이지에 대한 초기화된 파라미터를 사용할 수 있다.

  • 각각의 jsp 페이지의 웹 컨테이너에 의해 생성된다.

  • 일반적으로, 이 객체는 web.xml 파일의 초기화된 파라미터를 얻어 사용한다.

ex) String driver=config.getInitParameter("dname");

5. application
: ServletContext 유형의 implicit 객체

  • ServletContext의 인스턴스는 응용 프로그램이나 프로젝트가 서버에 배포될 때 웹 컨테이너에서 한 번만 생성된다.

  • 이 객체는 구성 파일(web.xml)에서 초기화된 매개 변수를 가져오기 위해 사용 가능

  • 응용 프로그램 범위에서 attribute를 get, set하거나 remove하기 위해 사용 가능

ex) String driver=application.getInitParameter("dname");

6. session
: HttpSession 유형의 implicit 객체이다.

  • 이 객체를 사용하여 속성을 set, get, remove, 또는 session 정보를 가져올 수 있다.

ex) session.setAttribute("user",name);

7. pageContext
: PageContext 타입의 implicit 객체이다.

  • 다음의 scope 중 하나에서 속성을 set, get, 또는 remove하기 위해 사용될 수 있다.
    • page
    • request
    • session
    • application

ex) pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);

8. page
: Object class 타입의 implicit 객체이다.

  • 자동으로 생성된 servlet 클래스의 참조를 할당 받는다.

  • log 안에 입력된 메세지는 콘솔 창에서 확인할 수 있다.

    • Object page=this;

ex) <% this.log ( "message"); %>

9. exception
: java.lang.Throwable 클래스 유형의 implicit 객체이다.

  • 예외를 프린트하기 위해 사용된다.

  • error인 페이지에만 사용된다.

출처: www.javatpoint.com/jsp-tutorial

profile
초보자가 기록하는 곳

0개의 댓글