现在的位置: 首页 > 综合 > 正文

JSP+JDBC实现简单用户登录模块【Deprecated】

2013年04月25日 ⁄ 综合 ⁄ 共 2962字 ⁄ 字号 评论关闭

这个例子中没有用session,仅是简单的JDBC测试程序

使用session的版本更好:详见http://blog.csdn.net/ruantao1989/article/details/8045879

 login_index.jsp 负责输入表格,然后提交到连接数据库检查的页面

 login_check.jsp 负责连接JDBC, 检查login_index.jsp提交来的值,是否在数据库中

 login_success.jsp 和 login_fail.html 负责输出成功登录或登录失败的信息

一:SQL脚本:

DROP TABLE userlogin;
CREATE TABLE userlogin
(
 	userid VARCHAR2(30),
	name VARCHAR2(30) NOT NULL,
	password VARCHAR2(32) NOT NULL,
	CONSTRAINT userlogin_userid_pk PRIMARY KEY(userid)
);
INSERT INTO userlogin(userid,name,password) VALUES('admin','adminstrator','admin'); 
commit;

最后一句commit极其重要,我花了半个小时才查到没提交实务操作这个问题
其他代码全都正确,只有这儿没提交的话很查到

二: login_index.jsp:接收用户输入信息

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
	<head>
		<title> 登录欢迎页 </title>
	</head>

	<body>
		<form action="login_check.jsp" method="post">
			<table border="1" width="50%">
				<tr>
					<td colspan="2">
						用户登录
					<td>
				<tr>
				
				<tr>
					<td>用户名</td>
					<td><input type="text" name="userid"></td>
				</tr>
				
				<tr>
					<td>密码</td>
					<td><input type="password" name="userpassword"></td>
				</tr>
				
				<tr>
				<td colspan="2">
					<input type="submit" value="提交">
					<input type="reset" value="重置">
				<td>
				<tr>
					
			</table>
		</form>
	
	
	</body>
	
</html>

三: login_check.jsp:连接数据库后,检查输入的登录信息是否正确

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>


<html>
	<head>
		<title> JSP_JDBC_Demo </title>
	</head>

	<body>
		
	<%!
		private static final String DBDriver = "oracle.jdbc.driver.OracleDriver";//驱动  
		private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORCL";//URL命名规则:jdbc:oracle:thin:@IP地址:端口号:数据库实例名
		private static final String DBUser = "scott";
		private static final String DBPassWord = "890307";
	%>	
	
	<%	
		Connection con = null; //数据库连接状态
		Statement st = null;	//数据库容器
		ResultSet res = null; //查询结果
		
		boolean flag = false ; //通过数据库匹配标识
		String name_checked = null; //检查通过的用户名
	%>
	
	<%
		try {
		//连接
		Class.forName(DBDriver);//加载数据库驱动  
		con = DriverManager.getConnection(DBURL, DBUser, DBPassWord);//连接  
			
	%>
			<h2>Debug_已连接 <%=con%></h2>
	<%	
		
		st = con.createStatement();	//实例化
		String u = request.getParameter("userid"); //从上级页面获得的用户名
		String p = request.getParameter("userpassword"); //从上级页面获得的密码
		String sql = "SELECT name FROM userlogin WHERE userid IN ('"+ u + "')" + " AND password IN ('"+p+"')";//组装SQL语句
		System.out.println(sql);//执行上述设置的sql查询语句
		res = st.executeQuery(sql);
		if(true == res.next())//只有查询得到结果才能进入循环
		{
			flag = true; 
			name_checked = res.getString(1); //获得查询出的name				
	%>		
				<h3>Debug_数据库查询结果 <%=name_checked%> </h3>
	<%
		}
	} catch (Exception e) {
		System.out.println(e);
	}finally{
		//关闭连接		
		try{
			res.close();//依次关闭
			st.close();
			con.close();
		}catch(Exception e)
		{
		}
	}
	%>
	
	<!-- 根据连接状态跳转 -->
	<%
		if(true == flag)//登录成功
		{
	%>
			<jsp:forward page="login_success.jsp" >
				<jsp:param name="user_name" value="<%=name_checked%>" />
			</jsp:forward>
	<%
		}
		else//登录失败
		{
	%>
			<h2>Debug_<%=request.getParameter("userid")%></h2>
			<h2>Debug_<%=request.getParameter("userpassword")%></h2>
			<jsp:forward page="login_fail.html" /> //静态显示就足够了
	<%	
		}
	%>
	
	
	

	
	
	</body>
	
</html>

四:login_success.jsp 和 login_fail.html 显示登陆成功或失败

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
	<head>
		<title> 登录成功 </title>
	</head>

	<body>
		
		<h2> 登录成功 </h2>
		<h2> 欢迎 <%=request.getParameter("user_name")%> 登录 </h2>
	
	</body>
	
</html>

<html>
	<head>
		<title> 登录失败 </title>
	</head>

	<body>
		
		<h2> 登录失败 </h2>
		<h2> 点此处<a href="login_index.jsp">重新登录</a> </h2>
	</body>
	
</html>

抱歉!评论已关闭.