JSP+MySQL实现网站的登录与注册小案例

为了练手,我就自己试着做了一个网站的登录注册的小案例。由于没有做美化处理,所以界面并不是很好看。

网站实现的功能如下:

•用户首次注册功能
 •用户登录功能 

下面我将会分模块展示

注册模块

首先需要一个注册界面,如下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"><br> Pswd:<input type="password"><br> <br><br><br> <input type="submit">&nbsp;&nbsp;&nbsp;&nbsp;<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"><br> Pswd:<input type="password"><br> <br><br><br> <input type="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset"><br> Click me to <font color="green"><a href="https://www.jb51.net/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 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:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjpjd.html