wellnwill

JSP

2. page directive

page directive is used to perform page level operations eg. import, thread safe

Syntax

<%@ page
language=””
import=””
session=””
extends=””
isThreadSafe=””
errorPage=””
isErrorPage=””
isELIgnored=””
%>

language:

  • language attribute is used to specify the language for the scriptlets and declarations
  • Currently only java is the valid value which is also default value
  • Value other than java will cause invalid attribute error
  • Eg:
    <%@ page language=”java” %>

import:

  • import attribute is used to specify the packages to be imported for translated servlet
  • one or more packages can be specified by comma separation
  • Eg:
    <%@ page import=”java.io.*,java.util.*””%>

session:

  • Session object is used to enable and disable session attribute in JSP
  • By default session object is enabled
  • Eg:
    <%@ page session=”false” %>
  • If disables, session object cannot be used in JSP otherwise compilation error

extends:

  • HttpJspBase is the default superclass for the translated servlet
  • extends attributes can be used to replace this default HttpJspBase class by other
  • If HttpServlet class is used as a superclass using extend attribute, you have to override any of the methods of HttpServlet class
  • Eg:
  • <%@ page extends=”javax.servlet.http.HttpServlet” %>
    <%@ page import=”java.io.IOException,javax.servlet.ServletException”%>
    <%@ page extends=”javax.servlet.http.HttpServlet” %>
    <html>
       <body>
          <h1>
             <%!
                public void service(HttpServletRequest req, HttpServletresponse) 
                throws IOException, ServletException{
                _jspService(req, res);
                }
                %>
          </h1>
       </body>
    </html>
    

isThreadSafe:

  • isThreaSafe attribute is used to specify the servlet Thread model required
  • Default Servlet Thread Model is multi-thread model
  • Default values of isThreadSafe attribute is true
  • Eg:
    <%@ page isThreadSafe=”false” %> //Servlet Single Thread Model
    <%@ page isThreadSafe=”true” %> //Servlet Multi Thread Model
    

errorPage and isErrorPage:

  • isErrorPage attribute used to specify the JSP page is error page (error.jsp)
  • error.jsp will be displayed in case of any exception in other JSP files having errorPage=”error.jsp”
  • In such a way error.jsp acts as centralized JSP for error handling
  • Both attributes work in coordination with each other
  • index.jsp error.jsp
    <%@ page errorPage=”error.jsp” %>
    <html>
       <body>
          <h1>
             Welcome to Home
          </h1>
          <% int i = 10/0; %>
       </body>
    </html>
    
    <%@ page isErrorPage=”true” %>
    <html>
       <body>
          <h1>Error Occurred!</h1>
          <%= exception %>
       </body>
    </html>
    

isELIgnored:

  • isELIgnored attribute is used to enable or disable EL expressions
  • From Tomcat 6.0, EL Expressions are enabled by defaults
  • index.jsp
    <%@ page isELIgnored=”false” %>
    <html>
       <body>
          <% request.setAttribute(“msg”, “My message”); %>
          <h1>Messae: ${msg}</h1>
       </body>
    </html>
    
Footer with Map

Top