Advanced Java: Mca 4 Sem Unit Iv Part 1st
Advanced Java: Mca 4 Sem Unit Iv Part 1st
Advanced Java: Mca 4 Sem Unit Iv Part 1st
◦ Page
◦ Include
◦ Taglib
◦ Attribute
◦ Tag
◦ Variable
Page directive:
This directive can only be used in JSP pages, not tag files. It
defines page dependent attributes, such as scripting language,
error page, buffer requirements.
Syntax:
<%@ Page [autoflush=”true/false”]
[buffer=”skb/NNkb/None] [ContentType=”MIMEType”]
[errorPage=”page or ContextRelativePath”]
[extends=”classname”] [import=”packagelist”] [info=”info” ]
[isErrorPage=”true/false”] [isThreadSafe=”true/false”]
[language=”java/language”] [pageEncoding=”encoding”]
[session=”true/false”] %>
Example:
<%@ page language=”java” ContentType=”text/html” %>
Include directive:
It includes a static file, merging its content with the including
Syntax:
<%@ include file=”page or contextRelativePath” %> Example:
<%@ include file=”home.html” %>
Taglib directive:
Declares a tag library, containing custom actions that are used in the page.
Syntax: <%@
taglib prefix=”prefix” [Uri=”tagliburi/ tagdir =”contextRelativePath”]
%>
Example:
<%@ taglib prefix=”ora” Uri=”orataglib” %>
<%@ taglib prefix=”mylib” tagdir=”/WEB-INF/tags/mylib” %>
Attribute directive:
This directive can only be used in tag files. It declares the
attributes that tag file supports.
Syntax:
<%@ attribute name=”attrname” [description=”desc”]
[required=”true/false”]
[fragment=”true/false”/type=”attrDataType”] %>
Example:
<%@ attribute name=”date” type=”java.util.Date” %>
Tag directive:
This directive can only be used in tag files. Syntax:
<%@ tag [body-
content=”empty/scriptless/tagdependent”]
[description=”desc”] [display- name=”displayname”]
[dynamic-attributes=”attrcolvar”]
[import=”packagelist”] [language=”java/language”]
[page-encoding=”encoding”] %>
Example:
<%@ tag body-content=”empty” %>
Variable directive:
It is similar to variable declarations.
Syntax:
<%@ variable name-given=”attrname”/name-from-
attribute=”attrname” alias=”varname”
%>
Sample program for directive elements: Scripting.jsp file:
<%@ page import="java.util.Date"%>
<html>
<head><title>Scripting elements example</title></head>
<body>
<%! int count=0; %>
<% count++;
out.println("You are accessing this page for "+count+ "times");
out.println(new Date().toString());
%>
<%= count %>
</body>
</html> Web.xml file:
<web-app>
</web-app>
Output:
Java Beans
Java beans are reusable components. We can use simple java beans in a JSP. This helps us
in keeping the business logic separate from presentation logic. Beans are used in the JSP
pages as instance of a class. We must specify the scope of the bean in the JSP page. Here
scope of the bean means the range time span of the bean for its existence in the page.
There are various scopes using which the bean can be used in the JSP page.
Page scope: The bean object gets disappeared as soon as the current page gets discarded.
The default scope for a bean in the JSP page is page scope.
Request scope: the bean object remains in the existence as long as the request object is
present.
Session scope: A session can be defined as the specific period of time the user spends in
browsing the site. Then the time spent by the user in starting and quitting the site is one
session.
Application scope: during application scope the bean will gets stored to ServletContext.
Hence particular bean is available to all the servlets in the same web application.
Application scope is the broadest scope provided by the JSP.
Object Scopes
<jsp:setProperty> This tag is used to set the property value for java bean
<jsp:getProperty> This tag is used to get the property value from java bean
<jsp:param> For adding the specific parameter to the request this tag is used.
<jsp:forward> This tag helps in forwarding the current request to
servlet or to JSP page.
<jsp:plugin> This tag is used to generate the HTML code and to embed
the applet into
it.
<jsp:text> This tag is used to handle template text. When JSP pages are
written as
XML documents then this tag is used.
The objects which we access in our JSP page, without
any explicit declaration are called as implicit objects.
Implicit objects are exposed by the JSP container and can
be seen in the generated servlet of a JSP page.
Implicit objects are advantageous because, they don’t
require the JSP authors to explicitly declare and initialize
a few of the servlet objects, which is a difficult task.
Object Class/Interface Description