Friday, June 20, 2014

Directive attributes

I think you got an simple idea about directives in my last post. In this post I said something about attributes. This post is for understanding about JSP directive attributes.

There are few other attributes too, but these are the most common attributes that can be used in a JSP page.

The import attribute

This is very  similar to importing packages to Java file. In JSP this can be done in one line or using multiple lines. Look at the following code.

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


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

But you need not to import java.lang.*, javax.servlet.*, javax.servlet.jsp.* and javax.servlet.http.* packages. By default these packages are imported by JSP engine.

The session attribute

This is used to indicate whether the JSP page associate with HTTP session. This is boolean attribute. default value is true.

<%@ page session="false" %>

The errorPage attribute

This is used to indicate the URL of error page for relevant JSP page. In Java we handle errors with try-catch block and throw/throws clauses. This is also can be done in JSP. But JSP allows you to do it in handy way with introducing errorPage attribute. Then you can handle any error occurred in you JSP within separate JSP page.

<%@ page errorPage="myErrorPage.jsp" %>

The isErrorPage attribute

This indicates the error handling page. This is a separate page that can be use to give any message or any operation if error is occurred. 

<%@ page isErrorpage="true"  %>

Look at the following code to see how to handle errorPage in JSP.

This is the main page.

<%@page errorPage="errors.jsp" %>

 <title>JSP error test</title>

<%! int x[] = {1,2,3}; %>

<h2>Result : <%= x[5] %> </h2>

This is the errors.jsp page

<%@ page isErrorPage="true" %>

 <title>JSP error page</title>

<h2> Error has occurred </h2>

This is the way to use errorPage and isErrorPage attributes in JSP.

The language attribute

This is used to specify the language which is using in the JSP page.

<% page language="java"  %>

The extends attribute

This specify the super class that the servlet must extend.

<%@ page extends="package.class"  %>

The buffer attribute

This is used to specify the minimum size required  by the output buffer. Default size is depend on JSP engine. It is better if you can have more than 8kb.

<%@page buffer="32kb" %>

The autoFlush attribute

This attribute is used to flush buffered output if it is full. Default value is true.

<%@page autoFlush="false" %>

The info attribute

This is used to provide some description about the JSP page.

<%@page info="This is a JSP page"  %>

The contentType attribute

This is specify the MIME( Multipurpose Internet Mail Extension ) type and the character encoding of the JSP page. 

<%@page contentType="text/html ; charset=ISO-8859-1" %>

The pageEncoding attribute

This is specify the character encoding of the JSP page. Default value is ISO-8859-1.

<%@page pageEncoding="ISO-8859-1"%>