Jsp Drectives tutorial
How to use directives in JSP
III. Jsp Drectives
JSPHome
What are Directives in JSP?
Directives in JSP Page, are nothing but elements of JSP code, which directs web container at the time of converting, JSP page to servlet.
Directive provides special information, messages about the entire JSP page which is used in processing of the page. This information is global.
There are three types of directive tags available in JSP.
- page
- include
- taglib
All directives start with @ symbol.
<%@ %> → directives used to import library files.
Page directive
example:
<%@ page import="javal.util.*;" %>
include directive
It includes files like html,jsp,xml at compile time to this page. <%@ include file="relativeurl" %> <%@ include file="header.html" %>
example:
pageIndludedemo.jsp
---------------
<%@ page pageEncoding="UTF-8" %>
<%@ include file="PageDemo.jsp" %>
<% out.println("Including Page"); %>
o/p: pageDemo o/p plus current page data.
taglib directive:
<%@ taglib uri=taglibraryUri" prefix="tagprefix" %>
<%@ taglib uri="mytags" prefix="mt" %>
pagedemo.jsp
ex:
<%@ page import="java.util.*" %>
<b><%Date d = new Date();
out.println(d);
%></b>
page directive attributes
import : To import java packages.
extends: Extends only HttpServlet/subclasses.
isErrorpage(boolean value): Tells the page wether errorpage/not.
ex: <%@ page iserrpage=true %>
isThhreadsafe: makes the JSP page singlethreadmodel.
ex: <%@ page isthreadsafe=true %>
buffer: Used to set buffer size of response obj, and is used in file downloading application.
ex: <%@ page buffer=8192 bytes %> (8kb)
can be 16 and 32 kb.
errorpage: if there are any exception in cur jsp page insteading of
displaying stacktrace errorpage is displayed
ex: <%@ page errorpage="error.html" %>
pageencoding: used to specify page encoding
<%@ page pageEncoding="ISO-8859-1" %>
session:by using session attribute you can avoid creating session obj by
saying session=false
ex: <% @page session=true %>
autoflush:used clears buffer of responseobj automatically
ex: <% @page autoflush=true %>
contenttype:diff content types text/plain or text/html, text/xml
ex: <% @page contenttype="text/html" %>
errorpage.jsp
URL Looks as shown..
.../errorpage.jsp?name=xxx
errorpage.jsp
-------------
<%@ page errorpage="error.html" %>
String user= req.getParameter("name");
out.println("Hello.."+user);
%>
ex2:
<%@ page errorPage="err.jsp" %>
<html>
<body>
<%
int i = 10;
i = i / 0;
%>
</body>
</html>
err.jsp
<%@ page isErrorPage = "true"%>
<%@ page import = "java.io.*" %>
<HTML>
<HEAD>
<TITLE> Coding-zon demo - JSP custom error page </TITLE>
</head>
<body>
<h2>Application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br>
<font color="red"><%= exception.toString() %></font>
</body>
</html>
Javabean feautres:
- Java Bean is a simple java class.
- It contains private properties(member variables) and get/set methods for each property.
- It implements serializable.
Comments
Post a Comment