1. 程式人生 > >JSP的內建物件2

JSP的內建物件2

一、實驗目的

    通過程式設計和上機實驗理解 JSP各個頁面之間的響應和傳遞的過程。並且能夠熟練的掌握JSP的內建物件的屬性和方法,並能靈活運用。

二、實驗要求

1.編寫頁面重定向程式。

2.編寫session屬性設定和獲取的程式。

3.編寫用out物件輸出表格程式。

三、實驗內容

(一)頁面重定向

  1.在建立的WEB工程中建立 Redirect.jsp 檔案,

Redirect.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Redirect.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
<body>
<%//頁面重定向程式片
String url;
url=request.getParameter("goaddress");
if(url!=null)
{
  response.sendRedirect(url);
}
%>
<form name="form1" action="Redirect.jsp" method="post" >
頁面重定向:
<select name="goaddress" onchange="javascript:form1.submit()">
  <option value="">========請選擇========</option>
  <option value="http://www.csai.cn">中國系統分析員</option>
  <option value="http://www.51cmm.com">軟體工程專家網</option>
  <option value="http://www.hnii.gov.cn">湖南省資訊辦</option>
  <option value="http://www.temco.com.cn">天工遠科資訊科技有限公司</option>
</select>
</form>
</body>
</html> 

(二)session屬性設定和獲取

  1. 在建立的WEB工程中建立 Login.jsp和LoginInfor.jsp 檔案。
    • 程式功能:實現session屬性在整個頁面快取資訊的儲存和銷燬。
    • 程式原始碼如下:

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script language="javascript">
 function on_submit()
 {//驗證資料的合法性
  if (form1.username.value == "")
			 {
			  alert("使用者名稱不能為空,請輸入使用者名稱!");
			  form1.username.focus();
			  return false;
			 }
    if (form1.userpassword.value == "")
			 {
			  alert("使用者密碼不能為空,請輸入密碼!");
			  form1.userpassword.focus();
			  return false;
			 }
 }
</script>
		<%!
		public String codeToString(String str)
		{//處理中文字串的函式
		  String s=str;
		  try
		    {
		    byte tempB[]=s.getBytes("UTF-8");
		    s=new String(tempB);
		    return s;
		}
		catch(Exception e)
		{
		  return s;
		}
		}
		%>
		<%
		String username=request.getParameter("username");
		String userpassword=request.getParameter("userpassword");
		if(username!=null&userpassword!=null)
		{//如果使用者名稱和密碼都合法,記下使用者名稱,一般把使用者和密碼存在資料庫中,
		 //用資料庫中的資訊與提交的使用者名稱和密碼比較以進行使用者合法性檢查,
		
		  session.setAttribute("username",codeToString(username));
		  response.sendRedirect("sessionUserLogin1.jsp");
		}
		%>
		<html>
		<head>
		<title>使用者登入</title>
		</head>
		<body>
		<table aligin="center">
		<form name="form1" method="post" action="LoginInfor.jsp" onsubmit="on_submit()">
		<tr aligin="center">
		<td>
		使用者登入
		</td>
		</tr>
		<tr aligin="center">
		<td>
		請輸入使用者名稱:<input type="text" name="username" size="20">
		</td>
		</tr>
		<tr aligin="center">
		<td>
		請輸入密碼:&nbsp;&nbsp;<input type="password" name="userpassword" size="20">
		</td>
		</tr>
		<tr aligin="center">
		<td>
		<input type="submit" value="提交" name="B1">
		<input type="reset" value="全部重寫" name="B2">
		</td>
		</tr>
</form>
</table>
</body>
</html>

LoginInfo.jsp

<body>
使用者登入成功!<br>
您的使用者名稱是:<%=(String)session.getAttribute("username")%>
</body>

(三)用out物件輸出表格程式

  1. 在建立的WEB工程中建立Outapp.jsp檔案

程式功能:通過使用out輸出靜態表格,從而掌握利用內建物件實現靜態網頁的方法。

Outapp.jsp程式原始碼如:

<BODY>
<%
out.print("<table border='1' width='100%'><tr><td width='100%' colspan='4'>");
out.print("參會人員名單(用out輸出表格)"+"</tr>");
out.print("<tr><td width='25%'>"+"姓名"+"</td>");
out.print("<td width='15%'>"+"年齡"+"</td>");
out.print("<td width='15%'>"+"性別"+"</td>");
out.print("<td width='45%'>"+"工作單位"+"</td></tr>");
out.print("<tr><td width='25%'>"+"鄧子云"+"</td>");
out.print("<td width='15%'>"+"26"+"</td>");
out.print("<td width='15%'>"+"男"+"</td>");
out.print("<td width='45%'>"+"長沙市商業銀行"+"</td></tr>");
out.print("<tr><td width='25%'>"+"張賜"+"</td>");
out.print("<td width='15%'>"+"27"+"</td>");
out.print("<td width='15%'>"+"男"+"</td>");
out.print("<td width='45%'>"+"長沙凌科遠信科技有限公司"+"</td></tr>");
out.print("<tr><td width='25%'>"+"袁樹雄"+"</td>");
out.print("<td width='15%'>"+"31"+"</td>");
out.print("<td width='15%'>"+"男"+"</td>");
out.print("<td width='45%'>"+"計算機學院"+"</td></tr>");
out.print("</table>");
%>
</BODY>