Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Assignment-5: Jsp:forward Action Tag

The document discusses various JSP action elements and provides examples of how to use them. It includes examples of the jsp:forward and jsp:include action tags. It also provides code samples for JSP programs that display the system date and time, generate a Fibonacci series, display a random weekday based on user input, and demonstrate the use of @page and @include directives.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignment-5: Jsp:forward Action Tag

The document discusses various JSP action elements and provides examples of how to use them. It includes examples of the jsp:forward and jsp:include action tags. It also provides code samples for JSP programs that display the system date and time, generate a Fibonacci series, display a random weekday based on user input, and demonstrate the use of @page and @include directives.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Luhindaar.

1907022

Assignment-5

1) JSP Action element with examples.

Some of the action elements are:

● jsp:forward
● jsp:include

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 %>" />
Example:
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>
jsp:include action tag:
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.
Syntax of jsp:include action tag without parameter
<jsp:include page="relativeURL | <%= expression %>" />

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

2) Write a JSP program shows the System date and time.

Source Code:

<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>


<html>

<head>

<title>Display Current Date & Time</title>

</head>

<body>

<center>

<h1>Display Current Date & Time</h1>

</center>

<%

Date date = new Date();

out.print( "<h2 align = \"center\">" +date.toString()+"</h2>");

%>

</body>

</html>

Output:
3) Write a JSP program shows the Fibonacci series up to a particular term, while the input is taken
from an HTML form.

Source Code:

Input.html

<!DOCTYPE html>

<html>

<head>

<title>Fibonacci Series</title>

<link rel="stylesheet" type="text/css">

<style type="text/css">

body

margin: 0;

padding: 0;

background-color:#6abadeba;

font-family: 'Arial';

.login{

width: 382px;

overflow: hidden;

margin: auto;

margin: 20 0 0 450px;

padding: 80px;

background: #23463f;

border-radius: 15px ;

h2{

text-align: center;
color: #277582;

padding: 20px;

label{

color: #08ffd1;

font-size: 17px;

#Uname{

width: 300px;

height: 30px;

border: none;

border-radius: 3px;

padding-left: 8px;

#Pass{

width: 300px;

height: 30px;

border: none;

border-radius: 3px;

padding-left: 8px;

#log{

width: 300px;

height: 30px;

border: none;

border-radius: 17px;

padding-left: 7px;

color: blue;
}

span{

color: white;

font-size: 17px;

a{

float: right;

background-color: grey;

</style>

</head>

<body>

<h2>Fibonacci Series</h2><br>

<div class="login">

<form id="login" method="get" action="Factorial.jsp">

<label><b>Enter the number of Fibonacci Series

</b>

</label>

<input type="text" name="val" id="val" placeholder="Username">

<input type="submit" value="Submit">

</form>

</div>

</body>

</html>

Factorial.jsp

<html>

<body>
<%!

int n;

String str;

int fibo(int n) {

if(n<2)

return n;

else

return fibo(n-1) + fibo(n-2);

%>

<b>Fibonacci series: </b><br>

<%

str = request.getParameter("val");

n = Integer.parseInt(str);

for(int i=0; i<=n; i++) {

out.print(fibo(i) + " ");

%>

</body>

</html>

Output:
4) Write a  JSP program to get random value using math function and based on the input
from  1 to 7,weekdays need to be displayed.
Source Code:

<html>

<body>

<%@ page import="java.util.Random"%>

<%

Random rand = new Random();

int int1 = rand.nextInt(7);

String[] days = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",


"Saturday","Sunday" };

out.print(days[int1-1]);

%>

</body>

</html>

Output:
5) Write a sample JSP program for @page and @include

Index.jsp

<html>

<head>

<title>

Abis

</title>

</head>

<body>

<h2>Hi, Welcome to Abinandan's Random Day generator</h2>

<p>A random day generated is: </p>

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

<h2>Include & Page Tags used in this page</h2>

</body>

</html>

Printdate.jsp

<%@ page import="java.util.Random"%>

<%

Random rand = new Random();

int int1 = rand.nextInt(7);


String[] days = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday","Sunday" };

out.print(days[int1-1]);

%>

Output:

You might also like