您好,登錄后才能下訂單哦!
本篇內容主要講解“java+mysql怎么實現登錄和注冊功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java+mysql怎么實現登錄和注冊功能”吧!
1、首先是賬號密碼輸入框和按鈕:
登錄:
<form action="login.do" method="get"> <table> <tr><td>賬號:</td><td><input type="text" name="id" /></td></tr> <tr><td>密碼:</td><td><input type="password" name="password" /></td></tr> <tr><td> <a href="register.jsp" rel="external nofollow" >還沒注冊?點擊注冊</a></td><td><input id="login_in" type="submit" value=" 登錄 "></td></tr> </table> </form>
注冊:
<form name="form1" action="register.do" method="post"> <table width="200" border="0"> <tr> <td colspan="2"> <div id="status">注冊新用戶</div> </td> <tr> <td>*用戶名</td> <td><input type="text" name="username" size="10" id="username"></td> </tr> <tr> <td>*密碼</td> <td><input type="password" name="password1" size="10"></td> </tr> <tr> <td>*確認密碼</td> <td><input type="password" name="password2" size="10"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" size="10"></td> </tr> <tr> <td colspan="2"><a href="login.jsp" rel="external nofollow" >返回登錄頁</a> <input type="submit" name="submit" value="提交注冊"></td> </tr> </table> </form>
兩個form表的屬性里都有action,可理解為是用來標記登錄和注冊的,在web.xml中和對應的servlet綁定
2、數據庫建表,插入數據
在服務器中打開MySQL,建表
create table login( uname char(15) primary key, password char(15) not null, email char(20) )
3、sql語句內嵌到java語句中,查找(登錄)和插入(注冊)用戶信息,以下類名即為文件的名字
登錄:
import java.sql.*; public class userlogin { String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/login"; String usr = "root"; String pwd = "***";//自己的密碼 public boolean isuserlogin(String id,String password){ boolean isValid = false; String sql="select * from ulogin where uname='"+id+"' and password='"+password+"'"; try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url,usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(rs.next()){ isValid = true; } rs.close(); stm.close(); conn.close(); }catch (Exception e) { e.printStackTrace(); System.out.println(e); } if(isValid){//判斷用戶名以及密碼是否與設定相符 return true; } else return false; } }
注冊:
import java.sql.*; public class register { String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/login"; String usr = "root"; String pwd = "***"; public boolean userregister(String id,String password,String email){ boolean b = false; String sql = "select * from ulogin where uname='"+id+"'"; try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url,usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(!rs.next()){ sql = "insert into ulogin(uname,password,email) values('"+id+"','"+password+"','"+email+"')"; stm.execute(sql); b = true; } rs.close(); stm.close(); conn.close(); }catch (Exception e) { e.printStackTrace(); System.out.println(e); } if(b) { return true; } else return false; } }
4、構建servlet來處理事務,如果連接數據庫查找成功,則登錄成功,否則失敗;如果注冊成功返回登錄界面
登錄:由用戶輸入信息,所以使用doGet方法
@WebServlet(name = "ServletLogin") public class ServletLogin extends HttpServlet { public void init() throws ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ boolean b=false; userlogin MyPOJO=new userlogin();//新建MyPOJO類的對象myPOJO //根據標示名獲取JSP文件中表單所包含的參數 String id=request.getParameter("id"); String password=request.getParameter("password"); String result = ""; b=MyPOJO.isuserlogin(id,password);//使用模型對賬號和密碼進行驗證,返回一個boolean類型的對象 PrintWriter out = response.getWriter(); if(b){ //如果驗證結果為真,跳轉至登錄成功頁面 out.println("success"); result = "success"; //Cookie Cookie username= new Cookie("username",id); //設置路徑,這個路徑即該工程下都可以訪問該cookie 如果不設置路徑,那么只有設置該cookie路徑及其子路徑可以訪問 username.setPath("/"); username.setMaxAge(60*60); response.addCookie(username); response.sendRedirect("index.jsp"); } else { //如果驗證結果為假,跳轉至登錄失敗頁面 out.println("fail"); result = "fail"; response.sendRedirect("JSP/LoginFailed.jsp" ); } out.write(result); out.flush(); out.close(); System.out.println(result); }
注冊:使用doPost方法
@WebServlet(name = "ServletRegister") public class ServletRegister extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean b = false; register myPOJO=new register(); String id=request.getParameter("username"); String password=request.getParameter("password1"); String email = request.getParameter("email"); b=myPOJO.userregister(id,password,email); if(b){ response.sendRedirect("login.jsp"); } else{ response.sendRedirect("register.jsp"); } } }
5、在web.xml中將servlet和url-pattern進行匹配
<servlet> <servlet-name>ServletLogin</servlet-name> <servlet-class>service.ServletLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletLogin</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>ServletRegister</servlet-name> <servlet-class>service.ServletRegister</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletRegister</servlet-name> <url-pattern>/register.do</url-pattern> </servlet-mapping>
6、測試運行
將服務器端的mysql數據庫中加入一條用戶信息:
insert into ulogin values('test1','123',null);
網頁上輸入該信息:
點擊登錄:登陸成功
注冊:在注冊框中輸入信息:
提交后在數據庫中查找該用戶信息:
注意:項目中應該導入jsp-api.jar和servlet-api.jar兩個jar包
到此,相信大家對“java+mysql怎么實現登錄和注冊功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。