HOME
Java Server Pages
JSP
Introduction JSP
Syntax JSP Expression
JSP Declaration
JSP Comments
JSP Directives
JSP Actions
JavaBeans
JSP Sessions
JSP JDBC
JSP And HTML Forms
JSP With XML
JSP Exam
|
JSP Directive
JSP directives serve as message to the JSP container from the JSP. They do not produce any output to the client. All directives have scope to the entire JSP file. They are used to set page level instruction, insert data form files and specify custom tag libraries.
Types of JSP directives
These are three main types of JSP directives they are-
Page directive
-
Include directive
-
Taglib directive
All the above three directives begin with <% @ and end with %> . They affect the overall structure of the servlet class and have the following general syntax.
<%@ directivname attribute1="value1" attribute2="value2" ………..attribute="valuen" %>
This means that for this page directive, assign these values for these attributes. Let us now discuss each of these directives in details.
The Page Directive
the page directive is a JSP tag that will be used in almost every JSP file. The page directive gives instruction to the JSP engine that applies to the entire source file.
It may specify a comment that you would like to become a part of the compiled JSP file or it may specify the scripting language used in the JSP file. It can also specify the package the source file would import, or an error page called if an error or exception occurs.
The following listing uses the page directive. Not that java.util.* is important inside the page directive
example for page directive |
<%@ page language="java" import="java.util.*" session="true" buffer="12kb" autoFlush="true" info="example for page directive" errorPage="error.jsp" is ErrorPage="false" isThreadSafe="false" >
<html>
<body>
<h1> odnr </h1>
</body>
</html>
|
The error page can also be displayed using the page directive. The above listing is modified to displays an error page. A file named error.jsp is created which displays an error message
Now i will explain how error page appears
| Math.jsp
<html>
<body>
<h1>
This page tests the page directive. This will display the error message
<%=3/0 %></h1>
</body>
</html>
error .jsp
<html>
<body>
There is an error in this page</body></html>
|
The file
Math.jsp has an error in it as the expression
<%=3/0 %>
Divides 3 by 0. So when math.jsp is executed in the web server, the error page ids displayed
The page directives can be used any where in the program but it is good practice to use it on the top of the file. It is a JSP tag it may even be placed before opening the <html> tag
Include Directive
The include directive includes a static file in a JSP file. The include directive has the following syntax
<%@ include file=”relative URL” %>
The attribute relative URL is a pathname to the include file, which is always a relative URL. Relative URL is just a path segment of an URL. without a protocol, port, or domain name.
The include directive inserts a file of text or code in JSP file at translation time, when the JSP is compiled. When the include Directive is used, the include process is static. This means that the text of the include file is added to the JSP file. The include file may be a JSP file, HTML file, or text file or a code file written in Java programming language. if the include file is the JSP file , the JSP elements are parsed and their results included in the JSP file. It should be taken care that the included files don’t parsed and their results included in the JSP file.
The following example demonstrates the include directive
includefile.html |
<html>
<table border=0 width=400 cellpadding=0 cellspacing=0>
<tr><td>
this is the example for include directive</td></tr>
</table>
</html></pre>
example.jsp
<%@ page info=” a simple jsp example” %>
<html>
<head><title>example</title></head>
<body>
<%@ include file=”incldefile.html”%>
<h1>I think you got idea how this works</h1>
</body>
</html> |
Taglib Directive
This directive allows the page to use custom used defined tags, it also defines the tag library. The JSP engine used this tag library to process the tags it comes across in the JSP. Its syntax is as follows.
<%@ taglib uri=”uritaglibrary” prefix=”tagprefix” %>
The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies this tag prefix.
|
|
Inform Friend About This Site |