java+mysql實(shí)現(xiàn)登錄和注冊功能
初學(xué)JAVA EE,老師留下一小作業(yè),用JAVA實(shí)現(xiàn)與服務(wù)器端交互,實(shí)現(xiàn)登錄和注冊功能,初學(xué)一種專業(yè)課很多老師都會留下一種讓學(xué)生實(shí)現(xiàn)登錄和注冊的作業(yè)。
下面是記錄的實(shí)現(xiàn)步驟:
1、首先是賬號密碼輸入框和按鈕:
登錄:
<form action="login.do" ?method="get"> ? ? <table> ? ? ? ? <tr><td>賬號:</td><td><input style="line-height: 28px;" type="text" name="id" /></td></tr> ? ? ? ? <tr><td>密碼:</td><td><input style="line-height: 28px;" type="password" name="password" /></td></tr>? ? ? ? ? <tr><td> <a href="register.jsp" rel="external nofollow" >還沒注冊?點(diǎn)擊注冊</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>*確認(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" >返回登錄頁</a> <input ? ? ? ? ? ? ? ? ? ? type="submit" name="submit" value="提交注冊"></td> ? ? ? ? </tr> ? ? </table> </form>
兩個form表的屬性里都有action,可理解為是用來標(biāo)記登錄和注冊的,在web.xml中和對應(yīng)的servlet綁定
2、數(shù)據(jù)庫建表,插入數(shù)據(jù)
在服務(wù)器中打開MySQL,建表
create table login( uname char(15) primary key, password char(15) not null, email char(20) )
3、sql語句內(nèi)嵌到j(luò)ava語句中,查找(登錄)和插入(注冊)用戶信息,以下類名即為文件的名字
登錄:
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; ? ? } }
注冊:
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來處理事務(wù),如果連接數(shù)據(jù)庫查找成功,則登錄成功,否則失??;如果注冊成功返回登錄界面
登錄:由用戶輸入信息,所以使用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 ? ? ? ? ? //根據(jù)標(biāo)示名獲取JSP文件中表單所包含的參數(shù) ? ? ? ? String id=request.getParameter("id"); ? ? ? ? String password=request.getParameter("password"); ? ? ? ? String result = ""; ? ? ? ? ? b=MyPOJO.isuserlogin(id,password);//使用模型對賬號和密碼進(jìn)行驗(yàn)證,返回一個boolean類型的對象 ? ? ? ? PrintWriter out = response.getWriter(); ? ? ? ? if(b){ ?//如果驗(yàn)證結(jié)果為真,跳轉(zhuǎn)至登錄成功頁面 ? ? ? ? ? ? out.println("success"); ? ? ? ? ? ? result = "success"; ? ? ? ? ? ? //Cookie ? ? ? ? ? ? Cookie username= new Cookie("username",id); ? ? ? ? ? ? ? //設(shè)置路徑,這個路徑即該工程下都可以訪問該cookie 如果不設(shè)置路徑,那么只有設(shè)置該cookie路徑及其子路徑可以訪問 ? ? ? ? ? ? ? username.setPath("/"); ? ? ? ? ? ? username.setMaxAge(60*60); ? ? ? ? ? ? response.addCookie(username); ? ? ? ? ? ? response.sendRedirect("index.jsp"); ? ? ? ? } ? ? ? ? else { ?//如果驗(yàn)證結(jié)果為假,跳轉(zhuǎn)至登錄失敗頁面 ? ? ? ? ? ? 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進(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、測試運(yùn)行
將服務(wù)器端的mysql數(shù)據(jù)庫中加入一條用戶信息:
insert into ulogin values('test1','123',null);
網(wǎng)頁上輸入該信息:
點(diǎn)擊登錄:登陸成功
注冊:在注冊框中輸入信息:
提交后在數(shù)據(jù)庫中查找該用戶信息:
注意:項(xiàng)目中應(yīng)該導(dǎo)入jsp-api.jar和servlet-api.jar兩個jar包
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)登錄和注冊案例
- Java使用IO模擬注冊登錄
- Java基于IO流實(shí)現(xiàn)登錄和注冊功能
- Java實(shí)現(xiàn)登錄與注冊頁面
- javaweb實(shí)現(xiàn)注冊登錄頁面
- JavaWeb實(shí)現(xiàn)用戶登錄注冊功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Java+mysql用戶注冊登錄功能
- JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼
- Servlet+JavaBean+JSP打造Java Web注冊與登錄功能
- java實(shí)現(xiàn)注冊登錄系統(tǒng)
相關(guān)文章
IDEA設(shè)置JVM運(yùn)行參數(shù)的方法步驟
這篇文章主要介紹了IDEA設(shè)置JVM運(yùn)行參數(shù)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08java中List集合及其實(shí)現(xiàn)類的方法詳解
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于java中List集合及其實(shí)現(xiàn)類的方法介紹(附代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。下面我們就來學(xué)習(xí)一下吧2019-06-06java數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01通過idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動過程圖解
這篇文章主要介紹了通過idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Freemaker Replace函數(shù)的正則表達(dá)式運(yùn)用
這篇文章主要介紹了Freemaker Replace函數(shù)的正則表達(dá)式運(yùn)用 的相關(guān)資料,需要的朋友可以參考下2015-12-12Java將GeoHash轉(zhuǎn)化為對應(yīng)的經(jīng)緯度坐標(biāo)實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)將GeoHash轉(zhuǎn)化為對應(yīng)的經(jīng)緯度坐標(biāo)的相關(guān)資料,需要的朋友可以參考下2016-01-01