页面重定向,session属性设置和获取及用out对象输出表格(jsp的内置对象part2)

1.页面重定向

在建立的WEB工程中建立 Redirect.jsp 文件,
• 程序功能:实现页面的重定向。
Redirect.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<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> 

运行结果:
在这里插入图片描述
在这里插入图片描述

2.session属性设置和获取

在建立的WEB工程中建立 Login.jsp和LoginInfor.jsp 文件。

• 程序功能:实现session属性在整个页面缓存信息的保存和销毁。
Login.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<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("ISO-8859-1");
    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("LoginInfo.jsp");
}
%>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<table aligin="center">
<form name="form1" method="post" action=" Login.jsp" onsubmit="return 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

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>用户登录成功</title>
</head>
<body>
用户登录成功!<br>
您的用户名是:<%=(String)session.getAttribute("username")%>
</table>
</body>
</html>

运行Login.jsp
在这里插入图片描述
提交结果
在这里插入图片描述
注意因为本例使用session,所以action=" ";作用的对象为当前页Login,可以理解为session使当前页面保存填写的信息,在需要的页面使用session方法进行调用。更加详细准确的session和request信息,大家可自行查找资料,后续也许会更新相关知识。

3.用out对象输出表格

在建立的WEB工程中建立Outapp.jsp文件
程序功能:通过使用out输出静态表格,从而掌握利用内置对象实现静态网页的方法。
Outapp.jsp

<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<head>
<title>out应用示例</title>
</head>
<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>
</HTML>

运行结果:
在这里插入图片描述
学习中,欢迎交流,指导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值