You are on page 1of 8

Lab Deliverable 2 Using Java Server Pages (JSP) Tags

Part II

1. 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:

1. User.jsp
2. 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 in the C:\Tomcat 5.5\Webapps\basic directory.

<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");

Using Java Server Pages (JSP) Tags Ver 1.0 2005 Aptech Limited 1
String sPName = request.getParameter("pname");
%>
User Name:<%=sUName%><br>
Password:<%=sPName%><br>
</font>
</body>
</html>

Save the code as UserDisplay.jsp in the C:\Tomcat 5.5\Webapps\basic directory.

The output of the program is as shown in Figure 4.1.

Figure 4.1: Output of User.jsp

The user enters the information and clicks the Submit button. The control is transferred to the
UserDisplay.jsp page.

2 Ver 1.0 2005 Aptech Limited


The output of the program is as shown in Figure 4.2.

Figure 4.2: Output of UserDisplay.jsp

2. 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:

1. The file required to run the application is Date.jsp. The file is to be saved in C:\Tomcat
5.5\Webapps\basic directory.

//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>

Using Java Server Pages (JSP) Tags Ver 1.0 2005 Aptech Limited 3
The output of the program is as shown in Figure 4.3.

Figure 4.3: Output of Date.jsp

4 Ver 1.0 2005 Aptech Limited


Do It Yourself

1. Write a program to retrieve the name and IP address of a remote computer.

Hint: Use the request implicit object to retrieve the values.

Solution:

The file used to run the application is IPAdd.jsp. The file is to be saved in C:\Tomcat
5.5\Webapps\basic directory.

<html>
<head>
<title>System Information</title>
</head>
<body>
<h3>Details of Remote Computer:</h3>

<b>Computer Name:</b>:
<br>
<%=request.getRemoteHost()%>
<br>
<br>

<b>IP Address:</b>:
<br>
<%=request.getRemoteAddr()%>
<br>
</body>
</html>

Using Java Server Pages (JSP) Tags Ver 1.0 2005 Aptech Limited 5
The output of the program is as shown in Figure 4.4.

Figure 4.4: Output of IPAdd.jsp

2. Write a program that passes three parameters to a JSP page. In addition, override a request
parameter when a JSP page is called. Specify the three parameters as param1, param2 and
param3.

Solution:

The files used to run the application are:

1. ParamId.jsp
2. ParamObj.jsp

<html>
<head></head>
<body>
<jsp:include page="ParamObj.jsp" >
<br>
<jsp:param name="param1" value="val1" />
<br>
<jsp:param name="param2" value="val2" />
<br>
<jsp:param name="param3" value="val3" />
</jsp:include>
<br>
<b>Calling page:<b>
<br>
Name1: <%= request.getParameter("param1") %>

6 Ver 1.0 2005 Aptech Limited


<br>
Name2: <%= request.getParameter("param2") %>
<br>
Name3: <%= request.getParameter("param3") %>
<br>
</body>
</html>

Save the code as ParamId.jsp in the C:\Tomcat 5.5\Webapps\basic directory.

<html>
<body>
<h2>Called page: </h2>
<b>Names:</b>
<br>
Name1: <%= request.getParameter("param1") %>
<br>
Name2: <%= request.getParameter("param2") %>
<br>
Name3: <%= request.getParameter("param3") %>
<br>
</body>
</html>

Save the code as ParamObj.jsp in the C:\Tomcat 5.5\Webapps\basic directory. The


http://localhost:8080/basic/ParamObj.jsp?param1= Alice&param2=Bob&param3=Cathy path is
entered into the Web browser. This passes the three parameters Eric, Alice, and Bob to the
ParamId.jsp page.

Using Java Server Pages (JSP) Tags Ver 1.0 2005 Aptech Limited 7
The output of the program is as shown in Figure 4.5.

Figure 4.5: Output of ParamObj.jsp

8 Ver 1.0 2005 Aptech Limited

You might also like