JSP 기초

JSP syntax
HTML 같은 static data
include같은 JSP directives
JSP scripting elements and variables
JSP actions
custom tags with correct library

JSP directive 는 JSP compiler가 servlet을 어떻게 생성할지 조정한다
include : <%@ include file="somefile.jspf" %>
page :
import  - Results in a Java import statement being inserted into the resulting file.
contentType  - specifies the content that is generated. This should be used if HTML is not used or if the character set is not the default character set.
errorPage  - Indicates the page that will be shown if an exception occurs while processing the HTTP request.
isErrorPage - If set to true, it indicates that this is the error page. Default value is false.
isThreadSafe - Indicates if the resulting servlet is thread safe.
autoFlush - To autoflush the contents.A value of true, the default, indicates that the buffer should be flushed when it is full. A value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is illegal when also using buffer="none".
session - To maintain session. A value of true (the default) indicates that the predefined variable session (of type HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to it. A value of false indicates that no sessions will be used, and attempts to access the variable session will result in errors at the time the JSP page is translated into a servlet.
buffer - To set Buffer Size. The default is 8k and it is advisable that you increase it.
isELIgnored - Defines whether EL expressions are ignored when the JSP is translated.
language - Defines the scripting language used in scriptlets, expressions and declarations. Right now, the only possible value is "java".
extends - Defines the superclass of the class this JSP will become. You won't use this unless you REALLY know what you're doing - it overrides the class hierarchy provided by the Container.
info - Defines a String that gets put into the translated page, just so that you can get it using the generated servlet's inherited getServletInfo() method.
pageEncoding - Defines the character encoding for the JSP. The default is "ISO-8859-1"(unless the contentType attribute already defines a character encoding, or the page uses XML document syntax).

<%@ page import="java.util.*" %> <%-- example import --%>
<%@ page contentType="text/html" %> <%-- example contentType --%>
<%@ page isErrorPage="false" %> <%-- example for non error page --%>
<%@ page isThreadSafe="true" %> <%-- example for a thread safe JSP --%>
<%@ page session="true" %> <%-- example for using session binding --%>
<%@ page autoFlush="true" %> <%-- example for setting autoFlush --%>
<%@ page buffer="20kb" %> <%-- example for setting Buffer Size --%>

JSP scripting elements and objects
JSP내부객체
out - The JspWriter used to write the data to the response stream(output page).
page - The servlet itself.
pageContext  - A PageContext instance that contains data associated with the whole page. A given HTML page may be passed among multiple JSPs.
request  - The HttpServletRequest object that provides HTTP request information.
response - The HttpServletResponse object that can be used to send data back to the client.
session  - The HttpSession object that can be used to track information about a user from one request to another.
config  - Provides servlet configuration data.
application - Data shared by all JSPs and servlets in the application.
exception  - Exceptions not caught by application code .

Scripting elements(3 types)
1. declaration tag : <%! int serverInstanceVariable = 1; %>  변수선언, Static data members, inner classes, methods
2. scriptlet tag : <% int localStackBasedVariable = 1;
out.println(localStackBasedVariable); %> _jspService() 내부 위치
3. expression tag : <%= "expanded inline data " + 1 %>   servlet 내부에서 evaluate되는 표현들. semi-colon있으면 안 됨
* comments tag : <%-- give your comments here --%>

JSP actions
웹서버 기능을 호출하기 위한 XML tags. runtime에 실행.
jsp:include  - Similar to a subroutine, the Java servlet temporarily hands the request and response off to the specified JavaServer Page. Control will then return to the current JSP, once the other JSP has finished. Using this, JSP code will be shared between multiple other JSPs, rather than duplicated.
jsp:param  - Can be used inside a jsp:include, jsp:forward or jsp:params block. Specifies a parameter that will be added to the request's current parameters.
jsp:forward  - Used to hand off the request and response to another JSP or servlet. Control will never return to the current JSP.
jsp:plugin  - Older versions of Netscape Navigator and Internet Explorer used different tags to embed an applet. This action generates the browser specific tag needed to include an applet.
jsp:fallback - The content to show if the browser does not support applets.
jsp:getProperty  - Gets a property from the specified JavaBean.
jsp:setProperty - Sets a property in the specified JavaBean.
jsp:useBean - Creates or re-uses a JavaBean available to the JSP page.

JSP Tag Libraries
<%@ taglib uri="mytaglib.tld" prefix="myprefix" %>
...
<myprefix:myaction> <%-- the start tag %>
...
</myprefix:myaction> <%-- the end tag %>
...

public class MyActionTag extends TagSupport {
   //Releases all instance variables.
   public void release() {...}
 
   public MyActionTag() { ... }
 
   //called for the start tag
   public int doStartTag() { ... }
 
   //called at the end tag
   public int doEndTag(){ ... }
 }

JSP Standard Tag Library (JSTL)
Java EE 5의 포커스는 자바 어노테이션 사용을 쉽게하는 것이다. JSP2.1은 JSP tags handler와 context listener에 Dependency Injection을 위한 어노테이션을 정의함으로써 이를 지원한다.
JSP2.0과 JSF1.1에 의해 정의된 expression language를 통합하여  Unified Expression Language가 나왔다.
JSP2.1은 web semantic을 위한 Servlet2.5 스펙에 영향을 미친다.

JSP 2.0
EL은 Velocity 스타일의 템플릿을 생성하게 한다.
Hello, ${param.visitor} <%-- same as: Hello, <%=request.getParameter("visitor")%> --%>

// consider some beans.
 class Person {
   String name;
   // person nests an organization bean.
   Organization organization;

   public String getName() { return this.name; }
   public Organization getOrganization() { return this.organization; }
 }

 class Organization {
   String name;
   public String getName() { return this.name; }
 }


// then if an instance of Person was to be placed onto a request attribute under the name "person"
<!-- the JSP would have -->
 Hello, ${person.name}, of company ${person.organization.name} 
 <%-- second expression same as
      <% Person p = (Person) request.getAttribute("person");
         if (p != null) {
           Organization o = p.getOrganization();
           if (o != null) {
             out.print(o.getName());
           }
         }
      %>
  --%>

@출처 : Wikipedia - JavaServer Page


덧글

댓글 입력 영역