JSP+MySQL Realization of Website Login and Registration Case

  • 2021-11-02 01:58:32
  • OfStack

In order to practice my hands, I tried to do a small case of logging in and registering a website myself. Because there is no beautification treatment, the interface is not very good-looking.

The functions of the website are as follows:

User first registration function
User login function

I will show it in modules below

Registration module

First, you need a registration interface, as follows: 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">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset"><br>
</body>
</html>

Then there is the registration processing page corresponding to action, as follows: 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>

Small summary:
Deficiencies:
The operation of the database is not good enough, and the unused resources are not closed in time. The unused open resources should be closed in time to release the resources.
The interface effect is not good enough, and the output of response precedes the output of out.
Database operation is too cumbersome, should be integrated under 1, do a special database operation toolkit, in order to achieve good code reusability!

Login module

The first is the login interface, login. jsp. I add a hyperlink (the intention is to let login. jsp be used as a portal page to realize the effect of login registration with 2 as 1, although the two are not combined with 2 as 1, and the registration interface is too simple). Let's take a look at it first.


<%@ 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">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset"><br>
Click me to <font color="green"><a href="register.jsp">Register</a>!</font><br>


</form>

</body>
</html>

Then there is the processing page for login information, 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>

For successful login users, jump to the successful login interface 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>

For users who fail to log in, give a warm page prompt, 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>

Big summary:

Progress:

The session object is used to store the name information of user login, and the information interaction between pages is realized
Cooperate with MySQL and experience the mode of JEE to a certain extent

Deficiencies:

The code is too redundant and reusable
The utilization rate of resources is not high, and the used resources that are no longer used should be closed in time. Although java virtual machine has automatic garbage collection mechanism, it is best to develop good habits!
The interface control is not good enough, the experience is poor, and there is a lack of thinking

Points for improvement:

With complicated user registration, it is better to use bean
Modularization, using the concept of MVC
Improve the permissions of the interface to prevent chain theft
Add other functions such as uploading files and downloading files to enrich the functions of the website.


Related articles: