一、 实验 目的 1 、 掌握表单的使用方法,以及服务器端处理表单请求的方法; 2 、 理解 Forward 的作用; 3 、 熟练运用 JSP 的典型内置 对象 二、 实验 内容及步骤 1 、表单的使用及其递交、处理过程 在原创教务系统中,系统会根据用户类型 ( 教师 / 学生
一、实验目的
1、 掌握表单的使用方法,以及服务器端处理表单请求的方法;
2、 理解Forward的作用;
3、 熟练运用JSP的典型内置对象
二、实验内容及步骤
1、表单的使用及其递交、处理过程
在原创教务系统中,系统会根据用户类型(教师/学生)显示不同的页面。为模拟该功能,需要定义设计如下页面:
-Login.html:包括用户类型名(默认为学生类型)、用户名、用户密码
源代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form name="Login" action="Forward.jsp" method="post">
<table>
<tr>
<td>用户类型</td>
<td>
<select name ="User">
<option value="Student">学生</option>
<option value="Teacher">老师</option>
</select>
</td>
</tr>
<tr>
<td>用户名</td>
<td>
<input name="Username" type="text" size="20">
</td>
</tr>
<tr>
<td>用户密码</td>
<td>
<input name="Password" type="password" size="20">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
运行结果:

-Forward.jsp: 根据用户类型,进行任务转发。如果是教师类型,把任务转发给Teacher.jsp;如果是学生类型,把任务转发给Student.jsp
源代码:
<%! String direction="";
%>
<%
//这里完成的是实验中的第二个功能
request.setCharacterEncoding("gbk");
String kindOfUser = request.getParameter("User");
String name = request.getParameter("Username");
String password = request.getParameter("Password");
if(name==null||name.equals("")||password==null||password.equals("")){
direction = "LoginFails.jsp";
}
else{
if(kindOfUser.equals("Student")){
direction = "Student.jsp";
}
else{
direction = "Teacher.jsp";
}
}
%>
<jsp:forward page="<%=direction%>"></jsp:forward>
-Teacher.jsp: 如果用户名和密码都等于TEACHER,同时输出“登录成功”,否则页面返回到Login.html
源代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String name = request.getParameter("Username");
String password = request.getParameter("Password");
if(name.equals("Teacher")&&password.equals("Teacher")){
out.println("登录成功");
}
else{
response.sendRedirect("Login.html");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Teacher</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>
</body>
</html>
运行结果:

-Student.jsp: 输出:您输入的用户名是***。
源代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String name = request.getParameter("Username");
String password = request.getParameter("Password");
out.println("您输入的用户名是"+name);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Student</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>
</body>
</html>

*2、附加:表单填写错误判断
尝试改进以上内容,在Forward.jsp中增加判断:若用户名或密码为null或为空,则返回到Login.html重新登录,并在登录页面的表单上方以红色字体显示:“用户名或密码不能为空!”。
我的做法是写了一个LoginFails.jsp文件,通过以下关键字段:
if(name==null||name.equals("")||password==null||password.equals("")){
direction = "LoginFails.jsp";
}
和<jsp:forward page="<%=direction%>"></jsp:forward>
实现要需要的功能
源代码:
一.Forward.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%! String direction="";
%>
<%
//这里完成的是实验中的第二个功能
request.setCharacterEncoding("gbk");
String kindOfUser = request.getParameter("User");
String name = request.getParameter("Username");
String password = request.getParameter("Password");
if(name==null||name.equals("")||password==null||password.equals("")){
direction = "LoginFails.jsp";
}
else{
if(kindOfUser.equals("Student")){
direction = "Student.jsp";
}
else{
direction = "Teacher.jsp";
}
}
%>
<jsp:forward page="<%=direction%>"></jsp:forward>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Forward.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>
</body>
</html>
二.LoginFails
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>LoginFails</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 >
<p><font color="#FF0000">用户名或密码不能为空!</font></p>
</body>
</html>
<jsp:include page="Login.html"></jsp:include>
运行结果:
*3、附加:用户注册功能
尝试完成用户注册功能,用户信息包括用户登陆名、密码、真实姓名、性别、出生年月、籍贯、Email、联系电话、联系地址、QQ号,要求对注册的信息进行有效性判断,并显示在页面。
源代码:
Register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%!
//判断是否是合格的Email格式
boolean isEmail(String Email){
boolean flag = true;
int locationDot = Email.indexOf(".");
int locationAt = Email.indexOf("@");
//.和 @中至少一个不存在
if(locationDot<0||locationAt<0){
flag = false;
}
//@前面没有任何字符
elseif(locationAt==0){
flag = false;
}
//.在@前面,或者@和.之间没有任何字符
elseif(locationDot-1<=locationAt){
flag = false;
}
//.后面没有字符了
elseif(locationDot==Email.length()-1){
flag = false;
}
return flag;
}
String tell = "";
%>
<%
request.setCharacterEncoding("gbk");
String StrRegisterok = request.getParameter("registerok");//得到一个隐藏参数,判读进入到这个界面是否是由按提交所得
if(StrRegisterok!=null&&StrRegisterok.equals("register")){
//得到各个各个参数
String StrLoginName = request.getParameter("loginName");
String StrPassword = request.getParameter("password");
String StrpasswordConfirm = request.getParameter("passwordConfirm");
String Strusername = request.getParameter("username");
String StrSex = request.getParameter("sex");
String StrAge = request.getParameter("age");
String StrHome = request.getParameter("home");
String StrEmail = request.getParameter("Email");
String StrpHone = request.getParameter("phone");
String StrLocation = request.getParameter("location");
String StrQQNumber = request.getParameter("QQNumber");
//判断用户所填的参数是否合格
//用户名为空
if(StrLoginName==null||StrLoginName.equals("")){
tell = "用户名不能为空!";
}
elseif(StrPassword==null||StrPassword.equals("")||StrpasswordConfirm==null||StrpasswordConfirm.equals("")){
tell = "密码和确认密码不能为空";
}
elseif(!StrPassword.equals(StrpasswordConfirm)){
tell = "前后输入的两次密码不同,请重新输入";
}
elseif(!isEmail(StrEmail)){
tell = "邮箱格式有问题";
}
else{
tell = "恭喜注册成功";
}
}
%>
<html>
<head>
<title>注册</title>
</head>
<body>
<form name ="register" action="Register.jsp" method="post">
<input name="registerok" type="hidden" value="register">
<table>
<tr>
<td>用户登陆名</td>
<td>
<input name="loginName" type="text" size=20 >
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input name=password type="password" size=20 >
</td>
</tr>
<tr>
<td>确认密码</td>
<td>
<input name="passwordConfirm" type="password" size=20 >
</td>
</tr>
<tr>
<td>真实姓名</td>
<td>
<input name="username" type="text" size=20 >
</td>
</tr>
<tr>
<td>性别</td>
<td>
<input name="sex" type="radio" value="man" checked="checked">男
<input name="sex" type="radio" value="woman">女
</td>
</tr>
<tr>
<td>出生年月</td>
<td>
<input name="age" type="text" size=20>
</td>
</tr>
<tr>
<td>籍贯</td>
<td>
<input name="home" type="text" size=20>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input name="Email" type="text" size=20>
</td>
</tr>
<tr>
<td>联系电话</td>
<td>
<input name="phone" type="text" size=20>
</td>
</tr>
<tr>
<td>联系地址</td>
<td>
<input name="location" type="text" size=20>
</td>
</tr>
<tr>
<td>QQ</td>
<td>
<input name="QQNumber" type="text" size=20>
</td>
</tr>
<tr>
<td>
<input name="submit" type="submit" value="提交">
</td>
</tr>
</table>
</form>
<p><font color="#FF0000"><%=tell %></font></p>
</body>
</html>
运行结果:

三.实验收获及碰到的问题等。
JavaScript一般只能在客户端起作用,如果在客户端中JavaScript的源代码被删除,再运行,就不起作用了。所以为了安全起见,我采用了jsp直接处理的形式注册信息的方法