jsp用户登录验证

项目结构


login.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>用户登录</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <form method="post" action="loginCheck.jsp">
    <center>
        	<h4>xxx学生管理系统</h4>
        	<table>
    		<tr>
    		<td>用户名:</td>
    		<td><input type="text" name="username"></td>
    		</tr>
    		<tr>
    		<td>密    码:</td>
    		<td><input type="password" name="password"></td>
    		</tr>
    		<tr>
    		<td></td>
    		<td><input type="submit" value="登录" onclick="check()"> <input type="reset" value="重置"></td>
    		</tr>
    	</table>
    </center>
    </form>
  </body>
</html>

界面效果


loginCheck.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>用户登录</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <form method="post" action="loginCheck.jsp">
    <center>
        	<h4>xxx学生管理系统</h4>
        	<table>
    		<tr>
    		<td>用户名:</td>
    		<td><input type="text" name="username"></td>
    		</tr>
    		<tr>
    		<td>密    码:</td>
    		<td><input type="password" name="password"></td>
    		</tr>
    		<tr>
    		<td></td>
    		<td><input type="submit" value="登录" onclick="check()"> <input type="reset" value="重置"></td>
    		</tr>
    	</table>
    </center>
    </form>
  </body>
</html>

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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>学生管理系统</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
		ul{
			list-style-type: none;
			background-color:#333;
			margin:0;
			padding:0;
		}
		li a{
			text-decoration: none;
			color:white;
		}
		li{
			display: inline;
			padding: 10px 16px;
		}
	</style>
  </head>
  <body>
  <%
  	String username=session.getAttribute("user").toString();
   %>
    <ul>
    	<li><a href="">学生管理</a></li>
    	<li><a href="">课程管理</a></li>
    	<li><a href="">成绩管理</a></li>
    </ul>
     <p><font color="red"><%=username%></font>欢迎您,使用学生管理系统!</p>
  </body>
</html>

界面效果


loginError.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录错误页面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    登录失败,用户名或密码输入有误。 <br>
    <a href="login.jsp">返回登录</a>
  </body>
</html>

DbHelper类

package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbHelper {
	private Connection conn=null;
	private Statement stmt=null;
	private ResultSet rest=null;
	private PreparedStatement prst=null;
	public Connection getConn(){
		try{
			String url="jdbc:mysql://localhost:3306/student";
			String username="root";
			String password="root";
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url,username,password);
			if(conn!=null){
				System.out.println("数据库连接成功。");
			}else{
				System.out.println("数据库连接失败。");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	public ResultSet selectUser(String name,String pwd){
		try{
			conn=getConn();
			String sql="select * from user where username=? and password=?";
			prst=conn.prepareStatement(sql);
			prst.setString(1, name);
			prst.setString(2, pwd);
			rest=prst.executeQuery();
		}catch(Exception e){
			e.printStackTrace();
		}
		return rest;
	}
}

猜你喜欢

转载自blog.csdn.net/dwenxue/article/details/79884242