1. JAVA SERVER PAGE DIRECTIVES
A. D. PATEL INSTITUTE OF TECHNOLOGY
AJ(2160707) : A.Y. 2018-19
GUIDED BY:
PROF. NAYAN MALI
(DEPT OF IT, ADIT)
PREPARED BY:
KUNAL KATHE
E.R.NO.:160010116023
DHRUV SHAH
E.R.NO.:160010116053
CHINTAN SUDANI
E.R.NO.:160010116056
DEPARTMENT OF INFORMATION TECHNOLOGY
A D PATEL INSTITUTE OF TECHNOLOGY (ADIT)
NEW VALLABH VIDYANAGAR, ANAND, GUJARAT
2. 2
Introduction To JSP:
• JSP technology has facilitated the segregation of the work of a Web designer and a Web
developer.
• A Web designer can design and formulate the layout for the Web page by using HTML.
• On the other hand, a Web developer working independently can use java code and other
JSP specific tags to code the business logic.
• The simultaneous construction of the static and dynamic content facilitates development of
quality applications with increased productivity.
• A JSP page , after compilation , generates a servlet and therefore incorporates all servlet
functionalities.
3. 3
• Servlets and JSP thus share common features, such as platform independence , creation of
database-driven Web applications , and server side programming capabilities.
• Servlets tie up files (an HTML file for the static content and a Java file for the dynamic
contents) to independently handle the static presentation logic and the dynamic business
logic.
• Due to this , a change made to any file requires recompilation of the servlet.
• JSP on the other hand allows Java to be embedded directly into an HTML page by using tags.
• The HTML content and the Java content can also be placed in separate files.
• Any changes made to HTML content is automatically compiled and loaded onto the servlet
4. 4
JSP Directives:
• JSP directives provide directions and instructions to the container, telling it how
to handle certain aspects of JSP processing.
• A JSP directive affects the overall structure of the servlet class.
• It usually has the following form:
<%@ directive attribute="value" %>
• The blanks between the @ symbol and the directive name, and between the last attribute
and the closing %>, are optional.
Directive Description
<%@ page ... %> Defines page-dependent attributes, such as scripting language, error
page, and buffering requirements.
<%@ include ... %> Includes a file during the translation phase.
<%@ taglib ... %> Declares a tag library, containing custom actions, used in the page
5. 5
Page directives:
• The page directive is used to provide instructions to the container that pertain to
the current JSP page. You may code page directives anywhere in your JSP page. By
convention, page directives are coded at the top of the JSP page.
• Following is the basic syntax of page directive:
<%@ page attribute="value" %>
• You can write XML equivalent of the above syntax as follows:
<jsp:directive.page attribute="value" />
6. 6
Following is the list of attributes associated with page directive:
Attribute Purpose
buffer Specifies a buffering model for the output stream.
autoFlush Controls the behavior of the servlet output buffer.
contentType Defines the character encoding scheme.
errorPage Defines the URL of another JSP that reports on Java unchecked
runtime exceptions.
isErrorPage Indicates if this JSP page is a URL specified by another JSP
page's errorPage attribute.
extends Specifies a superclass that the generated servlet must extend
import Specifies a list of packages or classes for use in the JSP as the
Java import statement does for Java classes.
info Defines a string that can be accessed with the servlet's
getServletInfo() method.
isThreadSafe Defines the threading model for the generated servlet.
language Defines the programming language used in the JSP page.
session Specifies whether or not the JSP page participates in HTTP
sessions
isELIgnored Specifies whether or not EL expression within the JSP page will
be ignored.
isScriptingEnabled Determines if scripting elements are allowed for use.
7. 7
Include directive:
• The include directive is used to include a file during the translation phase. This
directive tells the container to merge the content of other external files with the
current JSP during the translation phase. You may code include directives
anywhere in your JSP page.
• The general usage form of this directive is as follows:
<%@ include file="relative url" >
• If you just specify a filename with no associated path, the JSP compiler assumes that
the file is in the same directory as your JSP.
• You can write XML equivalent of the above syntax as follows:
<jsp:directive.include file="relative url" />
8. 8
taglib directive:
• The JavaServer Pages API allows you to define custom JSP tags that look like
HTML or XML tags and a tag library is a set of user-defined tags that implement
custom behavior.
• The taglib directive declares that your JSP page uses a set of custom tags,Identifies the
location of the library, and provides a means for identifying the custom tags in your JSP.
• The taglib directive follows the following syntax:
<%@ taglib uri=“http://java.sun.com/jsp/jstl/ex" prefix="prefixOfTag" >
where ex=(core | xml | function | sql)
• Where the uri attribute value resolves to a location the container understands and
the prefix attribute informs a container what bits of markup are custom actions.
9. 9
JSP Tags:
• There are five main tags:
1. Declaration tag
2. Expression tag
3. Directive tag
4. Scriptlet tag
5. Action tag
10. 10
Declaration tag :
• This tag allows the developer to declare variables or methods.
• Before the declaration you must have <%! And at the end of the declaration the
developer must have %>.
• Code placed in this must end in a semicolon(;)
• Declarations do not generate output, so are used with JSP expressions or scriptlets.
• Example:
<%!private int counter = 0 ;
private String getAccount (int accountNo);
%>
11. 11
Expression tag:
• This tag allows the developer to embed any java expression and is short for
out.println().
• A semicolon (;) does not appear at the end of the code inside the tag.
• Example:
<%= new java.util.Date() %>
12. 12
Directive tag:
• A JSP directive gives special information about the jsp page , to the JSP Engine.
• There are three main types of directives:
1. page - processing information for this page
2. Include - files to be included
3. Tag library - tag library to be used in this page
• Directives do not produce any visible output when the page is requested but change the
way the JSP engine processes the page.
• For example , you can make session data unavailable to a page by setting a page
directive (session) to false.
13. 13
Scriptlet tag:
• Between <% and %> tags , any valid Java Code is called a Scriptlet.
• This code can access any variable or bean declared.
• Example:
• <%
String message = “Be in Peace” ;
out.println(message);
%>
14. 14
Action tag:
• Standard Actions are tags that affect the runtime behavior of the JSP and the response
sent back to the user.
• They have to provided by the container irrespective of the usage.
• During compilation into the servlet , the container comes across the this tag and
replaces it with Java code that corresponds to the required predefined task.
• There are three main roles of the action tags:
• Enable the use of the server side Javabeans.
• Transfer control between pages
• Browser independent support for applet
15. 15
Contd…
• Types of the Standard Action :
• <jsp:usebean>
• <jsp:setProperty>
• <jsp:getProperty>
• <jsp:param>
• <jsp:include>
• <jsp:forward>
• <jsp:plugin>
16. 16
Examples Of JSP :
o Action Tag:
• DatePage.jsp
<html>
<body><%= new java.util.Date () %></body>
</html>
• IncludePage.jsp
<html>
<body><h4> Today’s Date is :<jsp:include page=“DatePage.jsp” flush=“true” /> </h4>
<% out.println(“<h4> The ouput of the file DatePage.jsp is shown above </h3>”); %>
</body>
</html>