您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java+mysql怎么實(shí)現(xiàn)登錄和注冊(cè)功能”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java+mysql怎么實(shí)現(xiàn)登錄和注冊(cè)功能”吧!
1、首先是賬號(hào)密碼輸入框和按鈕:
登錄:
<form action="login.do" method="get"> <table> <tr><td>賬號(hào):</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" >還沒(méi)注冊(cè)?點(diǎn)擊注冊(cè)</a></td><td><input id="login_in" type="submit" value=" 登錄 "></td></tr> </table> </form>
注冊(cè):
<form name="form1" action="register.do" method="post"> <table width="200" border="0"> <tr> <td colspan="2"> <div id="status">注冊(cè)新用戶</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>*確認(rèn)密碼</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" >返回登錄頁(yè)</a> <input type="submit" name="submit" value="提交注冊(cè)"></td> </tr> </table> </form>
兩個(gè)form表的屬性里都有action,可理解為是用來(lái)標(biāo)記登錄和注冊(cè)的,在web.xml中和對(duì)應(yīng)的servlet綁定
2、數(shù)據(jù)庫(kù)建表,插入數(shù)據(jù)
在服務(wù)器中打開(kāi)MySQL,建表
create table login( uname char(15) primary key, password char(15) not null, email char(20) )
3、sql語(yǔ)句內(nèi)嵌到j(luò)ava語(yǔ)句中,查找(登錄)和插入(注冊(cè))用戶信息,以下類名即為文件的名字
登錄:
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){//判斷用戶名以及密碼是否與設(shè)定相符 return true; } else return false; } }
注冊(cè):
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、構(gòu)建servlet來(lái)處理事務(wù),如果連接數(shù)據(jù)庫(kù)查找成功,則登錄成功,否則失敗;如果注冊(cè)成功返回登錄界面
登錄:由用戶輸入信息,所以使用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類的對(duì)象myPOJO //根據(jù)標(biāo)示名獲取JSP文件中表單所包含的參數(shù) String id=request.getParameter("id"); String password=request.getParameter("password"); String result = ""; b=MyPOJO.isuserlogin(id,password);//使用模型對(duì)賬號(hào)和密碼進(jìn)行驗(yàn)證,返回一個(gè)boolean類型的對(duì)象 PrintWriter out = response.getWriter(); if(b){ //如果驗(yàn)證結(jié)果為真,跳轉(zhuǎn)至登錄成功頁(yè)面 out.println("success"); result = "success"; //Cookie Cookie username= new Cookie("username",id); //設(shè)置路徑,這個(gè)路徑即該工程下都可以訪問(wèn)該cookie 如果不設(shè)置路徑,那么只有設(shè)置該cookie路徑及其子路徑可以訪問(wèn) username.setPath("/"); username.setMaxAge(60*60); response.addCookie(username); response.sendRedirect("index.jsp"); } else { //如果驗(yàn)證結(jié)果為假,跳轉(zhuǎn)至登錄失敗頁(yè)面 out.println("fail"); result = "fail"; response.sendRedirect("JSP/LoginFailed.jsp" ); } out.write(result); out.flush(); out.close(); System.out.println(result); }
注冊(cè):使用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進(jìn)行匹配
<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、測(cè)試運(yùn)行
將服務(wù)器端的mysql數(shù)據(jù)庫(kù)中加入一條用戶信息:
insert into ulogin values('test1','123',null);
網(wǎng)頁(yè)上輸入該信息:
點(diǎn)擊登錄:登陸成功
注冊(cè):在注冊(cè)框中輸入信息:
提交后在數(shù)據(jù)庫(kù)中查找該用戶信息:
注意:項(xiàng)目中應(yīng)該導(dǎo)入jsp-api.jar和servlet-api.jar兩個(gè)jar包
到此,相信大家對(duì)“java+mysql怎么實(shí)現(xiàn)登錄和注冊(cè)功能”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。