1. 程式人生 > >Hello JSP!——內建物件基礎練習題

Hello JSP!——內建物件基礎練習題

一、一個簡單的使用者登入案例

LoginForm.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
	<head>
		<title>使用者登入</title>
	</head>
	<body>
		<center>
			<form action = "LoginConf.jsp" method="post">
				<table>
					<tr>
						<td colspan="2">使用者登入</td>
					</tr>
					<tr>
						<td>使用者名稱:</td>
						<td><input type="text" name="username"></td>
					</tr>
					<tr>
						<td>密  碼:</td>
						<td><input type="password" name="userpassword"></td>
					</tr>
					<tr>
						<td colspan="2">
						<input type="submit" value="登入">
						<input type="reset" value="重置">
						</td>
					</tr>	
				</table>
			</form>
		</center>
	</body>
</html>

LoginConf.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
	<head>
		<title>登入判斷</title>
	</head>
	<body>
		<center>
			<%
				String username = request.getParameter("username"); //接收使用者名稱引數
				
				String userpassword = request.getParameter("userpassword"); //接收使用者密碼引數
			 %>
			 <%
			 	//判斷使用者名稱及密碼,如果為指定使用者則跳轉到登入成功頁面
			 	if ("James".equals(username) && "1234".equals(userpassword)){
			  %>
			  <jsp:forward page = "LoginSuccess.jsp"/>
			  <% 
			  	}
			  	//如果不是指定使用者則跳轉到登入失敗頁面
			  	else {
			   %>
			   <jsp:forward page = "LoginFailure.jsp"/>
			   <%
			   		 }
			    %>
		</center>
	</body>
</html>

LoginSuccess.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
	<head>
		<title>登入成功</title>
	</head>
	<body>
		<center>
			<h1>登入成功</h1>
		</center>
	</body>
</html>

LoginFailure.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
	<head>
		<title>登入失敗</title>
	</head>
	<body>
		<center>
			<h1>登入失敗</h1>
		</center>
	</body>
</html>

執行結果:




二、填空。

1.與Servlet有關的內建物件,包括   page   config  兩個內建物件。

2.JSP中提供了4種屬性儲存範圍,分別為  page  request    session     application

3.通過request物件的getParameter(String name)方法,可以獲得引數名為name的引數值。

4.通過response物件的sendRedirect(URL)方法設定頁面重定向,從而實現頁面跳轉。

5.通過out物件的print(String str)方法和println(String str)方法進行頁面輸出。

6.通過session物件的setMaxInactiveInterval()方法設定session生命週期,通過getMaxInactiveInterval()方法獲得session生命週期。