簡單的登錄實現

<span style="font-size:18px;">登錄頁面</span>
<span style="font-size:18px;"></span><pre name="code" class="java"><%@page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>河南</title></head>
<body>
<center>
<h1>登陸操作</h1>
<hr>
	<form action="login_check.jsp" method="post">
		<table border="1">
			<tr>
				<td colspan="2">
					用戶登陸
				</td>
			</tr>
			<tr>
				<td>登陸ID:</td>
				<td><input type="text" name="id"></td>
			</tr>
			<tr>
				<td>登陸密碼:</td>
				<td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="登陸">
					<input type="reset" value="重置">
				</td>
			</tr>
		</table>
	</form>
</center>
</body>
</html>

檢查頁面

<span style="font-size:18px;"></span><pre name="code" class="java"><%@page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head><title>河南</title></head>
<body>
<center>
<h1>登陸操作</h1>
<hr>
<%!	// 定義若干個數據庫的連接常量
	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
	public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
	public static final String DBUSER = "root" ;
	public static final String DBPASS = "admin" ;
%>
<%
	Connection conn = null ;		// 數據庫連接
	PreparedStatement pstmt = null ;	// 數據庫預處理操作
	ResultSet rs = null ;		// 查詢要處理結果集
	boolean flag = false ;	// 保存標記
	String name = null ;	// 保存真實姓名
%>
<%
try{
%>
<%
	Class.forName(DBDRIVER) ;
	conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
	String sql = "SELECT name FROM user WHERE userid=? AND password=?" ;
	pstmt = conn.prepareStatement(sql) ;
	pstmt.setString(1,request.getParameter("id")) ;
	pstmt.setString(2,request.getParameter("password")) ;
	rs = pstmt.executeQuery() ;	// 查詢
	if(rs.next()){	// 如果有數據,則可以執行
		flag = true ;	//  表示登陸成功
		name = rs.getString(1) ;
	}	
%>
<%
}catch(Exception e)	{
	e.printStackTrace() ;
}
finally{
	try{
		rs.close() ;
		pstmt.close() ;
		conn.close() ;
	} catch(Exception e){}
}
%>
<%
	if(flag){	// 登陸成功
%>
		<jsp:forward page="login_success.jsp">
			<jsp:param name="uname" value="<%=name%>"/>
		</jsp:forward>
<%
	} else {		// 登陸失敗
%>
		<jsp:forward page="login_failure.jsp"/>
<%
	}
%>
</center>
</body>
</html>
成功頁面

<span style="font-size:18px;"></span><pre name="code" class="java"><%@page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>河南</title></head>
<body>
<center>
<h1>登錄操作</h1>
<h2>登錄成功</h2>
<h2>歡迎<font color="red"><%=request.getParameter("uname")%></font>光臨</h2>
</center>
</body>
</html>



<span style="font-size:18px;">頁面截圖</span>
<span style="font-size:18px;"><img src="https://img-blog.csdn.net/20150601110647945?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhbnpodQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="https://img-blog.csdn.net/20150601110531761?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhbnpodQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span>
發佈了25 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章