JSP Exercises
JSP Exercises
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>
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>
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>
Solution:
<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>
Solution:
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>
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>
<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>
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:
<html>
<body>
<h1>Displaying </h1>
<%
int res;
int j=2;
Solution:
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>
//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>
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