java -------簡單的基於jsp+servlet+jdbc登錄


  1  2 項目結構以及數據庫表圖片
  3 
  4 
  5 package com.jmi.booklibmanage.service;
  6 
  7 import java.sql.Connection;
  8 import java.sql.DriverManager;
  9 import java.sql.ResultSet;
 10 import java.sql.SQLException;
 11 import java.sql.Statement;
 12 
 13 
 14 
 15 public class StudentDao {
 16     Statement stm;
 17     ResultSet rs;
 18     Connection conn;
 19    public boolean login(String stdno, String passwd){
 20        try {
 21         //1加載驅動程序   com.mysql.jdbc.Driver此為MySQL驅動
 22         Class.forName("com.mysql.jdbc.Driver");
 23         
 24         /** 獲取數據庫連接
 25           DriverManager.getConnection(url, user, password)
 26                                    需要傳入三個參數:數據URL,登錄數據的用戶名密碼
 27          MySQL 數據庫的URL寫法:  jdbc:mysql://hostname:port/databasename
 28               本機數據庫hostname 為localhost或者127.0.0.1   
 29                         port 一般為3306
 30                     本項目中的數據庫名稱(databasename)為 booklib
 31         **/
 32          conn=DriverManager.getConnection("jdbc:mysql:" +
 33                 "//localhost:3306/booklib","root","12345");
 34         String sql="select * from students where stdno='"+stdno+"' and loginpasswd='"+passwd+"'";
 35     
 36       // 2 通過Connection 對象創建基本的Statement 對象
 37          stm=conn.createStatement();
 38      
 39         //3  使用Statement執行sql 語句,執行后返回代表查詢結果的ResultSet對象
 40       //  Restult 實質是一個查詢結果集
 41           rs=stm.executeQuery(sql);
 42         
 43         // 4 Result 中的next方法 作用:rs.next()返回一個布爾值
 44         //  數據表中有記錄則返回true ,沒有記錄返回false
 45         if(rs.next()){
 46             
 47             //gerXxx()方法獲取記錄指針指向行,列特定列的值
 48            // 方法一 :使用列索引作為參數
 49             System.out.print(rs.getString(1)+"  ");
 50             System.out.print(rs.getString(2)+"  ");
 51             System.out.println(rs.getString(3)+"  ");
 52          // 5 方法二 : 使用列名作為參數
 53             System.out.print(rs.getString("stdname")+"  ");
 54             System.out.print(rs.getString("stdno")+"  ");
 55             System.out.println(rs.getString("loginpasswd")+"  ");
 56          // 以上是通過Result對象來出去查詢結果的兩種方法    
 57             
 58             return true;
 59         }
 60     } catch (ClassNotFoundException e) {
 61         // TODO Auto-generated catch block
 62         e.printStackTrace();
 63     } catch (SQLException e) {
 64         // TODO Auto-generated catch block
 65         e.printStackTrace();
 66     }
 67     
 68     // 6 回收數據資源,包括關閉ResultSet,Statement和Connection資源等
 69       try {
 70         rs.close();
 71     } catch (SQLException e) {
 72         // TODO Auto-generated catch block
 73         e.printStackTrace();
 74     }  
 75       try {
 76         stm.close();
 77     } catch (SQLException e) {
 78         // TODO Auto-generated catch block
 79         e.printStackTrace();
 80     }  
 81       try {
 82         conn.close();
 83     } catch (SQLException e) {
 84         // TODO Auto-generated catch block
 85         e.printStackTrace();
 86     }
 87     return false;
 88       
 89    }
 90    
 91    // 運行測試下此代碼
 92    public static void main(String[] args) {
 93 // 實例化對象,調用其login方法,傳入兩個參數
94 // "select * from students where stdno='"+stdno+"' and loginpasswd='"+passwd+"';" 95 96 //如果在數據庫中有有則返回結果true; 97 StudentDao stu = new StudentDao (); 98 boolean istrue = stu.login("15140101","123"); 99 System.out.println(istrue); 100 101 } 102 } 103 104// 結果
105 106 107 108 Login.html 頁面內容 109 注意以下method為post ,action為std.do 110 111 112 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 113 <html> 114 <head> 115 <title>login.html</title> 116 117 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 118 <meta http-equiv="description" content="this is my page"> 119 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 120 121 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 122 123 </head> 124 125 <body> 126 <form action="std.do" method="post"> 127 學號:<input name="stdno" type="text"><br> 128 密碼:<input name="passwd" type="text"><br> 129 <input type="submit" value="登錄"> 130 </form> 131 </body> 132 </html> 133 134 135 136 以下為web.xml內容 137 <?xml version="1.0" encoding="UTF-8"?> 138 <web-app version="2.5" 139 xmlns="http://java.sun.com/xml/ns/javaee" 140 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 141 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 142 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 143 <servlet> 144 <description>This is the description of my J2EE component</description> 145 <display-name>This is the display name of my J2EE component</display-name> 146 <servlet-name>StdService</servlet-name> 147 <servlet-class>com.jmi.booklibmanage.servlet.StdService</servlet-class> 148 </servlet> 149 150 <servlet-mapping> 151 <servlet-name>StdService</servlet-name> 152 <url-pattern>/std.do</url-pattern> 153 </servlet-mapping> 154 <welcome-file-list> 155 <welcome-file>login.html</welcome-file> 156 </welcome-file-list> 157 </web-app> 158 159 160 在login頁面中的action后面所跟內容比 <url-pattern>/</url-pattern>中間內容多出”/161
162 
163 所以   <url-pattern>/std.do</url-pattern>
164 注意:在一定要在前面加/
165 
166 
167  截圖中的紅色部分內容一致
170 才能根據action中后面內容找到com.jmi.booklibmanage.servlet.StdService這個類(此類為全名即com.jmi.booklibmanage.servlet包下StdService類)
171 
172 
173 新建一個StdService必須繼承HttpServlet類
174 保留一個doPost方法,刪除其它用不到的方法:
175 
176 package com.jmi.booklibmanage.servlet;
177 
178 import java.io.IOException;
179 import java.io.PrintWriter;
180 
181 import javax.servlet.ServletException;
182 import javax.servlet.http.HttpServlet;
183 import javax.servlet.http.HttpServletRequest;
184 import javax.servlet.http.HttpServletResponse;
185 
186 import com.jmi.booklibmanage.service.StudentDao;
187 
188 public class StdService extends HttpServlet {
189 
190     /**
191      * The doPost method of the servlet. <br>
192      *
193      * This method is called when a form has its tag value method equals to post.
194      * 
195      * @param request the request send by the client to the server
196      * @param response the response send by the server to the client
197      * @throws ServletException if an error occurred
198      * @throws IOException if an error occurred
199      */
200     public void doPost(HttpServletRequest request, HttpServletResponse response)
201             throws ServletException, IOException {
202 
203         response.setContentType("text/html");
204         PrintWriter out = response.getWriter();
205         String stdno = request.getParameter("stdno");
206         // 獲得login.html頁面中輸入的學號  
207          //后面 request.getParameter("stdno")中的引號中的內容
208         //   必須與login頁面中:<input name="stdno" type="text">中的name的引號里面內容一致
209         //才能獲得在login頁面中輸入的內容
210         String passwd = request.getParameter("passwd");
211         // 獲得login.html頁面中輸入的密碼
212         
213         //以下是在Console打印從login.html頁面輸入的內容
214         System.out.println(stdno);
215         System.out.println(passwd);
216         
217         StudentDao u = new StudentDao();
218         boolean user= u.login(stdno,passwd);
219         // 實例化對象 ,調用login方法,傳入參數
220         // 如果返回true則重定向到歡迎頁面,如果false重定向到login.html頁面
221         if(user){
222             response.sendRedirect("welocome.html");
223         }else{
224             response.sendRedirect("login.html");    
225             }
226         out.flush();
227         out.close();
228         
229     }
230 }

驗證整個項目


 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM