JSP Tutorial

Directives

Directives:

Directives are basically used to configure the code that is generated by container in a servlet. As a part of JSP we have three types of directives; they are page directives, include directives and taglib directives.

Page directives:

Page directives are basically used for supplying compile time information to the container for generating a servlet. The page directive will take the data in the form of (key, value) pair.

<%@
page attribute_name1=attribute_value1,
     attribute_name2=attribute_value2,
     ...................................., 
     Attribute_nameN=attribute_valueN
%>
  • Whenever we use page directive as a part of JSP program that statement must be the first statement.
  • he scope of page directive is applicable to current JSP page only.

The following table gives the page directive attribute name and page directive attribute value:

Attribute nameAttribute value
importThis attribute is used for importing either pre-defined or user- defined packages. The default value is java.lang.*

For example:

<%@ page import="java.sql.*, java.io.*" %>
contentType. This attribute is used for setting the MIME type (plain/text, img/jpeg, img/gif, audio/wave, etc.). The default value is text/html.

For example:

<%@ page contentType="img/jpeg" %>
languageThis attribute represents by default java i.e., in order to represent any business logic a JSP program is making use of java language. The attribute language can support any of the other programming languages for developing a business logic at server side.

For example:

<%@ page language="java" %>
isThreadSafeThis attribute represents by default true which represents one server side resource can be accessed by many number of clients (each client is treated as one thread). At a time if we make the server side resource to be accessed by a single client then the value of isThreadSafe is false.

For example:

<%@ page isThreadSafe="false" %>
  • isErrorPage
  • errorPage
When we write 'n' number of JSP pages, there is a possibility of occurring exceptions in each and every JSP page. It is not recommended for the JSP programmer to write try and catch blocks in each and every JSP page. It is always recommended to handle all the exceptions in a single JSP page.

isErrorPage is an attribute whose default value is true which indicates exceptions to be processed in the same JSP page which is not recommended. If isErrorPage is false then exceptions are not processed as a part of current JSP page and the exceptions are processed in some other JSP page which will be specified through an attribute called errorPage.

For example:

<%@ page isErrorPage="false" errorPage="err.jsp" %>

err.jsp:

<%= exception%>

[or]

<%= exception.getMessage()%>
  • autoflush
  • buffer
Whenever the server side program want to send large amount of data to a client, it is recommended to make autoflush value as false and we must specify the size of the buffer in terms of kb. The default value of autoflush is true which represents the server side program gives the response back to the client each and every time. Since, the buffer size is zero.

For example:

<%@ page autoflush="false" buffer="12kb" %>
<%@ page autoflush="true" %> [by default]
sessionWhen we want to make 'n' number of independent requests as consecutive requests one must use the concept of session. In order to maintain the session we must give the value of session attribute has true in each and every JSP page (recommended). The default value of session is true which represents the session is applicable to current JSP page.

For example:

<%@ page session="true" %>
- session will be created or old session will be continued.
infoUsing this attribute it is recommended for the JSP programmer to specify functionality about a JSP page, on what date it is created and author. attribute value

Write a JSP page which illustrates the concept of isErrorPage and errorPage?

Answer:

web.xml:

<web-app>
</web-app>

Exception.jsp:

<%@ page isErrorPage="false" errorPage="ErrorPage.jsp" %>
<html>
    <body>
        <%= 30 / 0%>
    </body>
</html>

ErrorPage.jsp:

<%@ page isErrorPage="true" %>
<html>
    <body>
        Exception is <%= exception%> generated...<br> Exception message is <%= exception.getMessage()%><br>
        <%@ include file="copyright.html" %>
    </body>
</html>

Copyright.html:

<html>
    <br>
    <br>
    <br>
    <em>All copy rights are reserved for kvr.com<em>
</html>

Develop JSP pages which will participate in session?

Answer:

web.xml:

<web-app>
</web-app>

First.jsp:

<%@ page session="true" %>
<html>
    <body>
        <form name="first" action="Second.jsp">
            Enter your name : <input type="text" name="first_name"><br>
            <input type="submit" value="Send">
        </form>
    </body>
</html>

Second.jsp:

<%@ page session="true" %>
<html>
    <body>
        <% String str = request.getParameter("first_name");%> 
        Your name is <h4><%= str%></h4>
        <% session.setAttribute("name", str);%>
        <form action="Third.jsp"><br>
            Send the request to next page<br>
            <input type="submit" value="Send">
        </form>
    </body>
</html>

Third.jsp:

<%@ page session="true" %>
<html>
    <body>
        Value in third page from session objects is
        <h4><%= session.getAttribute("name")%></h4>
    </body>
</html>

Include directives:

Include is the directive to include the server side resource. The server side resource can be either an html file or JSP or a servlet. If we include html file, it will be executed by browser when the response is rendering to the client. When we include a JSP or a servlet, it will be executed by container.

Syntax:

<% include file = "file name to be included" %>

For example:

<% include file = "copyright.html" %>