Thursday, April 16, 2009

Exercises 4 lab 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") %>
<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.

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

Figure 4.5: Output of ParamObj.jsp

0 nhận xét: