JSP基础语法之八:session对象,简单登录模块(session+JDBC)



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

logOut.jsp 负责注销(登出)操作

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

welcome.jsp 负责输出成功登录或登录失败的信息



一: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;  




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

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
	<head>
		<title> session-login </title>
	</head>

	<body>  
		
<%
	if(session.isNew())//判断是否已分配session
	{
%>
		<h2>欢迎新用户访问!</h2>	
<%				
	}
	else
	{
%>
		<h2>您是老用户,请登录</h2>	
		<h4>(您已经在本站点停留了<%=(session.getLastAccessedTime()-session.getCreationTime())/1000 %>秒)</h4> <!--计算session创建时间-->
<%	
	}
%>
		
    <form action="checkLogIn.jsp" method="post">  
        <table border="1" width="50%">  
          <tr>  
            <td colspan="2">  
                用户登录  
            <td>  
          <tr>  
            
          <tr>  
            <td>用户名</td>  
            <td><input type="text" name="user"></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>  
      
  
    </body>  
</html>






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

<%@ page contentType="text/html" pageEncoding="GBK"%>  
<%@ page import="java.sql.*"%>  
  
  
<html>  
    <head>  
        <title> checkLogIn </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; //检查通过的用户名  
        
        String u = ""; //从登陆页面获得的“用户名”
        String p = ""; //从登陆页面获得的“密码”
    %>  
      
    <%  
        try {  
        //连接  
        Class.forName(DBDriver);//加载数据库驱动    
        con = DriverManager.getConnection(DBURL, DBUser, DBPassWord);//连接    
              
    %>  
            <!-- 测试数据库连接 <h2>Debug_已连接 <%=con%></h2>  -->
    <%     
          
        st = con.createStatement(); //实例化  
        u = request.getParameter("user"); //从上级页面获得的用户名  
        p = request.getParameter("password"); //从上级页面获得的密码  
        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)
		{
			session.setAttribute("uname",u);
%>
			<h2>登陆成功, 3秒内若没有跳转,<a href="welcome.jsp">请点击此处</a></h2>		
<%		
			response.setHeader("refresh","3;URL=welcome.jsp");	
		}
		else
		{
%>
			<h2>登录失败 <a href="logIn.jsp">点此处重新登录</a></h2>
<%	
		}		
%>
		
	
		
	</body>
	
</html>




四:welcome.jsp 显示登陆成功或失败

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
	<head>
		<title> welcome </title>
	</head>

	<body>
	
<%	
	String sa = (String)session.getAttribute("uname");
	if(null != sa && !sa.equals("") ) //null是防止NullPointer,如果session设置成功,就不是空值
	{
%>
	<h2> 登录成功 ,欢迎  <%=sa%>  使用 </h2>
	<br>
	<h2> <a href="logOut.jsp">注销按钮</a> </h2>
<%
	}
	else
	{
%>
			<h2>还未登录 <a href="logIn.jsp">点此处登录</a></h2>
<%	
	}
%>		
	</body>
	
</html>




五:logOut.jsp 注销(登出)

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
	<head>
		<title> logOut </title>
	</head>

	<body>
	
		
		<br><br>

<%
	session.invalidate();
	response.setHeader("refresh","3;URL=logIn.jsp");
%>
		
	<h2>已经注销成功, 3秒内若没有跳转,<a href="logIn.jsp">请点击此处</a></h2>

	
		
	</body>
	
</html>




























  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值