实验目标:
掌握JSP中指令标识、脚本标识、动作标识和注释的使用。
实验内容:
(1)编写两个JSP页面,在页面1中有一个表单,用户通过该表单输入用户的姓名并提交给页面2;
在页面2中输出用户的姓名和人数。如果页面1没有提交姓名或者姓名含有的字符个数大于10,就跳转到页面1
(2)编写4个JSP页面。页面1、页面2和页面3都含有一个导航条,以便用户方便地单击超链接访问这3个页面;页面4为错误处理页面。要求这3个页面通过使用include动作标记动态加载导航条文件head.txt
实验代码:
·实验一:
//jsp1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method=get action=jsp2.jsp>
你的名字是:
<input type=text name=username>
<input type=submit value=submit>
</form>
</body>
</html>
//jsp2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%! int count=0; %>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("username");
session.setAttribute("username",name);
if(name!=""&&name.length()<=10){
out.println("你好,");
out.println(name);
out.println("你是第");
out.println(++count);
out.println("个用户");
}else{
/* 使用JavaScript跳转(会有提示框)
<!-- <script type="text/javascript">
window.location="jsp1.jsp";
alert(window.location.href);
</script> --> */
/* 使用response对象跳转 */
response.sendRedirect("jsp1.jsp");
}%>
</body>
</html>
·实验二:
//head.txt
<table cellSpacing="1" cellPadding="1" width="60%" align="center" border="0" >
<tr valign="bottom">
<td><A href="jsp01.jsp"><font size=3>jsp01.jsp页面</font></A></td>
<td><A href="jsp02.jsp"><font size=3>jsp02.jsp页面</font></A></td>
<td><A href="jsp03.jsp"><font size=3>jsp03.jsp页面</font></A></td>
</tr>
</Font>
</table>
//jsp01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<jsp:include page="head.txt"/>
</head>
<body>
<br/>
<font size=5 color=red>
This is jsp01.jsp
</font>
</body>
</html>
//jsp02.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp02</title>
<jsp:include page="head.txt"/>
</head>
<body>
<br/>
<font size=5 color=red>
This is jsp02.jsp
</font>
</body>
</html>
//jsp03.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp03</title>
<jsp:include page="head.txt"/>
</head>
<body>
<br/>
<font size=5 color=red>
This is jsp03.jsp
</font>
</body>
</html>
//jsp04.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp04</title>
<jsp:include page="head.txt"/>
</head>
<body>
<br/>
<font size=5 color=red>
This is the error page
</font>
</body>
</html>