為了練手,我就自己試著做了一個網站的登錄與注冊的小案例。由於沒有做美化處理,所以界面並不是很好看。
網站實現的功能如下:
•用戶首次注冊功能
•用戶登錄功能
下面我將會分模塊展示
注冊模塊
首先需要一個注冊界面,如下register.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>User to Register Page!</title> </head> <body> <hr><br>Welcome to this <font color="green">Enroll(Register) Page</font>!<br> <form action="do_register.jsp" method="get"> <br> <h1>Please input your message:</h1><br> Name:<input type="text" name="register_name"><br> Pswd:<input type="password" name="register_password"><br> <br><br><br> <input type="submit"> <input type="reset"><br> </body> </html>
然後就是action對應的注冊處理頁,如下do_register.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Server to do the register page!</title> </head> <body> <% String Register_name=request.getParameter("register_name"); String Register_password=request.getParameter("register_password"); %> <% try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/summer", "root", "mysql"); Statement stmt=conn.createStatement(); //desogn the sql statement String InsertSQL="INSERT INTO User(Name,Password) values('"+Register_name+"','"+Register_password+"')"; System.out.println(Register_name+"\t"+Register_password); //do the query operation,and here is the most important sql statement. int FLAG=stmt.executeUpdate(InsertSQL); if(FLAG>0){ response.getWriter().write("Congratulation! REgister Success!"); }else{ response.getWriter().write("Sorry!Register Failed!\nPlease Retry it!"); } }catch(SQLException e){ } %> </body> </html>
小總結:
不足之處:
•對於數據庫的操作做得不夠好,沒有及時的將不用的資源關閉,應該及時的對那些不用的打開的資源進行關閉操作,釋放資源。
•界面效果做的不夠好,response輸出是先於out的輸出的。
•數據庫操作顯得過於繁瑣,應該集成一下,做一個專門處理數據庫操作的工具包,以實現代碼的良好的復用性!
登錄模塊
首先是登錄界面,login.jsp,鄙人加進去一個超鏈接(用意是讓login.jsp作為門戶頁面,實現登錄注冊合二為一的效果,雖然二者並沒有合二為一,而且注冊界面過於簡單了),大家就先湊活看吧。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>User Login Page</title> </head> <body> <hr><br>Welcome to this <font color="green">Login Page</font>!<br> <form action="do_login.jsp" method="get"> <br> <h1>Please input your message:</h1><br> Name:<input type="text" name="name"><br> Pswd:<input type="password" name="password"><br> <br><br><br> <input type="submit"> <input type="reset"><br> Click me to <font color="green"><a href="register.jsp">Register</a>!</font><br> </form> </body> </html>
然後是對登錄信息的處理頁,do_login.jsp:
<%@page import="java.sql.DriverManager"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Server Page Depend !</title> </head> <body> <h3>Which Pae will be depend by the user's message!</h3> <% String name=request.getParameter("name"); String password=request.getParameter("password"); %> <% Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/summer", "root", "mysql"); Statement stmt=conn.createStatement(); //desogn the sql statement String queryNumberSQL="SELECT Name from User where Name='"+name+"' and Password='"+password+"'"; //do the query operation ResultSet rs=stmt.executeQuery(queryNumberSQL); boolean flag=false; if(rs.next()){ flag=true; session.setAttribute("UserName", name); }else{ flag=false; } %> <% if(flag){ %> <jsp:forward page="login_success.jsp"></jsp:forward> <% }else{ %> <jsp:forward page="login_failed.jsp"></jsp:forward> <% } %> </body> </html>
對於登陸成功的用戶,跳轉到登陸成功界面login_success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>User Login Success Page!</title> </head> <body> <hr><br> <h1>Login Success!</h1><br> <font color="green">Welcome <%=session.getAttribute("UserName") %>!</font> <h3 align="center">your persional Message is:</h3> <% out.println("Name:"+session.getAttribute("UserName")); %> <font color="red"><a href="login.jsp">Click me</a> to log out!</font> </body> </html>
對於登錄失敗的用戶,進行溫馨的頁面提示,login.failed.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Login Failed Page!</title> </head> <body> <hr> <br> <h1><font color="red">Sorry,Login Failed</font></h1><br> <font color="red"><a href="login.jsp">Click me</a> to login!</font> </body> </html>
大總結:
進步之處:
•使用到了session對象來存儲用戶登錄的姓名信息,實現了頁面間的信息的交互
•配合了MySQL,在一定程度上體驗了JEE的模式
不足之處:
•代碼過於繁冗,復用性不好
•資源利用率不高,使用過的不再使用的資源要及時的進行關閉。雖然java虛擬機有自動的垃圾回收機制,但最好還是養成好的習慣!
•界面控制做的不夠好,體驗性差,欠缺思考
待改進之處:
•加上復雜一點的用戶注冊,使用bean的方式做處理比較好
•模塊化,使用MVC的概念
•改善界面的權限,防止盜鏈
•加上其他的諸如上傳文件,下載文件功能,豐富網站的功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。