JSP Page Directive Element

JSP page Directive elements

  • JSP Directive elements are used in the JSP file and provide information to the JSP container about the page. There are three directives available: page, include, and taglib.
  • The page has a lot of attributes (see below) that specify conditional information to the JSP container for the page.
  • You may code many page directives anywhere in your JSP page, but the page directives are normally coded at the top of the JSP page.
  • JSP page directive element is specified either in the form of XML tags, or in the form of a JSP style notations.
    Here is the notation for the page directive element:
    JSP Style or XML Description
    <%@ page attributes %> or
    <jsp:directive.page attributes />
    Defines page-dependent attributes, such as session, import, buffering, scripting language and error page.
  • The following table lists the attribute(s) for the page directive:
    Attribute Valid values Default Description
    autoFlush true or false true Determines whether the output buffer should be flushed automatically when it is full.
    buffer Xkb or "none". 8kb The output buffer size in kilobytes. X is a integer value.
    contentType Any valid MIME type and character encoding combination. text/html; charset= ISO-8859-1 Determines the page's HTTP response MIME type and character encoding.
    deferredSyntax/- AllowedAsLiteral true or false false In earlier versions of the JSP specification, the #{} syntax for the expression language was not reserved. For backwards compatibility purposes, this attribute sets any expressions using this syntax to be string literals.
    errorPage Any valid relative URL to another JSP. n/a Indicates which page to navigate when the JSP throws an exception.
    extends The fully qualified name for the JSP's parent class. n/a Indicates the class this JSP extends.
    import A fully qualified name of a class to import. n/a Imports one or more classes to be used in scriptlets. You can use ".*" to import all necessary classes from the package (for example, <%@ page import java.util.* %>).
    info Any string. n/a The value for this attribute is incorporated into the compiled JSP. It can later be retrieved by calling the page's getServletInfo() method.
    isELIgnored true or false false Setting this value to true prevents expression language expressions from being interpreted.
    isErrorPage true or false false Determines if the page is an error page.
    isThreadSafe true or false true Determines whether the page is thread safe.
    language Scripting language. java Determines the scripting language used in scriptlets, declarations, and expressions in the JSP page. Scripting languages must be able to be performed in Java Virtual Machine (groovy, JRuby, and so on).
    pageEncoding Any valid page encoding. n/a Determines the page encoding, for example, "UTF-8".
    session true or false true Determines whether the page has access to the HTTP session.
    trimDirective/- Whitespaces true or false false When JSPs are rendered as HTML in the browser, the generated markup frequently has a lot of blank lines in it. Setting this attribute to true prevents these extraneous blank lines from being generated in the markup.
  • The most used attributes are shown in bold.

Example of using JSP page directive element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is PageDirective).

  • Page directive example:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
      </head>
      <body>
        <h1>Hello World!</h1>
      </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Hello).

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
      <servlet>
        <servlet-name>Hello</servlet-name>
        <jsp-file>Hello.jsp</jsp-file>
      </servlet>
      <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/showHello</url-pattern>
      </servlet-mapping>
      <session-config>
        <session-timeout>
          30
        </session-timeout>
      </session-config>
      <welcome-file-list>
        <welcome-file>showHello</welcome-file>
      </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url showHello. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /PageDirective) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/PageDirective</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.