基于jsp+JDBC完成用户的登录验证

每一次相遇都是一定的缘分,先赞后看,养成习惯,期待你点赞与关注

下一篇:利用JSP+JDBC实现网站的留言功能

创建用户表,能完成用户登录验证和用户注册功能。
实现如下效果:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
主要代码如下:完整版可在github下载完整代码期待你的小星星
login.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
 
    <title>starting page</title>
	
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	<!--
	
	.STYLE2 {
		font-family: "Courier New", Courier, monospace;
		font-size: 16px;
		font-weight: bold;
		color: red;
	}
	-->
	</style>
</head>

<body>

<p align="center" class="STYLE2"> 用户登录页面</p>
<%
    if(request.getAttribute("error") != null){
    %>
       <center>
         <h3> <font color="red" ><%=request.getAttribute("error") %></font></h3>
       </center>
  <%  }
 %>
 <%String info=request.getParameter("info");
 if(info==null)info="";%>
 <font color=red><%=info %></font>
<form id="form1" name="form1" method="post" action="suess.jsp">
  <table width="337" height="124" border="1" align="center">
    <tr>
      <td width="77">用户名:</td>
      <td width="107"><label>
        <input name="username" type="text" id="id" size="20" maxlength="20" />
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;&nbsp;&nbsp;码:</td>
      <td><label>
        <input name="password" type="password" id="password" size="20" maxlength="20" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="submit" name="submit" id="submit" value="登录" />
      </label></td>
      <td><label>
        <input type="reset" name="reset" id="reset" value="重置" />
      </label></td>
    </tr>
  </table>
</form>
<a href=register.jsp>注册用户</a>
  </body>
</html>

login_conf.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312" import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
    		String username = request.getParameter("username") ;
    		String password = request.getParameter("userPassword") ;
    		
    		final String DBDRRIVER = "com.mysql.jdbc.Driver" ;
    		/*  ?useUnicode=true&characterEncoding=gb2312*/
    		final String DBURL = "jdbc:mysql://localhost:3306/test2";
    		final String DBUSERNAME = "root" ;
    		final String DBPASSWORD = "123" ;
    		Connection conn = null ;
    		PreparedStatement pstmt = null ;
    		String sql = null ;
    	
    		if(username!=null&&password!=null){
    			try{
    				Class.forName(DBDRRIVER) ;
    				conn = DriverManager.getConnection(DBURL,DBUSERNAME,DBPASSWORD) ;
    				sql = "insert into users (username,userpassword) values (?,?)" ;
    				pstmt = conn.prepareStatement(sql) ;
    				pstmt.setString(1,username) ;
    				pstmt.setString(2,password) ;
    				pstmt.executeUpdate() ;
    				pstmt.close() ;
    				conn.close() ;
    	%>
    				<h3>注册成功!!!</h3>
    				<h3>三秒钟后自动转到登录页面!!!</h3>
    				<h3>如果没有跳转,请点击<a href="login.jsp">这里</a>!!!</h3>
    	<%
    				response.setHeader("refresh","3;url=login.jsp") ;
    			}catch(Exception e){
    	%>
    				<h3>注册失败!!!</h3>
    				<h3><%=e%></h3>
    	<%    	
    			}
    		}
    	%>

</body>
</html>

register.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<!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=gb2312">
		<title>新用户注册</title>
		<script language="javascript">
		//判断输入的用户名和密码是否合法
		function check()
		{
			if(reg_form.username.value == "")
			{//判断用户名是否为空
				alert("用户名不能为空!");
				reg_form.userName.focus();
			}
			else if(reg_form.userPassword.value == "")
			{//判断密码是否为空
				alert("用户密码不能为空!");
				reg_form.userPassword.focus();
			}
			else if(reg_form.userPassword.value != reg_form.confirm_UserPassword.value)
			{//判断两次输入的密码是否一致
				alert("两次输入的密码不一致!");
				reg_form.userPassword.focus();				
			}
			else
			{
				//设置跳转目的页面
				reg_form.action="login_conf.jsp";
			}
		}
		</script>
	</head>
	<body>
		<p align="center"><font size="5">新用户注册</font></p>
		<div align="center">
		<form name="reg_form" method="post" onSubmit="check()">
		<table width="60%" border="0">
			<tr>
			<td width="50%" align="right">请输入用户名:</td>
			<td width="50%" align="left"><input type="text" name="username"></td>
			</tr>
			<tr>
			<td width="50%" align="right">请输入密码:</td>
			<td width="50%" align="left"><input type="password" name="userPassword"></td>
			</tr>
			<tr>
			<td width="50%" align="right">请输入确认密码:</td>
			<td width="50%" align="left"><input type="password" name="confirm_UserPassword"></td>
			</tr>
			<tr>
			<td width="50%" align="center" colspan="2">
			<br>
			<input type="submit" name="sub" value="注册">&nbsp;&nbsp;
			<input type="reset" name="res" value="重填">
			</td>			
			</tr>
		</table>
		</form>
		</div>
	</body>
</html>

sucess.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>登录成功</title>
</head>
<body>
<%
    		String username = request.getParameter("username") ;
    		String password = request.getParameter("password") ;
    		
    		final String DBDRRIVER = "com.mysql.jdbc.Driver" ;
    		/* ?useUnicode=true&characterEncoding=gb2312 */
    		final String DBURL = "jdbc:mysql://localhost:3306/test2" ;
    		final String DBUSERNAME = "root" ;
    		final String DBPASSWORD = "123" ;
    		Connection conn = null ;
    		PreparedStatement pstmt = null ;
    		String sql = null ;
    		out.print(username);
    		if(username!=null&&password!=null){
    			try{
    				Class.forName(DBDRRIVER) ;
    				conn = DriverManager.getConnection(DBURL,DBUSERNAME,DBPASSWORD) ;
    				sql = "select * from users where username=? and userpassword=?;" ;
    				pstmt = conn.prepareStatement(sql) ;
    				pstmt.setString(1,username) ;
    				pstmt.setString(2,password) ;
    				ResultSet aa=pstmt.executeQuery();
    				if(!aa.next())response.sendRedirect("login.jsp?please login again!");
    				else out.println("登录成功");
    				pstmt.close() ;
    				conn.close() ;
    	
    			}catch(Exception e){
    	%>
    				<h3>登录失败!!!</h3>
    				<h3><%=e%></h3>
    	<%    	
    			}
    		}
    	%>
</body>
</html>

效果截图:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

以上仅是主要代码部分,如需要完整项目,可在GitHub上面下载,完整项目代码地址

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页