Java Web实现简单注册、登录程序(链接数据库)

1.工程文件
Java Web实现简单注册、登录程序(链接数据库)_第1张图片
至于如何配值数据库和连接数据库,网上有很多教程,我就不多说,关键就是连接数据库用的JDBC.jar一定要有并且把它放在图中的位置(sqljdbc4.jar)。其他诸如CSS和JS可以自由发挥,不用局限于我的工程文件。工作软件我用的是eclipse和SQL2008R2(官网可下)

2.Chack.jsp

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Check Page</title>
</head>
<body>
<% 
  String username="";
  String password="";
  username=request.getParameter("username");
  username=new String(username.getBytes("iso-8859-1"),"UTF-8");//编码转换,保证识别中文登录名
  password=request.getParameter("password");
  //out.println(username);
  String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=login";
  String userName="sa";//数据库用户名
  String userPwd="123456";//数据库密码
  String sql="select Username from users where Username='"+username+"'";
  String sql1="select Password from users where Password='"+password+"'";
  //out.println(sql);
  Class.forName(driverName);
  Connection stmt=DriverManager.getConnection(dbURL,userName,userPwd);
  
  Statement stmt1=stmt.createStatement();
  Statement stmt2=stmt.createStatement();
  ResultSet rs=stmt1.executeQuery(sql);
  ResultSet rs1=stmt2.executeQuery(sql1);
  if(rs.next()&&rs1.next()){
     
	  request.getRequestDispatcher("suc.jsp").forward(request, response);
  }
  else{
     
	  request.getRequestDispatcher("fail.jsp").forward(request, response);
  }
%>
</body>
</html>

3.fail.jsp

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<%
String username="";
String password="";
username=request.getParameter("username");
password=request.getParameter("password");
//out.println(username);
//out.println(username);
  String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=login";
  String userName="sa";
  String userPwd="123456";
  String sql="select Username from users where Username='"+username+"'";
  String sql1="select Password from users where Password='"+password+"'";
  //out.println(sql);
  Class.forName(driverName);
  Connection stmt=DriverManager.getConnection(dbURL,userName,userPwd);
  
  Statement stmt1=stmt.createStatement();
  Statement stmt2=stmt.createStatement();
  ResultSet rs=stmt1.executeQuery(sql);
  ResultSet rs1=stmt2.executeQuery(sql1);
  if(rs.next()){
     
	  out.println("密码错误,请重新登录!");
	  %>
	  <br><a href="Login.jsp">返回登陆</a>
	  <%
  }
  else{
     
	  out.println("用户名不存在,请注册!");
	  %>
	  <br><a href="zhuce.jsp">前往注册!</a>
	  <br><a href="Login.jsp">返回登陆!</a>
  <%
  }
  %>
</body>
</html>

4.Login.jsp

<%@ page language="java" import ="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>登录界面</title>
<link rel="stylesheet"  href="CSS/DesignPage.css"  type="text/css"/>
</head>
<body>

<div class="center" align="center">
	<div class="background">
	<div class="sysname-div">
	  <img id="logoimg" class="logoimg" src="images/logo.jpg">  
	     <label for="logoimg"><font color="white">用户登录 </font></label>
	</div>
	<div class="lgmain-div">
    <div class="lgtop-div">会员,请登录</div>
	<div class="lg-div">
    <div id="loginMessage" class="loginMessage"></div>
    
    <form action="Chack.jsp" method="post">
    <div class="login-div">
	  <input id="username" class="username" name="username" placeholder="用户名" value="" type="text" />
	</div>
    <div class="login-div">
	  <input id="password" name="password" class="password" placeholder="密码" value="" type="password" />
	</div>
    <div class="checkbox-div">
	<input type="checkbox" id="isRemember" class="isRemember" name="isRemember"/>
	<label for="isRemember">记住密码</label>	
	</div>
	<div class="btn-div">
	<button id="loginBtn" type="submit" >&nbsp;&nbsp;&nbsp;</button>		
    </div>
    </form>
    
    </div>
    </div>
    </div>
</div>
</body>
</html>

5.suc.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<%
  String username=request.getParameter("username");
  username=new String(username.getBytes("iso-8859-1"),"UTF-8");//防止中文乱码
%>
欢迎您登录!<font color="red"><% out.println(username); %></font>
</body>
</html>

6.zhuce.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<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=UTF-8">

<link rel="stylesheet" type="text/css" href="CSS/Register.css">
<link rel="stylesheet" type="text/css" href="CSS/Register-selector.css">
<script type="text/javascript" src="js/Register-js.js" charset="UTF-8"></script>
<title>注册界面</title>
</head>
<body>
<div class="background">
</div>
<div class="sysname-div">
<img id="logoimg" class="logoimg" src="images/logo.jpg">  
<label for="logoimg"><font color="black">用户注册 </font></label>
</div>
<div  class="center">
<div class="lgmain-div">
<div class="lgtop-div">创建新账户</div>
<div class="lg-div">
<form action="zhuceChack.jsp" method = "post">
<table border="0">
    	<tr><td class="td1"></td><td class = "td2"></td>
    	<tr><td>用户 名</td><td><input class = "input1" name ="username1" type = "text"  value = ""/>
    	<span id = "username1Span" class = "span1"></span></td></tr>
    	<tr><td>密  码</td><td><input class = "input1" name = "password1" type = "password" value = ""/>
    	<span id = "passwordSpan" class = "span1"></span></td></tr>
    	<tr><td>确认密码</td><td><input class = "input1" name = "sureName" type = "password" value = ""/>
    	<span id = "surePswSpan" class = "span1"></span></td></tr>
    	<tr><td>电子邮件</td><td><input class = "input1" name = "mail" type = "text" value = ""/>
    	<span id = "mailSpan" class = "span1"></span></td></tr>
    	<tr><td>电话号码</td><td><input class = "input1" name = "telephone" type = "text" value = ""/>
    	<span id = "telephoneSpan" class = "span1"></span></td></tr>
    	<tr><td>地  址</td><td><input class = "input1" name = "adrress" type = "text" value = ""/>
    	<span id = "adrressSpan" class = "span1"></span></td></tr>
    	<tr><td><input type = "submit"   value = "注   册"/></td>
    	<td><input type = "reset" value = "重新填写"/></td></tr>
</table>
</form>

</div>
</div>
<font color="red">注:用户名和密码不能为空</font><br>
未实现检查用户名重名和注册时密码是否为空的功能
</div>

</body>
</html>

7.zhuceChack.jsp

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username=request.getParameter("username1");
username=new String(username.getBytes("iso-8859-1"),"UTF-8");//编码转换,保证识别中文登录名
String password=request.getParameter("password1");
String mail=request.getParameter("mail");
String tel=request.getParameter("telephone");
String adrress=request.getParameter("adrress");
adrress=new String(adrress.getBytes("iso-8859-1"),"UTF-8");//编码转换,保证识别中文登录名
//out.println(username);
  String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=login";
  String userName="sa";
  String userPwd="123456";
  String sql="insert into users(Username,Password,Mail,Telephone,Adrress) values('"+username+"','"+password+"','"+mail+"','"+tel+"','"+adrress+"')";
  //out.println(sql);
  Class.forName(driverName);
  Connection stmt=DriverManager.getConnection(dbURL,userName,userPwd);
  Statement stmt1=stmt.createStatement();
  stmt1.executeUpdate(sql);
  request.getRequestDispatcher("zhucesuc.jsp").forward(request, response);
%>
</body>
</html>

8.zhucesuc.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功</title>
<link rel="stylesheet" type="text/css" href="CSS/Register.css">
</head>
<body>
<%
  String username=request.getParameter("username1");
  username=new String(username.getBytes("iso-8859-1"),"UTF-8");//编码转换,保证识别中文登录名
  String password=request.getParameter("password1");
  String mail=request.getParameter("mail");
  String tel=request.getParameter("telephone");
  String adrress=request.getParameter("adrress");
  adrress=new String(adrress.getBytes("iso-8859-1"),"UTF-8");//编码转换,保证识别中文登录名
%>
<div class="background">
<div  class="center">
<div class="lgmain-div">
<div class="lgtop-div">注册成功!</div><br><br>
<table border="0">
<tr><td>用户名:<font color="black"><% out.println(username); %></font></td></tr>
<tr><td>密码:<font color="black"><% out.println(password); %></font></td></tr>
<tr><td>电子邮件:<font color="black"><% out.println(mail); %></font></td></tr>
<tr><td>电话:<font color="black"><% out.println(tel); %></font></td></tr>
<tr><td>地址:<font color="black"><% out.println(adrress); %></font></td></tr>
</table>
<br><a href="Login.jsp">前往登陆</a>
</div>
</div>
</div>
</body>
</html>

9.数据库样式
Java Web实现简单注册、登录程序(链接数据库)_第2张图片
Java Web实现简单注册、登录程序(链接数据库)_第3张图片

效果:

登录

Java Web实现简单注册、登录程序(链接数据库)_第4张图片

Java Web实现简单注册、登录程序(链接数据库)_第5张图片

登录成功
在这里插入图片描述

登录失败
Java Web实现简单注册、登录程序(链接数据库)_第6张图片
在这里插入图片描述
Java Web实现简单注册、登录程序(链接数据库)_第7张图片

注册界面
Java Web实现简单注册、登录程序(链接数据库)_第8张图片
注册成功
Java Web实现简单注册、登录程序(链接数据库)_第9张图片
Java Web实现简单注册、登录程序(链接数据库)_第10张图片
Java Web实现简单注册、登录程序(链接数据库)_第11张图片
Java Web实现简单注册、登录程序(链接数据库)_第12张图片

你可能感兴趣的:(Java,Web学习)