JSP 내장 객체의 메소드 & 영역

YeHee·2024년 12월 10일

⏰ 2024.12.09 (D+43)

1. JSP 내장 객체의 메소드

1. Request 객체

설명: 클라이언트로부터 요청 데이터를 처리하는 데 사용됩니다.

  • getParameter(String name)

    • 특정 요청 파라미터의 값을 반환.
    • 예시:
      String username = request.getParameter("username");
  • getParameterValues(String name)

    • 동일한 이름을 가진 여러 요청 파라미터 값을 배열로 반환.
    • 예시:
      String[] selectedOptions = request.getParameterValues("options");
  • getAttribute(String name)

    • Request Scope에 저장된 속성을 반환.
    • 예시:
      Object data = request.getAttribute("data");
  • setAttribute(String name, Object value)

    • Request Scope에 속성을 저장.
    • 예시:
      request.setAttribute("data", "example");
  • getHeader(String name)

    • 요청 헤더 정보를 반환.
    • 예시:
      String userAgent = request.getHeader("User-Agent");
  • getCookies()

    • 클라이언트가 전송한 쿠키 배열을 반환.
    • 예시:
      Cookie[] cookies = request.getCookies();

2. Response 객체

설명: 서버에서 클라이언트로 응답을 전송할 때 사용됩니다.

  • setContentType(String type)

    • 응답의 MIME 타입을 설정.
    • 예시:
      response.setContentType("text/html");
  • getWriter()

    • 클라이언트에 데이터를 출력할 PrintWriter 객체를 반환.
    • 예시:
      PrintWriter out = response.getWriter();
      out.println("Hello, World!");
  • addCookie(Cookie cookie)

    • 클라이언트에 쿠키를 전송.
    • 예시:
      Cookie cookie = new Cookie("user", "John");
      response.addCookie(cookie);
  • sendRedirect(String location)

    • 클라이언트를 다른 URL로 리다이렉트.
    • 예시:
      response.sendRedirect("login.jsp");

3. Out 객체

설명: JSP 페이지에서 데이터를 출력하는 데 사용됩니다.

  • print(String s)

    • 문자열 데이터를 출력.
    • 예시:
      out.print("Hello, World!");
  • println(String s)

    • 문자열 데이터를 출력하고 줄바꿈 추가.
    • 예시:
      out.println("Hello, World!");
  • flush()

    • 출력 버퍼를 클라이언트로 강제로 전송.
    • 예시:
      out.flush();

4. Session 객체

설명: 클라이언트의 세션 데이터를 관리합니다.

  • getAttribute(String name)

    • 세션에 저장된 데이터를 반환.
    • 예시:
      String user = (String) session.getAttribute("user");
  • setAttribute(String name, Object value)

    • 세션에 데이터를 저장.
    • 예시:
      session.setAttribute("user", "John");
  • removeAttribute(String name)

    • 세션에서 특정 데이터를 제거.
    • 예시:
      session.removeAttribute("user");
  • invalidate()

    • 현재 세션을 무효화 (로그아웃 시 주로 사용).
    • 예시:
      session.invalidate();

5. Application 객체

설명: 애플리케이션 전체에서 데이터를 공유할 때 사용됩니다.

  • getAttribute(String name)

    • Application Scope에 저장된 데이터를 반환.
    • 예시:
      String config = (String) application.getAttribute("config");
  • setAttribute(String name, Object value)

    • Application Scope에 데이터를 저장.
    • 예시:
      application.setAttribute("config", "globalSettings");
  • removeAttribute(String name)

    • Application Scope에서 특정 데이터를 제거.
    • 예시:
      application.removeAttribute("config");

6. Config 객체

설명: 서블릿의 초기화 파라미터와 환경 정보를 제공합니다.

  • getServletName()

    • 현재 서블릿의 이름을 반환.
    • 예시:
      String servletName = config.getServletName();
  • getInitParameter(String name)

    • 초기화 파라미터 값을 반환.
    • 예시:
      String param = config.getInitParameter("dbURL");

7. PageContext 객체

설명: 현재 JSP 페이지의 컨텍스트 정보를 제공합니다.

  • getAttribute(String name)

    • Page Scope의 속성 값을 반환.
    • 예시:
      Object value = pageContext.getAttribute("key");
  • setAttribute(String name, Object value)

    • Page Scope의 속성 값을 설정.
    • 예시:
      pageContext.setAttribute("key", "value");
  • include(String relativeUrlPath)

    • JSP 파일을 현재 페이지에 포함.
    • 예시:
      pageContext.include("header.jsp");

8. Exception 객체

설명: 예외 처리 페이지에서 발생한 예외를 참조할 때 사용됩니다.

  • 주로 사용
    • getMessage(): 예외 메시지 반환.
    • 예시:
      String errorMessage = exception.getMessage();

2. JSP 내장 객체의 영역(Scope)

  1. Page Scope
    • 설명: 현재 JSP 페이지에서만 유효합니다.
    • 사용 객체: page, pageContext
    • 예시:
      pageContext.setAttribute("key", "value"); // 현재 페이지에서만 접근 가능

  1. Request Scope
    • 설명: 동일 요청(Request) 내에서만 유효합니다.
    • 사용 객체: request
    • 예시:
      request.setAttribute("key", "value");
      String value = (String) request.getAttribute("key");

  1. Session Scope
    • 설명: 동일 사용자(Session) 동안 유효합니다.
    • 사용 객체: session
    • 예시:
      session.setAttribute("user", "John");
      String user = (String) session.getAttribute("user");

  1. Application Scope
    • 설명: 애플리케이션 전체에서 유효합니다.
    • 사용 객체: application
    • 예시:
      application.setAttribute("config", "globalSettings");
      String config = (String) application.getAttribute("config");

3. JSP Action Tag

주요 Action Tag

  1. <jsp:useBean>
    • 설명: JavaBean 객체를 생성하거나 참조합니다.
    • 속성: id, class, scope
    • 예시:
      <jsp:useBean id="user" class="com.example.User" scope="session" />

  1. <jsp:getProperty>
    • 설명: JavaBean의 속성 값을 읽습니다.
    • 예시:
      <jsp:getProperty name="user" property="name" />

  1. <jsp:setProperty>
    • 설명: JavaBean의 속성 값을 설정합니다.
    • 예시:
      <jsp:setProperty name="user" property="name" value="John" />

  1. <jsp:include>
    • 설명: 다른 JSP 페이지를 포함합니다.
    • 예시:
      <jsp:include page="header.jsp" />

  1. <jsp:forward>
    • 설명: 요청을 다른 리소스로 포워딩합니다.
    • 예시:
      <jsp:forward page="next.jsp" />

  1. <jsp:param>
    • 설명: 요청에 파라미터를 추가합니다.
    • 예시:
      <jsp:forward page="next.jsp">
          <jsp:param name="id" value="123" />
      </jsp:forward>

4. JavaBean

특징

  1. 기본 생성자: 필수적으로 제공됩니다.
  2. Getter/Setter 메서드: 속성 접근과 수정을 지원합니다.
  3. Serializable 구현: 객체 직렬화가 가능합니다.

예시

public class User implements java.io.Serializable {
    private String name;
    private int age;

    public User() {} // 기본 생성자

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

0개의 댓글