AOT Unit 4
AOT Unit 4
Dynamic contents means the contents that get changed based on the user inputs or states of external
system or on some runtime conditions. JSP helps in handling such conditions. There are various ways
by which JSP handles the dynamic contents such as use of:
1.
Directive elements
2.
Java Beans
3.
Scripting elements
4.
Standard tag libraries
5.
Standard and custom actions
6.
Expression language.
1. Directive Elements:
Directive elements are used to specify the information about the page.
Syntax:
<%@ directivename %>
The directive names and attribute names are case sensitive.
Examples of some directives are:
Page
Include
Taglib
Attribute
Tag
Variable
i. Page directive:
This directive can only be used in JSP pages, not tag files. It defines page dependentattributes,
such as scripting language, error page, buffer requirements.
Syntax:
<%@ Page
or
%>
Example:
<%@ page %>
It includes a static file, merging its content with the including page before the combinedresults is
converted to JSP page implementation class.
Syntax:
Example:
taglib tagdir
%>
Example:
This directive can only be used in tag files. It declares the attributes that tag file supports.
Syntax:
<%@ attribute
%>
Example:
v. Tag directive:
This directive can only be used in tag files.
Syntax:
<%@ tag [body- [display-
[dynamic-
[page-
Example:
<%@ tag body- %>
Scripting.jsp file:
<html>
<% count++;
out.println(new Date().toString());
%>
</body>
</html>
Web.xml
file:
<web-app>
</web-app>Output:
2. 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.
i. 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.
ii. Request scope: the bean object remains in the existence as long as the request object is
present.
iii. Session scope: A session can be defined as the specific period of time the user spendsin
browsing the site. Then the time spent by the user in starting and quitting the site is one
session.
iv. 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.
Object Scopes
Before we look at JSP syntax and semantics, it is important to understand thescope or visibility of
Java objects within JSP pages that are processing a request.
Objects may be created implicitly using JSP directives, explicitly through actions,or, in rare cases,
directly using scripting code. The instantiated objects can be associated with a scope attribute
defining where there is a reference to the object and when that reference is removed. The following
diagram indicatesthe various scopes that can be associated with a newly created object:
Getname.html file:
<HTML>
<BODY>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
Nextpage.jsp file:
<HTML>
<BODY>
You entered<BR>
</BODY>
</HTML>
Savename.jsp file:
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
Web.xml file:
<web-app>
</web-
app>
Output:
3. Scripting elements:
Scripting allows us to embed java code in a JSP page and scripting elements (scriptlets,
declaration and expressions) allows us to do scripting. Scripting elements are executed at
request processing time and are used for a variety of purposes including manipulation of objects,
perform calculation on runtime variables values, etc.
4. Standard tag libraries:
JSTL stands for JSP standard tag libraries. This tag library is useful for performing some
common task such as condition execution, loop execution, data processing and so on. JSTL
allows the programmer to embed the logic in JSP page without using java code.The tag library
makes use of some standard set of tags. To install JSTL in tomcat just copy the jstl.jar and
standard.jar files in the lib folder of WEB-INF directory to your tomcat. Some tags are :
<c:out>, <c:set>, <c:if>, <c:choose> etc.
Expression language is all about generating dynamic content, without indulging in the
complexity of a programming language. Hence it is used to write script-free pages. It is used by the
JSP programmer in order to avoid the usage of java code for accessing data. The EL statements are
always used within along with the prefix $.
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.
declare and initialize a few of the servlet objects, which is a difficult task.
Sometimes to built the complex logic we require conditional statements. We can embed JAVA
conditional statements in JSP. This task can be done by scriptlets. The syntax for scriptlets is:
<html>
<head><title>Introduction to scriptlet</title></head>
<body>
<h1>
</h1>
</body>
</html>
Web.xml
file:
<web-app>
</web-
app>
Output:
Displaying Values Using An Expression To Set An Attribute:
The JSP expressions are used to insert java values directly into the output. The syntax ofusing
expression is as follows:
This expression gets evaluated at runtime and then the result will get displayed on the web
browser. Thus tags <%= and %> is used.
Scripting.jsp file:
<html>
<body>
<% count++;
out.println(new Date().toString());
%>
<%= count %>
</body>
</html>
Web.xml
file:
<web-app>
</web-app>
Directory
structure:
Output:
There are some predefined variables that can be used while defining the expressions. These
variables can be used along with the implicit object.
JSP allow us to use the user defined variables and methods using declaration. The variables and
methods can be used along with scriptlets and expression. The syntax for declaration is as follows:
return msg;
%>
<html>
<head>
<title>Use of Method</title>
</head>
<body>
<%
<br>
</body>
</html>
Web.xml
file:
<web-app>
</web-
app>
Output:
While developing any application we may come across several errors. We may get some
syntactical errors during development of JSP pages.
<html>
date is:
<%= new Date().toString() %>
</body>
</html>
Error will
be:
Debugging JSP actions:
Sometimes after fixing all the syntactical errors, still the application does not work properly
due to logical errors. Logical errors may not be highlighted by the tomcat explicitly. The runtime errors
can be handled by using exceptions and gracefully the JSP application can be terminated. Types:
Logical error
Dealing with runtime errors
Catch exceptions
Normally any web application is a client server application and it requires to handle multiple
pages. These multiple pages may access same kind of information. When multiple pages access the
same kind of information, the most required thing is data consistency. When particular request is being
processing the data should remain same. While handling any web application JSP technologies allow
some kind of partitions.
The logic is classified into three partitions:
Request processing
Business logic
Presentation logic.
We will make use of bean class in which the methods for getting and setting the counter valueare
written. There will be three JSP pages. On the first JSP page we will use the instance of a bean and
increment the counter value, when the counter reaches the value 100 then the control will be passed to
another JSP page using <jsp:forward> action. From their the control will be passed to third page by
using the hyperlink used in second page.
There are some web applications in which user moves from one page to another, then it becomes
necessary for data consistency. To achieve this we use an implicit object called session.Using session
we can save the data of a particular page.
Step-1: we will create the main page on which we will accept username and password.
first_page.jsp file:
<%@ %>
<html>
<head>
<title>registration form</title>
</head>
<body>
<form
<strong>User name:</strong>
<input
<strong>Password:</strong>
<input
<input
</form>
</body>
</html>
second_page.jsp
file:
<%@ page %>
<%
String
%>
<html>
<head>
<title>session object</title>
</head>
<body>
</h3>
</body>
third_page.jsp
file:
<%@ page %>
<%
String
%>
<html>
<body>
<br>
</body>
</ht
ml>
Outp
ut:
Sharing data between JSP pages using application data:
Data need to be shared between different components types like servlets, filters.
JSP pages when they need by any application. This is used by servlets while creating
java beans and passing them to JSP pages that are responsible for displaying those
beans.