JSP뽀수기 - accidentlywoo/secsec GitHub Wiki

JSP 요소

크게

  1. Scripting Element

  2. Directive Element

  3. Action Tag Element

  4. Scripting Element

  • scriptlet : <% %>

jsp용 java파일의 _jspService()내부에 들어갈 구문

  • expression : <%= %>

jsp용 java파일의 _jspService()내부에 들어갈 구문,

out.print()를 자동호출함

  • declaration : <%! %>

jsp용 java파일의 _jspService() 외부에 들어갈 구문

servlet class의 인스턴스 변수를 선언할 수 있다.

  1. Directive Element
  • page 지시자 : <%@ page %>

jsp용 java 파일이 만들어질때 필요한 정보를 기술

페이지 지시자 어트리뷰트들 : contentType, import, buffer, autoFlush, isThreadSafe, session, isErrorPage, errorPage

  • include 지시자 : <%@ include %>

Action Tag Element - include tag element와 비교

jsp용 java파일이 만들어질때 포함

  • taglib 지시자 : <%@ taglib %>
  1. Action Tag Element
  • standard tag element
  • include tag element
  • custom tag element

JSP 내장 변수

  • request :
  • response :
  • out : 웹 브라우저로 HTML 코드를 출력하는 기능 javax.servlet.jsp.JspWriter ->PrintWriter.print() + BufferedWriter()
  • application :
  • config : 레거시
  • pageContext :
  • page : servlet은 servlet class에서 attribute를 갖을 수 없지만 jsp는 page를 지원해서, jsp 페이지별로 attribute 세팅할 수 있다.
  • exception : <%@ page isErrorPage=true %> 일때만 사용가능

~Context : 기능을 모아놨다는 의미

EL(Expression Language)

jsp에서 제공하는 element의 한계점을 보안하기 위해 새로 추가된 문법

EL은 null을 빈문자열 ""로 표현한다.

EL식에 있는 데이터 이름을 해석하는 순서는 사용 범위가 좁은 애트리뷰트부터 점점 더 사용 범위가 넓은 애트리뷰트 순으로 진행된다.

${MapType.key}

${param.key}
${paramValues.key}

${JavaBeans.property }

  • 자바빈즈 조건 : public class, public 기본생성자, 프로퍼티용 필드는 private/protect, 프로퍼티용 메서드는 getter/setter

page Attribute > request Attribute > session Attribute > application Attribute

EL 내장 객체

  • pageScope
  • requestScope
  • sessionScope
  • applicationScope
  • param
  • paramValues
  • header
  • headerValues
  • cookie
  • initParam
  • pageContext : EL에서 request를 내장객체로 제공하지 않기 때문에 pageContext를 통해서 request에 접근할 수 있다.

EL은 단순 출력만 제공하기 때문에, 이걸 왜쓰지? 라는 생각이 들 정도이다.

-> JSTL 라이브러리 추가해서 JSTL 사용하기!

JSTL

maven repository에서 jar파일을 받은 후 WebContent > WEB-INF > lib > 여기에 추가!

jar그대로 넣는다!

Eclipse Dynamic Web Project 기준

Java Resources > Libraries > Web App Libararies > jstl.jar를 찾아서 구성을 보자!

라이브러리를 펼쳤을때 보이는 META-INF아래의 c.tld / fmt.tld파일을 펼쳐보자

xml형식의 tld파일들에서 을

Directive Element - taglib에서 사용한다.

나머지는 MVC모델에 적합하지 않는 모델 (redirect, db..)안씀

JSTL로 사용할 기능

  • 간단한 프로그램 로직의 구사(자바의 변수 선언, if문, for문 등에 해당하는 로직)
  • 날짜, 시간, 숫자의 포맷
  • 문자열 처리 함수

c.tld [Core Library]

  • <c:set var="name" value="value"> : pageScope의 attribute로 저장이된다.

  • <c:set var="sum" value="${num1+num2}" />

fmt.tld [Formatting Library]

fn.tld [Function Library]

정리하기

EL

  • ${pageScope.a}

= <%=pageContext.getAttribute("a") %>

  • ${requestScope.a}

= <%=request.getAttribute("a")%>

  • ${sessionScope.a}

= <%=session.getAttribute("a") %>

  • ${applicationScope.a}

= <%=application.getAttribute("a")%>

= <%=getServletContext().getAttribute("a")%>

  • ${param.a} : null일때 ""출력

= <%=request.getParameter("a")%> : null일때 "null"출력

  • ${empty param.a ? "null" : param.a}

= <%= ((request.getParameter("a")==null) || (request.getParameter("a").equals(""))) ? "null" : request.getParameter("a") %>

Custom Action Tag

Standard Action Tag

⚠️ **GitHub.com Fallback** ⚠️