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