1. 程式人生 > >jsp使用者登入驗證

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;
	}
}