JSP Tutorial 2

김빛나리·2020년 7월 6일
0

1. Directive Elements

  • <%@ directive attribute="value" %>
  • page directive
    • <%@ page attribute="value" %>
    • import
      • import 속성은 클래스, 인터페이스 또는 패키지의 모든 멤버를 가져 오는 데 사용됨
      • <%@ page import="java.util.Date" %>
      • Today is: <%= new Date() %>
    • contentType
      • <%@ page contentType="application/msword" %>
      • Today is: <%= new java.util.Date() %>
      • jsp파일에 맨 윗줄에 있는 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 에서 contentType="text/html; charset=UTF-8"은 삭제하고 실행해야함 -> contentType은 두 개이상 사용X
      • 실행하게되면 msword파일 열어서 거기에 Today: ~~~나옴
    • extends
      • 거의 사용되지 않음
    • info
      • This attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.
      • The web container will create a method getServletInfo() in the resulting servlet.
    • buffer
      • <%@ page buffer="16kb" %>
    • language
      • 기본 값은 java
    • isELIgnored
      • Expression Language (EL) in jsp를 무시할 수 있음
      • 기본 값은 false => EL는 기본적으로 사용됨
      • <%@ page isELIgnored="true" %> //Now EL will be ignored
    • isThreadSafe
      • JSP 페이지의이 동작을 제어하려면 페이지 지시문의 isThreadSafe 속성을 사용할 수 있음
      • 기본 값은 true => false이면 The web container가 여러 요청을 직렬화함(만들어줘야함)
      • <%@ page isThreadSafe="false" %>
    • errorPage
      • <%@ page errorPage="myerrorpage.jsp" %>
      • 에러가 발생하면 myerrorpage.jsp로 이동
    • isErrorPage
      • <%@ page isErrorPage="true" %>
      • error문구 넣어주기
  • include directive
    • <%@ include file="resourceName" %>
    • <%@ include file="header.html" %>
    • jsp file, html file, text file
  • taglib directive
    • <%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>

2. Action Elemnets

  • forward
    • Syntax of jsp:forward action tag without parameter
      • <jsp:forward page="relativeURL | <%= expression %>" />
    • Syntax of jsp:forward action tag with parameter
      • <jsp:forward page="relativeURL | <%= expression %>">
      • <jsp:param name="parametername" value="parametervalue | <%=expression%>" />
      • </jsp:forward>
    • <jsp:forward page="printdate.jsp" />
      • printdate.jsp파일에 있는 글 출력
    • <jsp:forward page="printdate.jsp" >
      • <jsp:param name="name" value="javatpoint.com" />
      • </jsp:forward>
  • include
    • to include the content of another resource it may be jsp, html or servlet.
    • it is better for dynamic pages because there might be changes in future.
    • Syntax of jsp:include action tag without parameter
      • <jsp:include page="relativeURL | <%= expression %>" />
    • Syntax of jsp:include action tag with parameter
      • <jsp:include page="relativeURL | <%= expression %>">
      • <jsp:param name="parametername" value="parametervalue | <%=expression%>" />
      • </jsp:include>
  • java bean class
    • JavaBean
      • 인수가 없는 생성자(no-arg constructor)가 있어야 함
      • 직렬화(Serializable) 가능해야 함
      • getter 및 setter 메소드라고하는 특성 값을 (set)설정하고 (get)가져 오는 메소드를 제공해야 함
    • example of JavaBean class
      • package mypack;
      • public class Employee implements java.io.Serializable{
        • private int id;
        • private String name;
        • public Employee(){}
        • public void setId(int id){this.id=id;}
        • public int getId(){return id;}
        • public void setName(String name){this.name=name;}
        • public String getName(){return name;}
      • }
    • javabean class를 access하려면 getter와 setter methods를 사용해야함
      • package mypack;
      • public class Test{
        • public static void main(String args[]){
          • Employee e=new Employee();//object is created
          • e.setName("Arjun");//setting value to the object
          • System.out.println(e.getName());
      • }}
      • There are two ways to provide values to the object. One way is by constructor and second is by setter method.
    • getPropertyName ()
      • 특성 이름이 firstName 인 경우 method 이름은 getFirstName ()으로 해당 특성을 읽고, 이 method를 접근자라고 함
      • if the property name is firstName, the method name would be getFirstName() to read that property. This method is called the accessor.
    • setPropertyName ()
      • 속성 이름이 firstName 인 경우 해당 속성을 쓰려면 method 이름이 setFirstName ()이 됨
      • if the property name is firstName, the method name would be setFirstName() to write that property. This method is called the mutator.
  • useBean
    • bean class를 찾거나 instantiate
      • class의 beanObject가 이미 작성된 경우 bean을 작성하지 않음
      • beanObject가 작성되지 않으면 bean을 instantiate
    • <jsp:useBean id= "instanceName" scope= "page | request | session | application"
      - class= "packageName.className" type= "packageName.className"
      - beanName="packageName.className | <%= expression >" >
      - </jsp:useBean>
      - 설명
      1. id
        지정된 범위에서 bean을 지정하는데 사용
      2. scope
        - bean의 범위
        - 기본 범위는 page
        • page
          jsp page내에서 bean을 사용할 수 있도록 지정
        • request
          동일한 요청을 처리하는 모든 jsp page에서 bean을 사용할 수 있도록 지정
          +page보다 범위가 넓음
        • session
          동일한 요청과 관계없이 동일한 세션의 모든 jsp page에서 bean을 사용할 수 있도록 지정
          +request보다 범위가 넓음
        • application
          동일한 application의 모든 jsp page에서 bean을 사용할 수 있도록 지정
          +session보다 범위가 넓음
      3. class
        • 지정된 baen class를 instantiates / bean class의 object를 만든다.
        • it must have no-arg or no constructor and must not be abstract.
      4. type
        • bean이 이미 범위에 있는 경우, bean에 데이터 유형을 제공
        • class 또는 beanName과 함께 사용
        • lass 또는 beanName없이 사용하면 Bean이 instantiates되지 않음
      5. beanName
        • instantiates the bean using the java.beans.Beans.instantiate() method.
  • set&getProperty
    • jsp:setProperty action tag
      • sets a property value or values in a bean using the setter method.
      • <jsp:setProperty name="instanceOfBean" property= "*"/>
        • <jsp:setProperty name="bean" property="*" />
      • <jsp:setProperty name="instanceOfBean" property="propertyName" param="parameterName" />
        • <jsp:setProperty name="bean" property="username" />
      • <jsp:setProperty name="instanceOfBean" property="propertyName" value="{ string | <%= expression %>}" />
        • <jsp:setProperty name="bean" property="username" value="Kumar" />
    • jsp:getProperty action tag
      • returns the value of the property
      • <jsp:getProperty name="instanceOfBean" property="propertyName" />
        • <jsp:getProperty name="obj" property="name" />
      • 예제 오류
  • displaying applet in jsp
    • Syntax of jsp:plugin action tag
      • <jsp:plugin type= "applet | bean" code= "nameOfClassFile" codebase= "directoryNameOfClassFile" </jsp:plugin>
    • Example
      • <jsp:plugin align="middle" height="500" width="500" type="applet" code="MouseDrag.class" name="clock" codebase="."/>

3. Expression Language(EL)

  • Syntax for Expression Language (EL)
    • ${ expression }
  • pageScope
    • it maps the given attribute name with the value set in the page scope
    • 주어진 속성 이름을 페이지 범위에 설정된 값으로 맵핑합니다.
  • requestScope
    • it maps the given attribute name with the value set in the request scope
    • 주어진 속성 이름을 요청 범위에 설정된 값으로 매핑합니다.
  • sessionScope
    • it maps the given attribute name with the value set in the session scope
    • 주어진 속성 이름을 세션 범위에 설정된 값으로 매핑합니다.
    • sessionS.jsp 예시
  • applicationScope
    • it maps the given attribute name with the value set in the application scope
    • 주어진 속성 이름을 응용 프로그램 범위에 설정된 값으로 매핑합니다.
  • param
    • it maps the request parameter to the single value
    • 요청 매개 변수를 단일 값에 맵핑합니다.
    • param.jsp 예시
  • paramValues
    • it maps the request parameter to an array of values
    • 요청 매개 변수를 값의 배열에 맵핑합니다.
  • header
    • it maps the request header name to the single value
    • 요청 헤더 이름을 단일 값에 맵핑합니다.
  • headerValues
    • it maps the request header name to an array of values
    • 요청 헤더 이름을 값 배열에 매핑합니다.
  • cookie
    • it maps the given cookie name to the cookie value
    • 주어진 쿠키 이름을 쿠키 값에 매핑합니다.
    • cookie.jsp 예시
  • initParam
    • it maps the initialization parameter
    • 초기화 매개 변수를 맵핑합니다.
  • pageContext
    • it provides access to many objects request, session etc.
    • 많은 객체 요청, 세션 등에 대한 액세스를 제공합니다.

참고:

0개의 댓글