一、实验目的
通过编程和上机实验理解 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属性设置和获取
- 在建立的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>
请输入密码: <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对象输出表格程序
- 在建立的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>