[JSP] Implicit Objects ( 묡μμ κ°μ²΄λ€ )
JSP Implicit Objectsλ Java κ°μ²΄λ€μ λλ€. λͺ μμ μΌλ‘ κ°λ°μκ° μ μΈνμ§ μμλ JSP Containerμμ μ 곡νλ κ°μ²΄λ€μ΄λΌκ³ 보μλ©΄ λκ² μ΅λλ€. κ·Έλμ JSP Implicit Objects λ₯Ό pre-defined λ³μλΌκ³ νκΈ°λ ν©λλ€.
JSP λ μνκ°μ§μ Implicit Objectsλ₯Ό μ§μν©λλ€. μ λ μλ κ°μ²΄λ€μ΄ λμ 보μ΄λ€μ.
Object | Description |
---|---|
request | This is the HttpServletRequest object associated with the request. |
response | This is the HttpServletResponse object associated with the response to the client. |
out | This is the PrintWriter object used to send output to the client. |
session | This is the HttpSession object associated with the request. |
application | This is the ServletContext object associated with application context. |
config | This is the ServletConfig object associated with the page. |
pageContext | This encapsulates use of server-specific features like higher performance JspWriters. |
page | This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. |
Exception | The Exception object allows the exception data to be accessed by designated JSP. |
The request Object:
request κ°μ²΄λ javax.servlet.http.HttpServletRequest κ°μ²΄μ μΈμ€ν΄μ€μ λλ€. μ¬μ©μκ° νμ΄μ§λ₯Ό μμ²ν λλ§λ€ JSP engine μ΄ ν΄λΉ μμ²μ λν μλ‘μ΄ requestκ°μ²΄λ₯Ό μμ±ν©λλ€. request κ°μ²΄λ HTTP header μ 보λ₯Ό μ»μ΄μ€κΈ°μν λ©μλλ₯Ό μ 곡ν©λλ€. form data, cookies, HTTP methodsκ°μ μ 보λ€μ΄ ν€λμ 보μ μκ² μ£ .
request κ°μ²΄μ κ΄λ ¨λ λ©μλλ€μ λν μ 보 λ° μ¬μ©λ²μ μ€λ₯Έμͺ½ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - Client Request.
The response Object:
response κ°μ²΄λ javax.servlet.http.HttpServletResponse κ°μ²΄μ μΈμ€ν΄μ€μ λλ€. μλ²κ° request κ°μ²΄λ₯Ό λ§λλ κ²μ²λΌ, ν΄λΌμ΄μΈνΈλ‘ 보λ΄κΈ°μν response κ°μ²΄λ λ§λλλ€.
response κ°μ²΄λ λν μλ‘μ΄ HTTPν€λ μμ±μ λ€λ£¨κΈ°μν μΈν°νμ΄μ€λ₯Ό μ μν©λλ€. μ΄ κ°μ²΄λ₯Ό ν΅ν΄μ JSP κ°λ°μλ€μ μΏ ν€, λ μ§ μ€ν¬ν, HTTP μν μ½λλ±μ μΆκ°ν μ μμ΅λλ€.
response κ°μ²΄μ κ΄λ ¨λ λ©μλλ€μ λν μ 보 λ° μ¬μ©λ²μ μ°μΈ‘ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - Server Response.
The out Object:
out κ°μ²΄λ javax.servlet.jsp.JspWriter κ°μ²΄μ μΈμ€ν΄μ€μ΄λ©° contentλ₯Ό responseμμ 보λ΄κΈ° μν΄μ μ¬μ©λ©λλ€.
μ²μμ JspWriter κ°μ²΄λ νμ΄μ§μ λ²νΌ μ¬μ©μ¬λΆμ λ°λΌ λ€λ₯΄κ² μ΄κΈ°νλ©λλ€. Buffering κΈ°λ₯μ page λλ ν°λΈμ buffered='false' μμ±μ ν΅ν΄μ μ½κ² off ν μ μμ΅λλ€. JspWriter κ°μ²΄λ java.io.PrintWriter ν΄λμ€μ κ±°μ λμΌν λ©μλλ€μ κ°μ§κ³ μμ΅λλ€. νμ§λ§ JspWriterλ μΆκ°μ μΌλ‘ λ²νΌλ§μ λ€λ£¨κΈ° μν λ©μλλ€μ κ°μ§κ³ μμ΅λλ€. PrintWriter κ°μ²΄μλ λ€λ₯΄κ² JspWriter λ IOExceptionsμ λμ§λλ€.
boolean, char, int, double, object, String, etc. μ μ°κ³ μΆμ λ μ¬μ©νλ λ©μλλ μλμ κ°μ΅λλ€.
Method | Description |
---|---|
out.print(dataType dt) | Print a data type value |
out.println(dataType dt) | Print a data type value then terminate the line with new line character. |
out.flush() | Flush the stream. |
The session Object:
sessionκ°μ²΄λ javax.servlet.http.HttpSessionμ μΈμ€ν΄μ€μ£ . Java Servletsμ μΈμ κ³Ό λμΌν μν μ νλ€κ³ 보μλ©΄ λ©λλ€.
session κ°μ²΄λ μ¬μ©μκ° μ¬λ¬λ² μμ²μ νκ² λ λ κ·Έ μμ²λ€ μ¬μ΄μ μΈμ μ μΆμ λ° κ΄λ¦¬νκΈ° μν΄μ μ¬μ©λ©λλ€.
session κ°μ²΄μ μ¬μ©λ²μ μ°μΈ‘ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - Session Tracking.
The application Object:
application κ°μ²΄λ ServletContextκ°μ²΄μ wrapperκ°μ²΄μ λλ€. μ€μ λ‘λ javax.servlet.ServletContext κ°μ²΄μ μΈμ€ν΄μ€μ λλ€.
μ΄ κ°μ²΄λ JSP page μλͺ μ£ΌκΈ° μ 체λ₯Ό λννλ€κ³ 보μλ©΄ λ©λλ€. μ ν리μΌμ΄μ μ΄ μ’ λ£λκΈ° μ κΉμ§ μ΄μμλ κ°μ²΄μ£ . JSP νμ΄μ§κ° μ΄κΈ°νλ λ μμ±λκ³ jspDestroy()λ©μλμ μν΄ JSPνμ΄μ§κ° μλ©Έλ λ μ΄ κ°μ²΄λ μλ©Έλ©λλ€.
application κ°μ²΄μ μμ±μ μΆκ°ν¨μΌλ‘μ¨ ν΄λΉ μ ν리μΌμ΄μ μ ꡬμ±νλ λͺ¨λ JSP νμΌλ€μ΄ μ΄κ³³μ μ κ·Όν μ μλ κΆνμ΄ μκΈ°κ² λ©λλ€.
Application κ°μ²΄μ κ°λ¨ν μ¬μ©λ²μ μ°μΈ‘ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - Hits Counter
The config Object:
config κ°μ²΄λ javax.servlet.ServletConfig μ μΈμ€ν΄μ€μ λλ€.
μ΄ κ°μ²΄λ JSP κ°λ°μκ° Servlet μ΄λ JSP μμ§ μ΄κΈ°ν νλΌλ―Έν° ( paths λλ file μμΉ λ± )μ μ κ·Όν μ μλλ‘ ν΄μ€λλ€.
λ§μ΄ μ°μ΄μ§ μλ κ°μ²΄μ΄λ©° μλ§ μ°κ² λλ€λ©΄ μλμ κ°μ΄ μ°λ κ² λ§κ³ λ μ λ§ μΈμΌμ΄ μμ κ²μ λλ€.
config.getServletName();
μ΄κ±΄ μλΈλ¦Ώλͺ μ μ»μ΄μ€λ λ©μλμ λλ€. WEB-INF\web.xml νμΌμ <servlet-name> μμμ λ€μ΄μλ μ΄λ¦μ μ»μ΄μ€λ κ²μ λλ€.
The pageContext Object:
pageContext κ°μ²΄λ javax.servlet.jsp.PageContext κ°μ²΄μ μΈμ€ν΄μ€μ λλ€. pageContext κ°μ²΄λ JSP page μ 체λ₯Ό λνν©λλ€.
μ΄ κ°μ²΄λ νμ΄μ§ μ 보λ₯Ό μ»μ΄μ€κΈ° μν μλ¨μΌλ‘ μ¬μ©λ©λλ€.
μ΄ κ°μ²΄λ κ° μμ²λ§λ€ request μ responseκ°μ²΄λ‘μ λνΌλ°μ€λ₯Ό μ μ₯ν©λλ€. application, config, session, κ·Έλ¦¬κ³ out κ°μ²΄λ€μ μ΄ κ°μ²΄μ μμ±μ μ κ·Όν΄μ μ»μ΄μ¬ μ μμ΅λλ€. pageContext κ°μ²΄λ λν ν΄λΉ JSP pageμμ λ°μλ directivesμ μ 보λ₯Ό μ μ₯νκ³ μμ΅λλ€. ( buffering μ 보, errorPageURL, κ·Έλ¦¬κ³ page scopeκ°μ μ λ³΄λ€ λ§μ΄μ£ ).
PageContext ν΄λμ€λ PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, κ·Έλ¦¬κ³ APPLICATION_SCOPE νλλ€μ μ μν©λλ€. 40κ° μ΄μμ λ©μλλ₯Ό μ 곡νλ©° κ·Έ μ€ λ°μ javax.servlet.jsp.JspContext ν΄λμ€μμ μμλ°μ λ©μλλ€μ λλ€.
κ°μ₯ μ€μν λ©μλ μ€ νλλ removeAttributeμΈλ° μ΄ λ©μλλ νλ λλ λκ°μ νλΌλ―Έν°λ₯Ό λ°μ΅λλ€. pageContext.removeAttribute ("attrName") λ λͺ¨λ scopeμμ attrName μμ±μ μ κ±°ν©λλ€. μλμ λμ¨ λ¬Έμ₯μ page scopeμμλ§ ν΄λΉ μμ±μ μ κ±°ν©λλ€.
pageContext.removeAttribute("attrName", PAGE_SCOPE);
pageContext κ°μ²΄μ λν μ¬μ©λ²μ μ°μΈ‘ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - File Uploading.
The page Object:
μ€μ νμ΄μ§λ₯Ό μ°Έμ‘°νκ³ μλ κ°μ²΄μ λλ€. JSP pageμ 체λ₯Ό λννλ κ°μ²΄λΌκ³ μκ°νμλ©΄ λ©λλ€..
μ΄ νμ΄μ§ κ°μ²΄λ μ€μ λ‘ μλ°μμ λμ€λ this κ°μ²΄μ λμμ΄λΌκ³ 보μλ©΄ λκ² μ΅λλ€.
The exception Object:
exception κ°μ²΄λ wrapper κ°μ²΄μ λλ€. μ΄μ νμ΄μ§μμ λμ Έμ§ exceptionμ κ°μ§κ³ μμ£ . λ μ΄κ±°μΌ μλ°λ₯Ό νμ λΆλ€μ΄λΌλ©΄ μμΈμ²λ¦¬λ₯Ό μν κ°μ²΄λΌκ³ κΈλ°© μμ€ κ²λλ€. λ λ μ€λͺ ν κ² μλ€μ.
μ΄ κ°μ²΄μ λν μ¬μ©λ²μ μ°μΈ‘ λ§ν¬μμ νμΈνμ€ μ μμ΅λλ€. JSP - Exception Handling.
Reference : http://www.tutorialspoint.com/jsp/jsp_implicit_objects.htm