数据库中建表:
login.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>
#top{
width: 1024px;
height: 207px;
background: url("image/topbg.gif") no-repeat;
margin: auto;
}
#tablee{ width: 1024px;
height: 340px;
padding-top:15px;
margin: auto;
}
/*table设置外围边框,设置table固定不动*/
table{ width:600px;
table-layout:fixed;
margin:auto; }
.inputt{
width:95%;
}
#tdd{ text-align:center; }
.tr30{
height:30px;
}
.tr50{
height:50px;
}
#tdd2{
text-align:center;
font-size:13px;
color:gray;
}
.anniu{
border: 1px solid #cccbcb;
border-radius: 7px;
font-size: 1em;
font-weight: bold;
color: #fff;
background: #0dbfdd;
}
#bottom{
width: 1024px;
height: 55px;
background: url("image/end.png") no-repeat;
margin:auto;
text-align: center;
padding-top: 12px;
line-height:20px;
font-size: 15px;
font-family: 宋体;
}
#huixian{
text-align: center;
font-size: 13px;
color:red;
}
</style> </head>
<body >
<form action="Login" method="post" name="frmLogin" >
<div class="tanlee">
<div id="top"></div>
<div id="tablee" >
<table cellpadding="0px" cellspacing="0px " >
<tr class="tr30" ><td id="huixian" colspan="3">${error}
</td>
</tr>
<tr class="tr50">
<td style="text-align:center"> 用户:
</td>
<td ><input class="inputt" id="Uidid" type="text" name="uid" onfocus="ShowTips('remind1')" onblur="Checkid()"/>
</td>
<td> <%-- input 的值弹出提醒--%>
<span id="remind1" ></span>
</td>
</tr>
<tr class="tr50">
<td style="text-align:center;" > 密码:
</td>
<td><input class="inputt" id="passid" type="password" name="password" onfocus="ShowTips('remind2')" onblur="Checkpass()" />
</td>
<td> <%-- input 的值弹出提醒--%>
<span id="remind2" ></span>
</td>
<tr class="tr50">
<td id="tdd" colspan="3">
<input type="submit" class="anniu" value=" 登陆 " />
<input type="button" class="anniu" value=" 重置 " onclick="resetValue()"/>
</td>
</tr>
<tr class="tr30">
</tr>
<tr class="tr30"> <td id="tdd2" colspan="3">忘记密码?请联系管理员!</td>
</tr>
</table>
</div>
<div id="bottom">
©CopyRight ****-****.Net Inc All Rights Reserved. ********版权所有
</div>
</div>
</form>
</body>
</html>
User.java
public class User {
private String uid;
private String uname;
private String password;
private String ulimit; public String getuid() {
return uid;
} public void setuid(String uid){
uid=this.uid;
}
public String getuname(){
return uname;
}
public void setuname(String uname){
this.uname=uname;
}
public String getpassword(){
return password;
}
public void setpassword(String password){
this.password=password;
}
public String getlimit(){
return ulimit;
}
public void setulimit(String ulimit){
this.ulimit=ulimit;
}
}
}
Userdao.java
public class Userdao {
public User loginIN(String uid,String password){
User newuser=null;
DButils db=new DButils();
try {
String sql="select *from users where uid=? and upassword=?";
Object param[]={uid,password};
db.doPost(sql, param);
ResultSet rs=db.getResultSet();
if(rs.next()){
newuser=new User();
newuser.setuid(rs.getString("uid"));
newuser.setuname(rs.getString("uname"));
newuser.setpassword(rs.getString("upassword"));
newuser .setulimit(rs.getString("ulimit"));
rs.close();
}else System.out.print("登陆失败");
} catch (Exception e) {
new RuntimeException("登陆异常!");
} return newuser;
}
}
Loginservlet.java
public class Loginservlet extends HttpServlet {
private static final long serialVersionUID=1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("uid");
String password=request.getParameter("password"); Userdao userdao=new Userdao();
User user= userdao.loginIN(id, password);
if(user!=null){
HttpSession session=request.getSession();
session.setAttribute("currentUser", user);
response.sendRedirect("succ/scuu2.jsp");
}
else {
request.setAttribute("error", "您输入的凭证有误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
} }
}