Friday, April 17, 2009

Write a program to include an HTML file in a JSP page using RequestDispatcher interface method

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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Include Example</title>
<link rel=STYLESHEET
href="My-Style-Sheet.css" type="text/css">
</head>
<body bgcolor="#FDF5E6" text="#000000" link="#0000EE"
vlink="#551A8B" alink="#FF0000"><P>
<jsp:include page="form.htm" flush="true"/>
</body>
</html>

//form.html
<html>
<head>
<style>
body, input { font-family:Tahoma; font-size:10pt; }
</style>
</head>
<body>
<!-- HTML Form -->
<form action="Welcome.jsp" method="post">
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Enter your Account Id:
<input type="text" name="Acc_Id" /> &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
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 is as shown in Figure 10.1.


Figure 10.1: Output of form.html

When the user enters the details and clicks on Submit button, the output appears as shown in Figure 10.2.

Figure 10.2: Output after clicking Submit button

0 nhận xét: