1.
Write a program to display a “Hello World” message in the
Web browser. In addition, display the host name and Session
Id. Write JSP code using HTML code.
Solution:
<html>
<head>
<title>Welcome to our Website</title>
</head>
<body>
<marquee><font size="3" color="#FF0033">Hello World!!
</font></marquee>
<font color = “#0000FF”> Hostname: <%= request.getRemoteHost()
%><br>
<font color = “#0000FF”> Session Id: <%= session.getId() %>
</body>
</html>
2. Write a program using JSP to calculate the sum of numbers
from 1 to 25.
Solution:
<html>
<head>
<title>JSP Example</title>
</head>
<body>
<h1>Displaying sum</h1>
<%
int sum=0;
for(int num=1;num<=25;num++)
{
sum=sum+num;
}
out.println("Sum of numbers from 1 to 25 is : " + sum);
%>
</body>
</html>
The output of the program is as shown:
3. Write a program that uses the response.setHeader()method
to display the date. In addition, set the response header
attribute and utilize the JSP expression tag.
Solution:
//Date.jsp
<html>
<head>
<title>Example of JSP Implicit Object</title>
<%@ page import="java.util.Date" %>
</head>
<body bgcolor=#ffffff>
<font color="Black">
<h2> This example gives the Current Date </h2>
<h3>
<% response.setHeader("Refresh", "6"); %>
Current date: <%= new Date() %>.
</h3>
</body>
</html>
The output of the program is as shown
4. Write a program to display the session ID, creation time, and
the last accessed time of the Web page. Using session.getID,
session.getCreationTime(), and
session.getLastAccessedTime().
Solution:
The file used in this exercise is Expression.jsp.
<html>
<head>
<title> JSP Expressions </title>
</head>
<body bgColor="white">
<h2>Simple JSP Expression</h2>
<ul>
<li> Session id:<%= session.getId() %>
<li> Creation time:<%= new
java.util.Date(session.getCreationTime()) %>
<li> Time of last access: " + <%= new
java.util.Date(session.getLastAccessedTime())%>
</ul>
</body>
</html>
The output of the program is as shown.
5. Write a program to display an error message to the user if an
exception occurs in the JSP page. In the JSP page, consider a
null string and find out the length of the string using
length()method of Java. Create an error handler to handle the
exception thrown by this JSP page.
Solution:
The files used to run the application are:
1. Exception.jsp
2. Example.html
//Exception.jsp
<html>
<body>
<%@ page errorPage="example.jsp" %>
Example for Null Pointer exception:
<%
String s=null;
s.length();
%>
</body>
</html>
//Error handler file
//Example.jsp
<html>
<body>
<%@ page isErrorPage="true" %>
The error is:
<%= exception %>
</body>
</html>
The output of the program is as shown.
6. Write a program using the request.getParameter() method
to enter the Name and Password of a user and display the
output on another JSP page.
Solution:
The files used to run the application are:
User.jsp
UserDisplay.jsp
<html>
<head>
<title>Example of Implicit Objects</title>
</head>
<body>
<h1>User submission form</h1>
<form action="UserDisplay.jsp" method="post">
Enter User Name:
<input type="text" name="uname">
<br>
<br>
Enter Password:
<input type="password" name="pname">
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Save the code as User.jsp
<html>
<head>
<title>Example of Implicit objects</title>
</head>
<body>
<font face=Times New Roman size=3>
Thank you for your submission. Your information has been
successfully added to the database:
<br>
<br>
<%
String sUName = request.getParameter("uname");
String sPName = request.getParameter("pname");
%>
User Name:<%=sUName%><br>
Password:<%=sPName%><br>
</font>
</body>
</html>
Save the code as UserDisplay.jsp
The output of the program:
The user enters the information and clicks the Submit button. The
control is transferred to the UserDisplay.jsp page.
7. Write a program to display the multiples of two. Include a
for loop in the scriptlet block and display the numbers.
Solution:
The file used in this exercise is Series.jsp.
<html>
<body>
<h1>Displaying </h1>
<%
int res;
int j=2;
for (int i=1; i<11; i++)
{
res=j*i;
out.println(j + " * " + i + " = " + res);
out.println("<br>");
}
%>
</body>
</html>
The output of the program
8. Write a program to include an HTML file in a JSP page using
RequestDispatcher interface method. The included file should
display a login form to the user. In addition, the form should
provide a Submit and Reset button. Display a Welcome
message to the user when the user clicks on Submit button
after entering the details.
Solution:
The files used to run the application are:
1. Include.jsp
2. form.html
3. Welcome.jsp
//Include.jsp
<html>
<head>
<title>Include Example</title>
</head>
<P>
<jsp:include page="form.html" flush="true"/>
</body>
</html>
//form.html
<html>
<head>
<style>
body, input { font-family:Tahoma;
font-size:10pt;
}
</style>
</head>
<body>
<form action="Welcome.jsp" method="post">
            Enter your Account Id:
<input type="text" name="Acc_Id" />        
               
Enter your Pin number:
<input type="text" name="Pin_num"/> <br><br><br><br>
<Center><input type="submit" value="Submit"/><Center>
</form>
</body>
</html>
//Welcome.jsp
<%@ page language = "java" import="java.util.*" %>
<html>
<head>
<title>Welcome to our Website</title>
</head>
<body>
<center>
out.println("Welcome to Online Banking");
</center>
</body>
</html>
The output of the program
Output of form.html
When the user enters the details and clicks on Submit button, the
output appears as shown:
Output after clicking Submit button