您的位置:首页 > 编程语言 > Java开发

MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能

2016-12-08 10:30 1311 查看
 代码:

 1.前端部分

 top.jsp(网页头部,内含弹出式登陆窗口,注册窗口)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<style>
#notClickDiv{
filter:alpha(Opacity=35);opacity:0.35;
background:#000000;
position:absolute;
display:none;
z-index:9;
top:0px;
left:0px;
margin: 0px;
padding: 0px;
}

</style>
<script language="javascript">

function myClose(divID){
divID.style.display='none';			//设置id为login的层隐藏
document.getElementById("notClickDiv").style.display='none';	//设置id为notClickDiv的层隐藏
}
function Myopen(divID){ 								//根据传递的参数确定显示的层
var notClickDiv=document.getElementById("notClickDiv");	//获取id为notClickDiv的层
notClickDiv.style.display='block';						//设置层显示
document.getElementById("notClickDiv").style.width=document.body.clientWidth;
document.getElementById("notClickDiv").style.height=document.body.clientHeight;
document.getElementById(divID).style.display='block';							//设置由divID所指定的层显示
document.getElementById(divID).style.left=(document.body.clientWidth-240)/2;		//设置由divID所指定的层的左边距
document.getElementById(divID).style.top=(document.body.clientHeight-139)/2;		//设置由divID所指定的层的顶边框
}
function checkUsername(str){
if(str==""){			//当用户名为空时
alert("请输入用户名!");
}
}

function checkPassword(str){
if(str==""){		//当密码为空时
alert("请输入密码!");
}
}
</script>
<style>
#top{					/*设置页面头部的DIV样式*/
background-image:url(images/bg_top.jpg);
width:800px;
height:176px;
}
#navigation{			/*设置导航条的样式*/
background-image:url(images/navigation_bg.jpg);
width:790px;
height:26px;
padding:5px 5px 0px 5px;
margin: 0px;
}
#loginUl{				/*设置登录所用的<ul>标记的样式*/
list-style:none;
margin: 0px;
}
#loginUl li{			/*设置登录所用的<li>标记的样式*/
padding: 5px;
}
#loginTitle{			/*设置登录窗口的标题样式*/
padding:15px;
background-color:#FCFBF0;
color:#1B7F5D;
font-size: 14px;
font-weight:bold;
margin:0px;
}
#login{
position:absolute;
width:280px;
padding:4px;
height:156px;
display:none;
z-index:10;
background-color:#546B51;
}
</style>
<link rel="stylesheet" href="CSS/style.css" />
<div id="notClickDiv"></div>
<div id="top"></div>
<div id="navigation">
<%@ include file="register.jsp" %>
<div style="float:left;color:#006700;">
<c:if test="${!empty sessionScope.userName}">
<b>   》  欢迎 ${sessionScope.userName} 登录个人博客网站!</b>
</c:if>
<c:if test="${empty sessionScope.userName}">
<b>   》  欢迎光临个人博客网站!</b>
</c:if>
</div>
<div style="float:right;text-align: right;">
<a href="Main.jsp">首页</a>
<c:if test="${empty sessionScope.userName}">
  |  <a href="#" onClick="Myopen('login')">登录</a>
  |  <a href="#" onClick="Regopen('register')">注册</a>
  |  <a href="#">找回密码</a>
</c:if>
<c:if test="${!empty sessionScope.userName}">
 |  <a href="#">我的博客</a>
  |  <a href="#">写个人博客</a>
  |  <a href="#">退出登录</a>
</c:if>
</div>

</div>
<div id="login">
<form name="form2" method="post" action="login" id="form2">
<div id="loginTitle">个人博客网站--用户登录</b></div>
<div id="loginContent" style="background-color:#FFFEF9; margin:0px;">
<ul id="loginUl">
<li>
用户名:<input type="text" name="username" style="width:120px"onBlur="checkUsername(this.value)">
</li>
<li>
密  码:<input type="password" name="pwd"  style="width:120px"  onBlur="checkPassword(this.value)"> <a href="#">找回密码</a>
</li><li style="padding-left:40px;">
<input name="Submit" type="submit"  value="登录">
 
<input name="Submit2" type="button" value="关闭" onClick="myClose(login)">
</li></ul>
</div>
<div style="background-color:#FEFEFC;height:10px;"></div>
</form>
</div>


content.jsp(博客发布的内容栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
<style type="text/css">
body{
background:#FFF;
}
.content{
margin:0px auto;
width:800px;
height:726px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
.list-lio{
float:left;
border:1px solid #069;
width: 780px;
height: 100px;
margin-left: 10px;
margin-top:10px;
}
.fonto{
background:#CCC;
}
.info{
margin:10px 2px 3px 5px;
font-family: "宋体",Tahoma, Arial, Helvetica, sans-serif;
font-size: 18px;
}
.date-time{
background:#F00;
text-align:center;
border:1px solid #666;
width:105px;
height:20px;
float:right;
margin:45px 5px 5px 70px;
color:#FFF;
}
.list-lit{
float:left;
border:1px solid #069;
width: 780px;
height: 100px;
margin-left: 10px;
margin-top:10px;
}
.fontt{
background:#CCC;
margin-left:10px;
}
.info{
margin:10px 2px 3px 5px;
font-family: "宋体",Tahoma, Arial, Helvetica, sans-serif;
font-size: 18px;
}
</style>
</head>
<body>
<div class="content">
<table class="list-lio">
<tr>
<td class="imageone">
<img src="images/fengjingone.jpg" width="100" height="100" alt="专注">
</td>
<td class="fonto">
<p class="info">
想要做成一件事,除了勤奋,还需要专注与热爱,三者缺一不可。这样才有可能会成功!
<span class="date-time">
2016-08-07
</span>
</p>
</td>
</tr>
</table>

<table class="list-lit">
<tr>
<td class="imagetwo">
<img src="images/fengjingtwo.jpg" width="100" height="100" alt="专注">
</td>
<td class="fontt">
<p class="info">
你愿与我一路同行,我将为你背负行囊!---加油 2015~
<span class="date-time">
2015-02-19
</span>
</p>
</td>
</tr>
</table>

<table class="list-lit">
<tr>
<td class="imagetwo">
<img src="images/fengjingthree.jpg" width="100" height="100" alt="专注">
</td>
<td class="fontt">
<p class="info">
发现一路走来,不经意间已经慢慢的失去了一些东西。
<span class="date-time">
2014-10-25
</span>
</p>
</td>
</tr>
</table>

<table class="list-lit">
<tr>
<td class="imagetwo">
<img src="images/four.jpg" width="100" height="100" alt="专注">
</td>
<td class="fontt">
<p class="info">
在这世界上,不管你做得在怎么好,总会有那么50%的人,不会认同你的做法。只有无视他们的存在,才会走得更远。
<span class="date-time">
2014-07-18
</span>
</p>
</td>
</tr>
</table>

<table class="list-lit">
<tr>
<td class="imagetwo">
<img src="images/five.jpg" width="100" height="100" alt="专注">
</td>
<td class="fontt">
<p class="info">
有时候缺的就是破釜沉舟的勇气,把自己逼一逼,才知道自己有多大潜力!
<span class="date-time">
2014-05-27
</span>
</p>
</td>
</tr>
</table>

<table class="list-lit">
<tr>
<td class="imagetwo">
<img src="images/six.jpg" width="100" height="100" alt="专注">
</td>
<td class="fontt">
<p class="info">
是男人就得对自己狠点,为事业而奋斗。30而立,给自己十年时间。成功与否十年后见!!!加油
<span class="date-time">
2014-05-09
</span>
</p>
</td>
</tr>
</table>
</div>
</body>
</html>


bottom.jsp(网页底部)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<style>
#bottom{
background-image:url(images/bg_bottom.jpg);
width:800px;
height:58px;
clear: both;
text-align:center;
padding-top:10px;
}
#bottom ul{
color:#FFFFFF;
list-style:none;
line-height:20px;
}
</style>
<div id="bottom">
<ul>
<li>烟台大学   计算机与控制工程学院 海哥 制作 电话:1786281023x  电子邮箱邮箱:1150979147@qq.com</li>
<li>Copyright © http://blog.csdn.net/linhaiyun_ytdx All Rights Reserved! </li>
</ul>
</div>


Main.jsp(首页)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>博客首页</title>
<style type="text/css">
body{
background:#FFF;
}
.content{
margin:0px auto;
width:800px;
height:927px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="content">
<%@ include file="top.jsp" %>
<%@ include file="content.jsp" %>
<%@ include file="bottom.jsp" %>
</div>
</body>
</html>


LoSuss.jsp(登陆成功页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<style>
#notClickDiv{
filter:alpha(Opacity=35);opacity:0.35;
background:#000000;
position:absolute;
display:none;
z-index:9;
top:0px;
left:0px;
margin: 0px auto;
padding: 0px;
}
#suss{
margin:0px auto;
width:800px;
height:927px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
</style>
<script language="javascript">

function myClose(divID){
divID.style.display='none';			//设置id为login的层隐藏
//设置id为notClickDiv的层隐藏
document.getElementById("notClickDiv").style.display='none';
}
function Myopen(divID){ 								//根据传递的参数确定显示的层
var notClickDiv=document.getElementById("notClickDiv");	//获取id为notClickDiv的层
notClickDiv.style.display='block';						//设置层显示
document.getElementById("notClickDiv").style.width=document.body.clientWidth;
document.getElementById("notClickDiv").style.height=document.body.clientHeight;
document.getElementById(divID).style.display='block';							//设置由divID所指定的层显示
document.getElementById(divID).style.left=(document.body.clientWidth-240)/2;		//设置由divID所指定的层的左边距
document.getElementById(divID).style.top=(document.body.clientHeight-139)/2;		//设置由divID所指定的层的顶边框
}

</script>
<style>
#top{					/*设置页面头部的DIV样式*/
background-image:url(images/bg_top.jpg);
width:800px;
height:176px;
}
#navigation{			/*设置导航条的样式*/
background-image:url(images/navigation_bg.jpg);
width:790px;
height:26px;
padding:5px 5px 0px 5px;
margin: 0px;
}
</style>
<link rel="stylesheet" href="CSS/style.css" />
<div id="suss">
<div id="notClickDiv"></div>
<div id="top"></div>
<div id="navigation">
<div style="float:left;color:#006700;">
<b>   》  欢迎<b style="color: red;"><%=request.getParameter("username") %></b>登录个人博客网站!</b>
</div>
<div style="float:right;text-align: right;">
<a href="Main.jsp">首页</a>
 |  <a href="#">我的博客</a>
  |  <a href="#">写个人博客</a>
  |  <a href="#">退出登录</a>
</div>

</div>
<%@ include file="content.jsp" %>
<%@ include file="bottom.jsp" %>
</div>


LoSuss.jsp(登陆失败页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆失败</title>
<style type="text/css">
body{
background:#FFF;
}
.content{
margin:0px auto;
width:800px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
.info{
margin:0px auto;
width:800px;
height:350px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
font-size: 16px;
color: red;
}
</style>
</head>
<body>
<div class="content">
<%@ include file="top.jsp" %>
<div class="info">
<p>登陆失败,请仔细检查登陆信息!</p>
</div>
<%@ include file="bottom.jsp" %>
</body>
</html>


Register.jsp(注册页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<script language="javascript" src="JS/wghFunction.js"></script>
<script language="javascript">
//显示用户注册页面
function Regopen(divID){
var notClickDiv=document.getElementById("notClickDiv");	//获取id为notClickDiv的层
notClickDiv.style.display='block';						//设置层显示
document.getElementById("notClickDiv").style.width=document.body.clientWidth;
document.getElementById("notClickDiv").style.height=document.body.clientHeight;
divID=document.getElementById(divID);	//根据传递的参数获取操作的对象
divID.style.display='block';			//显示用户注册页面
divID.style.left=(document.body.clientWidth-663)/2;		//设置页面的左边距
divID.style.top=(document.body.clientHeight-441)/2;		//设置页面的顶边距
}
//隐藏用户注册页面
function Myclose(divID){
document.getElementById(divID).style.display='none';	//隐藏用户注册页面
//设置id为notClickDiv的层隐藏
document.getElementById("notClickDiv").style.display='none';
}
</script>
<script language="javascript">
/*************************************************************************************************************/
//验证用户名
function checkUser(str){
if(str==""){			//当用户名为空时
document.getElementById("div_user").innerHTML="请输入用户名!";//设置提示文字
document.getElementById("tr_user").style.display='block';	//显示提示信息
}
else{
document.getElementById("div_user").innerHTML="";	//清空提示文字
document.getElementById("tr_user").style.display='none';	//隐藏提示信息显示行
}
}
/*************************************************************************************************************/
//验证密码
function checkPwd(str){
if(str==""){		//当密码为空时
document.getElementById("div_pwd").innerHTML="请输入密码!";	//设置提示文字
document.getElementById("tr_pwd").style.display='block';		//显示提示信息
}else if(!checkePwd(str)){		//当密码不合法时
document.getElementById("div_pwd").innerHTML="您输入的密码不合法!";	//设置提示文字
document.getElementById("tr_pwd").style.display='block';	//显示提示信息
}else{		//当密码合法时
document.getElementById("div_pwd").innerHTML="";	//清空提示文字
document.getElementById("tr_pwd").style.display='none';		//隐藏提示信息显示行
}
}
//验证确认密码是否正确
function checkRepwd(str){
if(str==""){		//当确认密码为空时
document.getElementById("div_pwd").innerHTML="请确认密码!";	//设置提示文字
document.getElementById("tr_pwd").style.display='block';	//显示提示信息
}else if(form1.pwd.value!=str){		//当确认密码与输入的密码不一致时
document.getElementById("div_pwd").innerHTML="两次输入的密码不一致!";	//设置提示文字
document.getElementById("tr_pwd").style.display='block';	//显示提示信息
}else{	//当两次输入的密码一致时
document.getElementById("div_pwd").innerHTML="";	//清空提示文字
document.getElementById("tr_pwd").style.display='none';		//隐藏提示信息显示行
}
}
//验证E-mail地址
function checkEmail(str){
if(str==""){//当E-mail地址为空时
document.getElementById("div_email").innerHTML="请输入E-mail地址!";//设置提示信息
document.getElementById("tr_email").style.display='block';		//显示提示信息
}else if(!checkemail(str)){//当E-mail地址不合法时
document.getElementById("div_email").innerHTML="您输入的E-mail地址不正确!";//设置提示信息
document.getElementById("tr_email").style.display='block';		//显示提示信息
}else{
document.getElementById("div_email").innerHTML="";//清空提示信息
document.getElementById("tr_email").style.display='none';//不显示提示信息
}
}
//验证提示问题答案
function checkQuestion(str_q,str_a){
if(str_q!="" && str_a==""){//当密码提示问题不为空,而提示问题答案为空时
document.getElementById("div_answer").innerHTML="请输入提示问题答案!";//设置提示信息
document.getElementById("tr_answer").style.display='block';		//显示提示信息
}else if(str_q=="" && str_a !=""){//当密码提示问题为空,而提示问题答案不为空时
document.getElementById("div_question").innerHTML="请输入密码提示问题!";//设置提示信息
document.getElementById("tr_question").style.display='block';	//显示提示信息
}else{
document.getElementById("div_answer").innerHTML="";//清空提示信息
document.getElementById("div_question").innerHTML="";//清空提示信息
document.getElementById("tr_answer").style.display='none';	//不显示提示信息
document.getElementById("tr_question").style.display='none';//不显示提示信息
}
}
</script>
<script language="javascript">
//重置表单函数
function form_reset(form){
form.reset();		//重置表单
document.getElementById("tr_user").style.display='none';
document.getElementById("tr_pwd").style.display='none';
document.getElementById("tr_email").style.display='none';
document.getElementById("tr_question").style.display='none';
document.getElementById("tr_answer").style.display='none';
}
</script>

<div id="register" style="width:663; height:421; background-color:#546B51; padding:4px; position:absolute; z-index:11;display:none;">
<form name="form1" action="Register" method="post">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FEFEFC">
<tr>
<td height="408" align="center" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="50" style="color:#1B7F5D;font-size: 14px;"><b>个人博客网站--用户注册</b></td>
</tr>
</table>
<table width="94%" height="331" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="310" valign="top" bgcolor="#FFFFFF"><table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFEF9">
<tr id="tr_user" style="display:none">
<td height="40" colspan="2" align="center"><div id="div_user" style="border:#FF6600 1px solid; color:#FF0000; width:90%; height:29px; padding-top:8px;"></div></td>
</tr>
<tr>
<td width="93" height="40" align="right">用户名:</td>
<td height="40" align="left"><input name="user" type="text" onBlur="checkUser(this.value)">
 * 用户名的首字母为大写,长度大于3位小于15位</td>
</tr>
<tr id="tr_pwd" style="display:none">
<td height="40" colspan="2" align="center"><div id="div_pwd" style="border:#FF6600 1px solid; color:#FF0000; width:90%; height:29px; padding-top:8px; background-image:url(images/div_bg.jpg)"></div></td>
</tr>
<tr>
<td height="40" align="right">密码:</td>
<td height="40" align="left"><input name="pwd" type="password" onBlur="checkPwd(this.value)">
 * 密码由字母开头的字母、数字或下划线组成,并且密码的长度大于8位小于30位</td>
</tr>
<tr>
<td height="40" align="right">确认密码:</td>
<td height="40" align="left"><input name="repwd" type="password" onBlur="checkRepwd(this.value)">
 * 请确认密码 </td>
</tr>
<tr id="tr_email" style="display:none">
<td height="40" colspan="2" align="center"><div id="div_email" style="border:#FF6600 1px solid; color:#FF0000; width:90%; height:29px; padding-top:8px; background-image:url(images/div_bg.jpg)"></div></td>
</tr>
<tr>
<td height="40" align="right">E-mail地址:</td>
<td height="40" align="left"><input name="email" type="text" size="35" onBlur="checkEmail(this.value)">
 * 请输入有效的E-mail地址,在找回密码时应用 </td>
</tr>
<tr>
<td height="40" align="right">所在地:</td>
<td height="40" align="left"><input name="city" type="text" size="35">
 * 请输入您的所在地</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">以下两个选项,只要有任何一个没有输入,将不可以通过答案问题重新设置密码。</td>
</tr>
<tr id="tr_question" style="display:none">
<td height="40" colspan="2" align="center"><div id="div_question" style="border:#FF6600 1px solid; color:#FF0000; width:90%; height:29px; padding-top:8px; background-image:url(images/div_bg.jpg)"></div></td>
</tr>
<tr>
<td height="40" align="right">密码提示问题:</td>
<td height="40" align="left"><input name="question" type="text" id="question" size="35" onBlur="checkQuestion(this.value,this.form.answer.value)">
如:我的工作单位</td>
</tr>
<tr id="tr_answer" style="display:none">
<td height="40" colspan="2" align="center"><div id="div_answer" style="border:#FF6600 1px solid; color:#FF0000; width:90%; height:29px; padding-top:8px; background-image:url(images/div_bg.jpg)"></div></td>
</tr>
<tr>
<td height="40" align="right">提示问题答案:</td>
<td height="40" align="left"><input name="answer" type="text" id="answer" size="35" onBlur="checkQuestion(this.form.question.value,this.value)">
如:烟台大学                 </td>
</tr>
<tr>
<td height="40"> </td>
<td height="40" align="center">
<input name="btn_sumbit" type="submit" class="btn_grey" value="提交">
 
<input name="btn_reset" type="button" class="btn_grey" value="重置" onClick="form_reset(this.form)">
 
<input name="btn_close" type="button" class="btn_grey" value="关闭" onClick="Myclose('register')"></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="10" align="center" valign="top" bgcolor="#FEFEFC"> </td>
</tr>
</table>
</form>
</div>


ReSuss.jsp(注册成功页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.bean.User,com.dao.CustomerDao,com.dao.CustomerDaoer"%>
<%
String username = request.getParameter("user");
CustomerDao dao=new CustomerDaoer();
User u=new User();
u.setUsername(username);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册成功页面</title>
<style type="text/css">
body{
background:#FFF;
}
.content{
margin:0px auto;
width:800px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
.info{
margin:0px auto;
width:800px;
height:350px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
font-size: 18px;
}
</style>
</head>
<body>
<div class="content">
<%@ include file="top.jsp" %>
<div class="info">
<div>恭喜<strong style="color: red;"><%=u.getUsername() %></strong>注册成功!</div>
<div><a href="Main.jsp" style="font-size: 18px;">-->返回主页登陆</a></div>
</div>
<%@ include file="bottom.jsp" %>
</div>
</body>
</html>


Refail.jsp(注册失败页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册失败</title>
<style type="text/css">
body{
background:#FFF;
}
.content{
margin:0px auto;
width:800px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
.info{
margin:0px auto;
width:800px;
height:350px;
text-align:left;
background:#FFF;
border: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="content">
<%@ include file="top.jsp" %>
<div class="info">
<p style="font-size: 18px; color: red;">注册失败,请仔细检查注册信息是否完整!</p>
</div>
<%@ include file="bottom.jsp" %>
</body>
</html>


Style.css

body{
margin:0px;
padding:0px;
font-size: 9pt;
}
#box{
margin:0 auto auto auto;
width:800px;
clear:both;
background-color: #FFFFFF;
}

a:hover {
font-size: 9pt;
color: #FF4400;
}
a {
font-size: 9pt;
text-decoration: none;
color: #3C404D;
}


wghFunction.js

//判定用户名
function checkeUser(user){
var str=user;
//在JavaScript中,正则表达式只能使用首字母开头
var Expression=/^[A-Z][a-z]{3,15}$/;
var objExp=new RegExp(Expression);		//创建正则表达式对象
if(objExp.test(str)==true){				//通过正则表达式验证
return true;
}else{
return false;
}
}
//判断密码
function checkePwd(user){
var str=user;
var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){8,30}$/;
var objExp=new RegExp(Expression);		//创建正则表达式对象
if(objExp.test(str)==true){				//通过正则表达式验证
return true;
}else{
return false;
}
}

//验证E-mail地址
function checkemail(email){
var str=email;
var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
var objExp=new RegExp(Expression);		//创建正则表达式对象
if(objExp.test(str)==true){				//通过正则表达式进行验证
return true;
}else{
return false;
}
}


2.后端部分

User.java

package com.bean;
public class User {
private int id = 0;// 用户ID
private String username = "";// 用户名
private String pwd = "";// 密码
private String email = "";// E-mail地址
private String question = "";// 密码提示问题
private String answer = "";// 密码提示问题答案
private String city = "";// 所在地

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public String getAnswer() {
return answer;
}

public void setAnswer(String answer) {
this.answer = answer;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

}


CustomDao.java(接口)

package com.dao;
import com.bean.*;
import com.connection.*;
public interface CustomerDao {
public void add(User u);//添加用户
}


CustomDaoer.java

package com.dao;

import java.sql.SQLException;

import com.bean.User;
import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class CustomerDaoer implements CustomerDao {

@Override
public void add(User u) {
// TODO Auto-generated method stub
Connection conn=DButil.open();
String sql="insert into user(username,pwd,email,city,question,answer) values(?,?,?,?,?,?)";
try {
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1, u.getUsername());
pst.setString(2, u.getPwd());
pst.setString(3, u.getEmail());
pst.setString(4, u.getCity());
pst.setString(5, u.getQuestion());
pst.setString(6, u.getAnswer());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DButil.close(conn);
}
}

}


ConnectLogin.java

package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class ConnectLogin {
public int login(String username,String pwd)
{
int ID=0;
Connection conn=DButil.open();
PreparedStatement pstmt=null;
ResultSet rs=null;

String sql="select id from user where username=? and pwd=?";
try {
pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,pwd);
rs=pstmt.executeQuery();
if(rs.next())
{
ID=rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return ID;
}
}


Register.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.User;
import com.dao.CustomerDao;
import com.dao.CustomerDaoer;
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
public Register() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String username = request.getParameter("user"); // 获取用户名
String pwd = request.getParameter("pwd"); // 获取密码
String email = request.getParameter("email"); // 获取E-mail地址
String city = request.getParameter("city"); // 获取地址
String question = request.getParameter("question"); // 获取密码提示问题
String answer = request.getParameter("answer"); // 获取密码提示问题答案
PrintWriter out = response.getWriter();
if(username!=""&&pwd!=""&&email!=""&&city!=""&&question!=""&&answer!="")
{
CustomerDao dao=new CustomerDaoer();
User u=new User();
u.setUsername(username);
u.setPwd(pwd);
u.setEmail(email);
u.setCity(city);
u.setQuestion(question);
u.setAnswer(answer);
dao.add(u);
RequestDispatcher view=request.getRequestDispatcher("ReSuss.jsp");
view.forward(request, response);
}
else{
RequestDispatcher view=request.getRequestDispatcher("ReFail.jsp");
view.forward(request, response);
}
}

}


login.java

package com.servlet;

import com.dao.*;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
public login() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");

String username=request.getParameter("username");
String password=request.getParameter("pwd");
PrintWriter out=response.getWriter();
ConnectLogin dao=new ConnectLogin();
int ID=dao.login(username, password);
if(ID!=0)
{
RequestDispatcher view=request.getRequestDispatcher("LoSuss.jsp");
view.forward(request, response);
}
else
{
RequestDispatcher view=request.getRequestDispatcher("LoFail.jsp");
view.forward(request, response);
}
}

}


测试截图:





















内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐