0% found this document useful (0 votes)
16 views16 pages

AOT Unit 4

The document discusses different ways to generate dynamic content in JSP including using directive elements, Java beans, scripting elements, standard tag libraries, standard and custom actions, and expression language. It provides examples of using directive elements like page, include, taglib attributes. It also discusses scopes of Java beans and provides an example of using beans in JSP. The document is intended to explain concepts related to application development using JSP.

Uploaded by

BABY ABHI RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

AOT Unit 4

The document discusses different ways to generate dynamic content in JSP including using directive elements, Java beans, scripting elements, standard tag libraries, standard and custom actions, and expression language. It provides examples of using directive elements like page, include, taglib attributes. It also discusses scopes of Java beans and provides an example of using beans in JSP. The document is intended to explain concepts related to application development using JSP.

Uploaded by

BABY ABHI RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT-4

JSP APPLICATION DEVELOPMENT

GENERATING DYNAMIC CONTENT:

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 %>

ii. Include directive:

It includes a static file, merging its content with the including page before the combinedresults is
converted to JSP page implementation class.
Syntax:

<%@ include or %>

Example:

<%@ include %>


iii. Taglib directive:
Declares a tag library, containing custom actions that are used in the page.
Syntax: <%@

taglib tagdir

%>

Example:

<%@ taglib %>

<%@ taglib - %>

iv. Attribute directive:

This directive can only be used in tag files. It declares the attributes that tag file supports.

Syntax:

<%@ attribute

%>

Example:

<%@ attribute %>

v. Tag directive:
This directive can only be used in tag files.
Syntax:
<%@ tag [body- [display-
[dynamic-
[page-

Example:
<%@ tag body- %>

vi. Variable directive:


It is similar to variable declarations.
Syntax:
<%@ variable name- -from-
%>

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:

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.

Application scope is the broadest scope provided by the JSP.

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:

Sample program for java beans in jsp:

Getname.html file:
<HTML>

<BODY>

<FORM METHOD=POST ACTION="SaveName.jsp">

What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's

your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>What's your

age? <INPUT TYPE=TEXT NAME=age SIZE=4>

<P><INPUT TYPE=SUBMIT>

</FORM>

</BODY>

</HTML>

Nextpage.jsp file:

<jsp:useBean id="user" class="user.UserData" scope="session"/>

<HTML>

<BODY>

You entered<BR>

Name: <%= user.getUsername() %><BR>

Email: <%= user.getEmail() %><BR>

Age: <%= user.getAge() %><BR>

</BODY>

</HTML>

Savename.jsp file:

<jsp:useBean id="user" class="user.UserData" scope="session"/>


<jsp:setProperty name="user" property="*"/>

<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.

5. Standard and custom actions:


The standard actions are those actions that can be defined by the JSP specification itself.
Following are some of the standard actions:
Action element Description

<jsp:usebean> This tag is used to instantiate the object of java bean


<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:include> This tag works as a sub-routine. It includes the response from the servlet or
JSP when the request is being processed.
<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:attribute> This tag sets the value of action attribute.
<jsp:element> This tag generates the XML elements dynamically.
<jsp:text> This tag is used to handle template text. When JSP pages are written as
XML documents then this tag is used.
<jsp:body> This tag is used to set the body element.

Custom actions allow us to create user- defined tags.


6. Expression language:

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 $.

IMPLICIT JSP OBJECTS:

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.

Object Class/Interface Description


application javax.servlet.ServletConfig Represents the context for the
JSP page servlet, in which
session javax.servlet.http.HttpSession This variable is used to access
current session.
request javax.servlet.http.HttpServletRequest It provides the method for
accessing the information made
by current request.
Response javax.servlet.http.HttpServletResponse It provides the methods related
to adding cookies, session,
setting headers.
PageContent javax.servlet.jsp.PageContent It provides access to several JSP
attributes.
Page Java.lang.object This variable is assigned to
instance of JSP implementation
class.
Out Javax.Servlet.jsp.JspWriter It provides methods related toI/O.

Exception Java.lang.Throwable This object is used for handling error


pages and containinformation about
runtime
errors.
config Javax.servlet.ServletConfig It helps in passing the
information to the servlet or JSP
page during initialization.
Conditional processing:

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:

<% any java code %>

Sample program for conditional processing:

<%@ page %>

<html>

<head><title>Introduction to scriptlet</title></head>

<body>

<h1>

<% is very %>

</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:

<%= Expression code %>

This expression gets evaluated at runtime and then the result will get displayed on the web
browser. Thus tags <%= and %> is used.

Sample program for using an expression:

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>

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.

Declaring Variables And Methods:

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:

<%! Any java code %>

Sample program for declaring variables and methods:


<%@ page language="java" contentType="text/html" %>

<% String msg="Hello"; %>

<%! public String MyFunction(String msg)

return msg;

%>
<html>

<head>

<title>Use of Method</title>

</head>

<body>

<%

out.println("Before function call:"+msg); %>

<br>

After function call: <%= MyFunction("Web Technologies") %>

</body>

</html>

Web.xml

file:

<web-app>

</web-

app>

Output:

Error Handling & Debugging:

While developing any application we may come across several errors. We may get some
syntactical errors during development of JSP pages.

Errors are of two types:


1. Element syntax error
2. Expression language syntax error
1. Element syntax error:
Element syntax errors may encounter due to:
Improperly terminate directive.
Improperly terminate action
Mistyped attribute
Missing endquote in attribute values.
2. Expression language syntax error:
Expression language syntax error may occur due to:
Missing both curly braces
Missing end curly brace
Misspelled property name
Misspelled parameter
name.Sample program for error handling:
<%@ page

<%@ page %>

<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

Passing Control & Data Between Pages:

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.

This three partitioned architecture is known as View Controller (MVC) model.

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.

Sharing data between JSP pages using session object:

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><a here to view the other session</a>

</h3>
</body>
third_page.jsp
file:
<%@ page %>

<%
String
%>

<html>

<head><title>end of session page</title></head>

<body>

<strong>User Name is:<%= username %></strong>

<br>

<strong>Password is:<%= password %></strong>

</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.

Memory usage considerations:


When object gets saved in session scope and application scope they consume
some memory in server process.

You might also like