课后作业:用户进入企业邮箱,要求登录成功后需要在空间首页显示“欢迎你xxx”的字样;登录失败后,返回到登录界面,重新登录。对邮箱格式要进行表单校验(使用正则表达式完成)。
创建工程:
添加图片
index.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>
<HTML>
<HEAD>
<base href="<%= basePath %>">
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>登录验证</TITLE>
<STYLE type="text/css">
td {
font-size: 12px
}
</STYLE>
<SCRIPT type="text/javascript">
function checkLogin() {
var userName = document.myform.userid.value;
var userPass = document.myform.passwd.value;
var vegName = /^[a-zA-Z0-9]+$/;
if (!vegName.test(userName)) {
alert("用户名错误,请重新输入");
document.myform.userid.focus();
return false;
}
var regPwd = /^\w{6,}$/;
if (!regPwd.test(userPass)) {
alert("密码输入错误,请重新输入");
document.myform.passwd.select();
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" METHOD="POST" action="" onSubmit="return checkLogin()">
<TABLE align="center">
<TR>
<TD>
<IMG src="images/Snap1.jpg">
<DIV style="position:absolute; left: 490px; top: 216px; width: 356px;">
<TABLE width="100%" height="100%" border=0 cellPadding=0 cellSpacing=0>
<TBODY>
<TR vAlign=top>
<TD width="23%"
height=27 align=right class=bld>用户名:
</TD>
<TD height="27" align=left><INPUT TYPE="text" SIZE="20" NAME="userid"><BR>用户名由字母和数字组成</TD>
</TR>
<TR vAlign=top>
<TD class=bld align=right height=27>密码: </TD>
<TD height="27" align=left><INPUT TYPE="password" SIZE="20" NAME="passwd"><BR>密码由字母、数字和下划线组成,至少6位
</TD>
</TR>
<TR vAlign=top>
<TD class=bld align=right height=40> </TD>
<TD align=left valign="middle"><A href="#"><INPUT name="submit" type="image"
src="images/login.gif" width="101"
height="22" border="0"></A></TD>
</TR>
</TABLE>
</DIV>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
运行结果:
当用户名密码输入不规范时:
当密码输入不规范时:
登录处理页面do_login.jsp:
创建登录成功页面:
运行程序:
档登陆成功时:
当登陆失败时: