實驗目標:
掌握JSP中指令標識炒辉、腳本標識豪墅、動作標識和注釋的使用。
實驗內容:
(1)編寫兩個JSP頁面黔寇,在頁面1中有一個表單偶器,用戶通過該表單輸入用戶的姓名并提交給頁面2;
在頁面2中輸出用戶的姓名和人數(shù)缝裤。如果頁面1沒有提交姓名或者姓名含有的字符個數(shù)大于10屏轰,就跳轉到頁面1
(2)編寫4個JSP頁面。頁面1憋飞、頁面2和頁面3都含有一個導航條霎苗,以便用戶方便地單擊超鏈接訪問這3個頁面;頁面4為錯誤處理頁面榛做。要求這3個頁面通過使用include動作標記動態(tài)加載導航條文件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>
表單輸入
第一個用戶訪問
第二個用戶訪問
使用JavaScript跳轉(會有提示框)
·實驗二:
//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>
屏幕快照 2018-10-17 下午3.20.04.png
屏幕快照 2018-10-17 下午3.20.11.png
屏幕快照 2018-10-17 下午3.20.16.png
屏幕快照 2018-10-17 下午3.19.57.png