Sunday, 8 March 2015

JSP page directive

page directive specifies the attributes for a jsp page, like importing packages, content type, output buffer size etc.,

Syntax
<%@ page [attribute="value" attribute="value" ...] %>

Example
<%@page contentType="text/html" pageEncoding="UTF-8"%>

Below program generates a random number by instantiating Random class.

random.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Generate Random Number</title>
    </head>
    <body>
        <h1>Random Number:
            <%
                Random rand = new Random();
                out.print(rand.nextInt());
            %>
        </h1>
    </body>
</html>


Output

As you observe the above program, it imported java.util package by using page directive.

<%@page import="java.util.*"%>

You can import multiple packages in jsp by comma separted.
<%@page import="java.util.*, java.io.*"%>

Attributes supported by page directive

Attribute Description
autoFlush It accept two values true/false. By default it is true. If autoFlush set to true, then the buffered output should be flushed automatically when the buffer filled. If it set to false, then an exception is raised to indicate buffer overflow.
buffer Specifies the size of output buffer in kilo bytes. Depending upon the value of the autoFlush attribute, the contents of this buffer is either automatically flushed, or an exception is raised, when overflow would occur. Default buffer size is 8kb.
contentType Specifies the MIME type and character encoding for the
response of the JSP page.
errorPage Specifies another jsp page url to handle any uncaught exceptions. The other JSP page must specify isErrorPage="true" in its page directive.
extends Specifies the super class of this jsp page. The value is a fully qualified java class file name.
import Used to import oackages and classes. Packages java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.* are imported implicitly by the JSP container.
info You can get this string message using Servlet.getServletInfo method.
isELIgnored
Defines whether Expression Language (EL) expressions are ignored or recognized for this page and translation unit. If isELIgnored is set to true, then EL expressions are ignored, else recognized.
isErrorPage If it is set to true, then this jsp page can be used as other jsp error page.
language Specifies the scripting language to be used in the scriptlets expression scriptlets, and declarations within the body of the translation unit. Currently the only valid value for this attribute is java.
pageEncoding
Describes the character encoding for the JSP page. For JSP pages in standard syntax, the character encoding for the JSP page is the charset given by the pageEncoding attriute if it is present, otherwise the charset given by the contentType attribute if it is present, otherwise “ISO-8859-1”.
session Specifies whether jsp page requires an http session. If the value is true, then the generated servlet will contain code that causes an HTTP session to be created or accessed, if it already exists. The default value is true.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment