0% found this document useful (0 votes)
21 views

Unit 3 Java Server Pages (JSP) AWT

1. Java Server Pages (JSP) allow mixing static HTML with dynamically generated content by enclosing Java code in special tags. 2. JSP pages are translated into servlets behind the scenes, so they provide an easier way to write servlets while separating presentation from logic. 3. Some benefits of JSP over plain servlets include easier HTML authoring and maintenance, ability to use standard web tools, and ability to split development teams for frontend and backend.

Uploaded by

Stick8r None
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit 3 Java Server Pages (JSP) AWT

1. Java Server Pages (JSP) allow mixing static HTML with dynamically generated content by enclosing Java code in special tags. 2. JSP pages are translated into servlets behind the scenes, so they provide an easier way to write servlets while separating presentation from logic. 3. Some benefits of JSP over plain servlets include easier HTML authoring and maintenance, ability to use standard web tools, and ability to split development teams for frontend and backend.

Uploaded by

Stick8r None
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT # 3: JAVA SERVER PAGES (JSP)

 INTRODUCTION
Java Server Pages (JSP) technology enables you to mix regular, static HTML with dynamically
generated content. You simply write the regular HTML in the normal manner, using familiar
Web-page-building tools. You then enclose the code for the dynamic parts in secial tags, most of
which start with <% and end with %>

Ex: Following code uses a request parameter to display the title of a book. Notice that the listing
is mostly standard HTML, the dynamic code consists entirely of the half line as shown below;

Code: OrderConfirmation.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE>
Order Confirmation
</TITLE>
<LINK REL=STYLESHEET HREF=”JSP-Styles.css” TYPE=”text/css”>
</HEAD>
<BODY>
<H2> Order Cofirmation </H2>
Thanks for ordering
<I> <%=request.getParameter(“title”)%> </I>!
</BODY>
</HTML>

You can think of Servlets as Java code with HTML inside; and of JSP as HTML with Java code
inside. Neither Servlets nor JSP pages are restricted to using HTML, but they usually do and this
over-simplified description is a common way to view the technologies.

Despite the large apparent differences between JSP pages and Servlets, behind the scenes they
are the same thing. JSP pages are translated into servlets, the servlets are compiled, and at
request time it is the compiled servlets that execute. So, writing JSP pages is really just another
way of writing Servlets.

Even though servlets and JSP pages are equivalent behind the scenes, they are not equally useful
in all situations. Separating the static HTML from the dynamic content provides a number of
benefits over servlets alone, and the approach used in Java Server Pages offers several
advantages over competing technologies.
 UNDERSTANDING THE NEED FOR JSP
Servlets are convenient to write and efficient to execute. They make it simple to read request
parameters and to set up custom code to handle missing and malformed data. They can easily
make use of HTTP request headers and can flexibly manipulate HTTP response data. They can
customize their behavior based on cookies, track user-specific data with the session tracking API
and talk to relational databases with JDBC.
Servlets are indeed useful and JSP by no means makes them obsolete. Servlets excel at all tasks
related to programming or data processing. But servlets are not so good at presentation. Servlets
have the following deficiencies;
1. It is hard to write and maintain the HTML
2. You cannot use standard HTML tools
3. The HTML is inaccessible to non-Java developers
JSP pages are translated into servlets. So, fundamentally, any task JSP pages can perform could
also be accomplished by servlets. However, this underlying equivalence does not mean that
servlets and JSP pages are equally appropriate in all scenarios. The issue is not the power of the
technology, it is the convenience, productivity, and maintainability of one or the other. After all,
anything you can do on a particular computer platform in the Java programming language you
could also do in assembly language. But it still matters which you choose.
JSP provides the following benefits over servlets alone.
1. It is easier to write and maintain the HTML
2. You can use standard website development tools
3. You can divide up your development team.

 COMPARING JSP TO OTHER TECHNOLOGIES


1. JSP Versus .NET and Active Server Pages(ASP)
.NET is well-designed technology from Microsoft. ASP.NET is the part that
directly competes with servlets and JSP.
Advantage:
1. JSP is portable to multiple operating systems and Web servers. The core .NET
platform runs on a few non-Windows platform.
2. JSP has a clear advantage for the dynamic code. With JSP the dynamic part is
written in Java. So JSP is moe powerful and better suited to complex
applications that require reusable components.
2. JSP Versus PHP
PHP (a recursive acronym for “PHP:Hypertext Preprocessor) is a free, open-
source. HTML-embeddedscripting language that is somewhat similar to both ASP
and JSP.
Advantage:
1. The dynamic part in JSP is written in Java, which already has an extensive
API for networking, database access, distributed objects and the like. Whereas
PHP requires learning an entirely new, less widely used language.
2. JSP is much more widely supported by tool and server vendors than in PHP.
3. JSP Versus Pure Servlets
JSP doesn’t provide any capabilities that couldn’t, in principle, be
accomplished with servlets. JSP documents are automatically translated into
servlets behind the scenes.
Advantage:
1. JSP is more convenient to write and modify
2. By separating the presentation from the content, you can put different
people on different tasks.
Need for Servlets
1. JSP pages get translated into servlets
2. JSP consists of static HTML, special purpose JSP tags and Java code
3. Some tasks are better accomplished by servlets than by JSP
4. Some tasks are better accomplished by a combination of servlets and JSP
than by either servlets or JSP alone.
4. JSP versus JavaScript
JavaScript is completely distinct from the Java programming language and is
normally used to dynamically generate HTML on the client, building parts of
the web page as the browser loads the document. This is a useful capability
and does not normally overlap with the capabilities of JSP which runs only on
the server.
Advantage:
1. JSP pages include SCRIPT tags forJavaScript, just as normal HTML
pages do.
2. JSP can even be used to dynamically generate the JavaScript that will be
sent to the client.
3. JavaScript is not a competing technology ; it is a Complementary one.

 INSTALLING JSP PAGES


Servlets require you to set your CLASSPATH, use packages to avoid name conflicts,
install the class files in servlet-specific locations, and use special-purpose URLs. Not so
with JSP pages. JSP pages can be placed in the same directories as normal HTML pages,
images, and style sheets; they can also be accessed through URLs of the same form as
those for HTML pages, images, and style sheets. Here are a few examples of default
installation locations (i.e; locations that apply when you aren’t using custom Web
applications) and associated URLs. Where it si listed SomeDirectory you can use any
directory name you like, except you are not allowed to use WEB-INF or META-INF as
directory names. When using the default Web application, you also have to avoid a
directory name that matches the URL prefix of any other Web application.
JSP Directories for Tomcat
 Main Location.
install_dir/webapps/ROOT
 Corresponding URL.
http://host/SomeFile.jsp
 More Specific Location (Arbitrary Subdirectory)
install_dir/webapps/ROOT/SomeDirectory
 Corresponding URL
http://host/SomeDirectory/SomeFile.jsp

JSP Directories for JRun


 Main Location.
install_dir/servers/default/default-ear/default-war
 Corresponding URL.
http://host/SomeFile.jsp
 More Specific Location (Arbitrary Subdirectory)
install_dir/servers/default/default-ear/default-war/SomeDirectory
 Corresponding URL
http://host/SomeDirectory/SomeFile.jsp

JSP Directories for Resin


 Main Location.
install_dir/doc
 Corresponding URL.
http://host/SomeFile.jsp
 More Specific Location (Arbitrary Subdirectory)
install_dir/doc/SomeDirectory
 Corresponding URL
http://host/SomeDirectory/SomeFile.jsp
Note that although JSP pages themselves need no special installation directories, any Java classes
called from JSP pages still need to go in the standard locations used by servlet classes (eg:
/WEB-INF/classes/directoryMatchinPackageName). Note that the Java classes used by JSP
pages should always be in packages.

 CREATING TEMPLATE TEXT


In most cases, a large percentage of your JSP document consists of static text (usually
HTML), known as template text. In almost all respects, this HTML looks just like normal
HTML, follows all the same syntax rules, an is simply “passed through” to the client by
the servlet created to handle the page. Not only does the HTML look normal, it can be
created by whatever tools you already are using for building Web pages.
There are 2 minor exceptions to the “template text is passed straight through” rule.
1. If you want to have <% or %> in the output, you need to put <\% or %\> in the
template text.
2. If you want a comment to appear in the JSP page but not in the resultant document ,
use
<%--JSP Comment--%>
HTML comments of the form
<!—HTML Comment -->
Are passed through to the client normally
 INVOKING JAVA CODE FROM JSP
There are a number of different ways to generate dynamic content from JSP. Each of
these approaches has a legitimate place; the size and complexity of the project is the most
important factor in deciding which approach is appropriate. However, be aware that
people err on the side of placing too much code directly in the page much more often
than they err on the opposite end of the spectrum. Although putting small amounts of
Java code directly in JSP pages works fine for simple applications, using long and
complicated blocks of Java code in JSP pages yields a result that is hard to maintain, hard
to debug, hard to reuse, and hard to divide among different members of the development
team.
Strategies for invoking dynamic code from JSP.
1. Call Java code directly:
Place all Java code in JSP page. Appropriate only for very small amounts of code
2. Call Java code indirectly
Develop separate utility classes. Insert into JSP page only the Java code needed to
invoke the utility classes
3. Use beans
Develop separate utility classes structured as beans. Use jsp:useBean, jsp:getProperty,
and jsp:setProperty to invoke the code
4. Use the MVC Architecture
Have a servlet respond to original request, look up data, and store results in beans.
Forward to a JSP page to present results. JSP page uses beans
5. Use the JSP expression language
Use shorthand syntax to access and output object properties. Usually used in
conjunction with beans and MVC
6. Use custom tags
Develop tag handler classes. Invoke the tag handlers with XML-like custom tags.

 LIMITING THE AMOUNT OF JAVA CODE IN JSP


1. Development
You generally write regular classes in a Java-oriented environment(ex: an IDE like
JBuilder or Eclipse or a code editor like UltraEdit or emacs). You generally write JSP
in an HTML-oriented environment like Dreamweaver. The Java-oriented
environment is typically better at balancing parentheses, providing tooltips, checking
the syntax, colorizing the code,and so on.
2. Compilation
To compile a regular Java class, you press the Build button in your IDE or invoke
javac. To compile a JSP page, you have to drop it in the right directory, start the
server, open a browser and enter appropriate URL.
3. Debugging
When we write Java classes or JSP pages, we occasionally make syntax errors. If
there is a syntax error in a regular class definition, the compiler tells you right away
and it also tells you what line of code contains the error. If there is a syntax error in a
JSP page the server typically tells you what line of the servlet contains the error. For
tracing output at runtime you can use print statements but where those statements are
displayed varies from server to server
4. Division of Labor
Many large development teams are composed of some people who are experts in Java
language and others who are experts in HTML
5. Testing
6. Reuse
 USING JSP EXPRESSION
A JSP expression is used to insert values directly into the output. It has the following syntax;
<%= Java Expression %>
The expression is evaluated, converted to a string, and inserted in the page. This evaluation is
performed at runtime (when the page is requested) and thus has full access to information about
the request.
Ex: To show the Date/Time that the page has requested
Current Time: <%= new java.util.Date() %>
 JSP EXPRESSION
The code placed within JSP expression tag is written to the output stream of the response. So
you need not write out.print() to write data. It is mainly used to print the values of variable or
method.
Syntax of JSP expression tag
<%= statement %>
Example of JSP expression tag
In this example of jsp expression tag, we are simply displaying a welcome message.

<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of expression tag.
Example of JSP expression tag that prints current time
To display the current time, we have used the getTime() method of Calendar class. The
getTime() is an instance method of Calendar class, so we have called it after getting the instance
of Calendar class by the getInstance() method.
index.jsp

<html>
<body>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
Example of JSP expression tag that prints the user name
In this example, we are printing the username using the expression tag. The index.html file gets
the username and sends the request to the welcome.jsp file, which displays the username.
File: index.jsp

<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp

<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>

 WRITING SCRIPLETS
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
<% java source code %>
Example of JSP scriptlet tag
In this example, we are displaying a welcome message.

<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>

Example of JSP scriptlet tag that prints the user name


In this example, we have created two files index.html and welcome.jsp. The index.html file gets
the username from the user and the welcome.jsp file prints the username with the welcome
message.
File: index.html

<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp

<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>

 SCRIPLET EXAMPLES
A JSP page that uses the bgColor request Parameter to set the background color of the page;
<! DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> COLOR TESTING </TITLE>
</HEAD>
<%
String bgColor=request.getParameter(“bgColor”);
if ((bgColor==null) || (bgColor.trim().equals(“”)))
{
bgColor=”WHITE”;
}
%>
<BODY BGCOLOR=”<%=bgColor %>”>
<H2 ALIGN=”CENTER”> TESTING A BACKGROUND OF “<%=bgColor %>”
</H2>
</BODY>
</HTML>

 SCRIPLETS FOR CONDITIONAL EXECUTION


Another use of Scriplets is to conditionally output HTML or other content that is not within any
JSP tag. Key to this approach are the facts that a code inside a scriplet gets inserted into the
resultant servlet’s _jspService method (called by service) and that any static HTML (template
text) before or after a scriplet gets converted to print statements. This behaviour means that
Scriplets does not contain complete Java statements and that code blocks left open can affect the
static HTML or JSP outside the Scriplet
Ex:

<! DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>


<HTML>
<HEAD>
<TITLE> Wish For The Day </TITLE>
<LINK REL=STYLESHEET HREF=”JSP-STYLES.CSS” TYPE=”TEXT/CSS”>
</HEAD>
<BODY BGCOLOR=”<%=bgColor %>”>
<%
if(Math.random() < 0.5)
{ %>
<H1 ALIGN=”CENTER”> HAVE A NICE DAY…!!!!</H1>
<%
}
else
{ %>
<H1 ALIGN=”CENTER”> HAVE A LOUSY DAY…!!!!</H1>
<%
}
%>
</BODY>
</HTML>
JSP Elements:
We will learn about the various elements available in JSP with suitable examples. In JSP
elements can be divided into 4 different types.
These are:
 Expression
 Scriplets
 Directives
 Declarations
1. Expression:
We can use this tag to output any data on the generated page. These data are automatically
converted to string and printed on the output stream.
Syntax: JSP Expressions are : <%=”Anything” %>
JSP Expressions start with Syntax of JSP Scriptles are with <%=and ends with %>. Between
these, you can put anything that will convert to the String and that will be displayed.
Example:
<%="HelloWorld!" %> Above code will display 'HelloWorld!'
2. Scriplets:
In this tag we can insert any amount of valid java code and these codes are placed in the _jsp
Service method by the JSP engine.
Syntax:<%//java codes%>
JSP Scriptlets begins with <% and ends %> . We can embed any amount of Java code in the JSP
Scriptlets. JSP Engine places these codes in the _jspService() method.
Variables available to the JSP Scriptlets are:
 Request
 Response
 Session
 Out
3. Directives:
A JSP “directive” starts with <%@ characters. In the directives, we can import packages , and
define error-handling pages or the session information of the JSP page.
Syntax:<%@directive attribute="value"% >
 page
 include
 taglib
4. Declarations:
This tag is used for defining the functions and variables to be used in the JSP.
Syntax:
<%!
//java codes
%>
JSP Declaratives begins with <%! and ends %> with We can embed any amount of java code in
the JSP Declaratives. Variables and functions defined in the declaratives are class-level and can
be used anywhere on the JSP page.
Example :
<%@page import="java.util.*"%>
<HTML>
<BODY>
<%!
Date the Date=new Date(); Date getDate()
{
System.out.println("In getDate() method"); return theDate;
}
%>
Hello! The time is now<%=getDate()%>
</BODY>
</HTML>
Explain about Jsp programs.
A Web Page with JSP code
<HTML>
<HEAD>
<TITLE>A Web Page</TITLE>
</HEAD>
<BODY>
<%out.println("Hello there!");%>
</BODY>
</HTML>

 USING DECLARATION EXAMPLE


The JSP declaration tag is used to declare fields and methods.
The code written inside the jsp declaration tag is placed outside the service() method of auto
generated servlet.
So it doesn't get memory at each request.
Syntax of JSP declaration tag

<%! field or method declaration %>


Difference between JSP Scriptlet tag and Declaration tag
Jsp Scriptlet Tag Jsp Declaration Tag
The jsp scriptlet tag can only declare variables not The jsp declaration tag can declare variables as well as
methods. methods.
The declaration of scriptlet tag is placed inside the The declaration of jsp declaration tag is placed outside
_jspService() method. the _jspService() method.

Example of JSP declaration tag that declares field


In this example of JSP declaration tag, we are declaring the field and printing the value of the
declared field using the jsp expression tag.
index.jsp

<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>

Example of JSP declaration tag that declares method


In this example of JSP declaration tag, we are defining the method which returns the cube of
given number and calling this method from the jsp expression tag. But we can also use jsp
scriptlet tag to call the declared method.
index.jsp

<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>

 USING PREDEFINED VARIABLES


For JSP expressions and scriplets to be useful, you need to know what variable names the
autogenerated servlet uses. You are supplied with 8 automatically defined local variables in
_jspService, sometimes called as “implicit objects”. These are names of local variables not
constants or reserved words. These variables are not accessible in declarations. Following are the
available variables;
1. request
This variable is the HttpServletRequest associated with the request; it gives you access to
the request parameters, the request type (Ex: GET or POST) and the incoming HTTP
headers (Ex: Cookies).
2. response
This variable is the HttpServletResponse associated with the response to the client. Since
the output stream is normally buffered, it is usually legal to set HTTP status codes and
response headers in the body of JSP pages, even though the setting of headers or status
codes is not permitted in servlets once any output has been sent to the client. If you turn
buffering off, however, you must set status codes and headers before supplying any
output.
3. out
This variable is the Writer used to send output to the client. However, to make it easy to
set response headers at various places in the JSP page, out is not the standard PrintWriter
but rather a buffered version of Writer called JspWriter. You can adjust the buffer size
through use of the buffer attribute of the page directive. The out variable is used almost
exclusively in scriplets since JSP expressions are automatically placed in the output
stream and thus rarely used to refer to out explicitly.
4. session
This variable is the HttpSession object associated with the request. Recall that sessions
are created automatically in JSP, so this variable is bound even if there is no incoming
session reference. The one exception is the use of the session attribute of the page
directive to disable automatic session tracking. In that case, attempts to reference the
session variable cause errors at the time the JSP page is translated into a servlet.
5. application
This variable is the ServletContext as obtained by getServletContext. Servlets and JSP
pages can store persistent data in the ServletContext object rather than in instance
variables. ServletContext has setAttribute and getAttribute methods that let you store
arbitrary data associated with specified keys. The difference between storing data in
instance variables and storing it in the ServletContext is that the ServletContext is shared
by all servlets and JSP pages in the Web application, whereas instance variables are
available only to the same servlet that stored the data.
6. config
This variable is the ServletConfig object for the page. In principle, you can use it to read
initialization parameters, but, in practice, initialization parameters are read from jspInit,
not from _jspService.
7. pageContext
JSP introduced a class called PageContext to give a single point of access to many of the
page attributes. The PageContext class has methods getRequest, getResponse, getOut,
getSession, and so forth. The pageContext variable stores the value of the PageContext
object associated with the current page. If a method or constructor needs access to
multiple page-related objects, passing pageContext is easier than passing many separate
references to request, response, out and so forth.
8. page
This variable is simply a synonym for this and is not very useful. It was created as a
placeholder for the time when the scripting language could be something other than Java.
 INCLUDING PAGES AT REQUEST TIME

THE jsp:include Action


The jsp:include action tag is used to include the content of another resource it may be jsp, html
or servlet.
The jsp include action tag includes the resource at request time so it is better for dynamic
pages because there might be changes in future.
The jsp:include tag can be used to include static as well as dynamic pages.
Advantage of jsp:include action tag
Code reusability : We can use a page many times such as including header and footer pages in all
pages. So it saves a lot of time.

Difference between jsp include directive and include action

JSP include directive JSP include action

includes resource at translation time. includes resource at request time.

better for static pages. better for dynamic pages.

includes the original content in the generated servlet. calls the include method.

Syntax of jsp:include action tag without parameter


<jsp:include page="relativeURL | <%= expression %>" />

Syntax of jsp:include action tag with parameter


<jsp:include page="relativeURL | <%= expression %>">
<jsp:param name="parametername" value="parametervalue | <%=expression%>" />
</jsp:include>

Example of jsp:include action tag without parameter


In this example, index.jsp file includes the content of the printdate.jsp file.
File: index.jsp

<h2>this is index page</h2>


<jsp:include page="printdate.jsp" />
<h2>end section of index page</h2>
File: printdate.jsp

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>


Output:

 INCLUDING FILES AT PAGE TRANSLATION TIME: THE include Directive


The jsp directives are messages that tells the web container how to translate a JSP page into the
corresponding servlet.
There are three types of directives:
 page directive
 include directive
 taglib directive

THE include Directive


The include directive is used to include the contents of any resource it may be jsp file, html file
or text file. The include directive includes the original content of the included resource at page
translation time (the jsp page is translated only once so it will be better to include static
resource).

Advantage of Include directive


Code Reusability

Syntax of include directive


<%@ include file="resourceName" %>

Example of include directive


In this example, we are including the content of the header.html file. To run this example you
must create an header.html file.

<html>
<body>

<%@ include file="header.html" %>

Today is: <%= java.util.Calendar.getInstance().getTime() %>

</body>
</html>
Note: The include directive includes the original content, so the actual page size grows at
runtime.

 FORWARDING REQUESTS WITH jsp:forward

JSP Action Tags


There are many JSP action tags or elements. Each JSP action tag is used to perform some
specific tasks.
The action tags are used to control the flow between pages and to use Java Bean. The Jsp action
tags are given below.
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.
jsp:param sets the parameter value. It is used in forward and include mostly.
jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.

The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we
will see these tags in bean developement.

jsp:forward action tag


The jsp:forward action tag is used to forward the request to another resource it may be jsp, html
or another resource.
Syntax of jsp:forward action tag without parameter
<jsp:forward page="relativeURL | <%= expression %>" />

Syntax of jsp:forward action tag with parameter


<jsp:forward page="relativeURL | <%= expression %>">
<jsp:param name="parametername" value="parametervalue | <%=expression%>" />
</jsp:forward>

Example of jsp:forward action tag without parameter


In this example, we are simply forwarding the request to the printdate.jsp file.
index.jsp

<html>
<body>
<h2>this is index page</h2>

<jsp:forward page="printdate.jsp" />


</body>
</html>
printdate.jsp

<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>

Example of jsp:forward action tag with parameter


In this example, we are forwarding the request to the printdate.jsp file with parameter and
printdate.jsp file prints the parameter value with date and time.
index.jsp

<html>
<body>
<h2>this is index page</h2>

<jsp:forward page="printdate.jsp" >


<jsp:param name="name" value="javatpoint.com" />
</jsp:forward>

</body>
</html>
printdate.jsp

<html>
<body>

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>


<%= request.getParameter("name") %>
</body>
</html>

 THE jsp:param and jsp:params Element


The jsp:param element is used with jsp:plugin in a manner similar to the way that
PARAM is used with APPLET, specifying a name and value that are accessed from
within the applet by getParameter. There are 2 main differences,
1. Sice jsp:param follows XML syntax, attribute names must be lower case, attribute
values must be enclosed in single or double quotes, and the element must end with />,
and not just >.
2. All jsp:param entries must be enclosed within a jsp:params element.
So for example, you would replace
<APPLET CODE=”MyApplet.class” WIDTH=475 HEIGHT=350>
<PARAM NAME=”PARAM1” VALUE=”VALUE1”>
<PARAM NAME=”PARAM2” VALUE=”VALUE2”>
</APPLET>
With
<jsp:plugin type=”applet” code=”MyApplet.class” width=”475” height=”350”>
<jsp:params>
<jsp:param name=”NAME1” value=”VALUE1” />
<jsp:param name=”NAME2” value=”VALUE2” />
</jsp:params>
</jsp:plugin>
 The jsp:fallback Element
The jsp:fallback element provides alternative text to browsers that do not support OBJECT or
EMBED. You use this element in almost the same way as you would use alternative text placed
within an APPLET element. So, for example, you would replace
<APPLET CODE=”MyApplet.class” WIDTH=475 HEIGHT=350>
<B> Error: This Example requires Java. </B>
</APPLET>
With
<jsp:plugin type=”applet” code=”MyApplet.class” width=”475” height=”350”>
<jsp:fallback>
<B> Error: This Example requires Java. </B>
</jsp:fallback>
</jsp:plugin>

You might also like