制作一個銀行卡用戶登錄頁面,提交后連接數據庫進行登錄驗證,根據驗證結果跳轉到不同頁面


Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>


<form action="check.jsp" method="post"><!--action里面寫要轉的jsp頁面  -->
卡號:<input type="text" name="cardid"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="登陸"><br>


</form>

</body>
</html>

check.jsp   ojdbc6.jar 放lib里

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--這個頁面是
接收用戶輸入的卡號和密碼,進行驗證,驗證需要連數據庫  
request對象負責接收-->

<%
    //接收數據
    String cardid=request.getParameter("cardid");
    String password=request.getParameter("password");
    //前兩部接收數據,接受完了,就該驗證數據
    //一定要確定數據收到了
    //數據驗證     檢查用戶名 密碼
    if(cardid==null||password==null
    ||cardid.equals("")||password.equals(""))//得保證都有數據???
    {
        out.write("請正確登陸系統");
    }
    else
    {
        out.write(cardid);
        out.write(password);
    }
    
    
    
    
    
    //連接數據庫
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn=DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:orcl",
            "test0816","123456");
    PreparedStatement ps=conn.prepareStatement(
            "select * from t_bankcard where cardid=?" 
                    +"and password=? and state='1'");
    ps.setString(1,cardid);
    ps.setString(2,password);
    
    ResultSet rs=ps.executeQuery();
    
    if(rs.next())
    {
        out.write("用戶名和密碼正確");
    }
    else
    {
        out.write("用戶名或密碼錯誤");
    }
    //釋放資源
    rs.close();
    ps.close();
    conn.close();
%>

</body>
</html>

Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>


<form action="check2.jsp" method="post"><!--action里面寫要轉的jsp頁面  -->
卡號:<input type="text" name="cardid"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="登陸"><br>


</form>

</body>
</html>

CardDAO.java  建在java Resources src 包下面

package com.hanqi.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class CardDAO {
    
    private ComboPooledDataSource cpds=new ComboPooledDataSource("helloc3p0");
    //通過配置文件構建連接池對象
    public boolean checkLogin(String cardid,String password)
    {
        boolean rtn=false;
        
        try {
            Connection conn=cpds.getConnection();
            
            PreparedStatement ps=conn.prepareStatement(
                    "select * from t_bankcard where cardid=?" 
                            +"and password=? and state='1'");
            ps.setString(1,cardid);
            ps.setString(2,password);
            
            ResultSet rs=ps.executeQuery();
            
            rtn=rs.next();//
            
        } catch (SQLException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }    
        
        return rtn;
    }
    
}

 

check2.jsp  webContent

<%@page import="com.hanqi.web.CardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
String cardid=request.getParameter("cardid");
String password=request.getParameter("password");

if(cardid==null||password==null
||cardid.equals("")||password.equals(""))
{
    out.write("請正確登陸");
}
else
{
//檢查登陸信息
CardDAO cd=new CardDAO();

if(cd.checkLogin(cardid,password))
{
    out.write("登陸成功");
}
else
{
    out.write("登陸失敗");
}
    
    
    
}

%>
</body>
</html>

 

关注微信公众号

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com