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 personal details form to the user. In addition, the form should provide a Submit and Reset button. Display the details entered by the user after the user clicks the Submit button.

Solution:
The files used to run the application are:
1. Main.jsp
2. Personal.jsp
3. Details.jsp
//Main.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="Personal.jsp" flush="true"/>
</body>
</html>
//Personal.jsp
<html>
<head>
<title>Personal Details Form</title>
</head>
<body>
<Center><h1>
Personal Details form
</h1></Center>
<form action="Details.jsp" method="post">
First Name:
<input type="text" name="fname">
<br><br>
Last Name:
<input type="text" name="lname">
<br><br>
Address:<br>


<textarea name=address rows=3 cols=15></textarea>
<br><br>
Phone Number:
<input type="text" name="phone">
<br><br>
City:


<input type="text" name="city">
<br><br>
State:

<input type="text" name="state">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
//Details.jsp
<html>
<head>
<title>Example of Implicit objects</title>
</head>
<body>
<font face=Times New Roman size=3>
Thank you for your submission. Confirm the details:
<br><br>
<%
String sfName = request.getParameter("fname");
String slName = request.getParameter("lname");
String saddress = request.getParameter("address");
String sphone = request.getParameter("phone");
String scity = request.getParameter("city");
String sstate = request.getParameter("state");
%>
First Name:<%=sfName%><br>
Last Name:<%=slName%><br>
Address:<%=saddress%><br>
Phone:<%=sphone%><br>
City:<%=scity%><br>
State:<%=sstate%><br>
</font>
</body>
</html>
The output appears as shown in Figure 10.4.
Figure 10. 4: Output of Main.jsp

When the user clicks on Submit button after entering the details, the output appears as shown in Figure 10.5.
Figure 10.5: Output of Details.jsp

0 nhận xét: