Java Web realizes the function of simple registration and login - (Registration)

Keywords: JSP SQL Java xml

The registration and login function implemented here is the content of a big assignment after learning java web course. It does not involve any framework technology, but is made up of basic knowledge It is. It's suitable for children's paper that just started to touch the web.

——Home page:

Home code:

<%@ page contentType="text/html;charset=GB2312" %>
<%@page language="java" import="java.util.*" import="javax.servlet.http.*"%>
<HTML> 
<HEAD>
<title>"Kawaii dream factory</title>
</HEAD>
<BODY style="background:url(image/Fly a kite.jpg) ;background-size:cover; ">
<center>
<br><br>
<h1><font color=AE57A4 size="6px">Welcome to the doll's world!</font></h1>
</center>
<br>
<div align="center">
    <td><A href="register.jsp"><img src="image/zhuce.png"/></A></td>
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<td><A href="login.jsp"><img src="image/login.png"/></A></td>
    </div>
</BODY>
</HTML>

The icons are all in EasyIcon Downloaded

The home page is mainly used to place the links between the registration page and the login page, which is convenient for users to operate.

——Registration page:

Registration page code:

<%@ page contentType="text/html;charset=GB2312" %>
<%@page language="java" import="java.util.*" import="javax.servlet.http.*"%>
<jsp:useBean id="userBean" class="mybean.data.Register" scope="request"/>
<HEAD>
<title>Registration page</title>
<HTML><BODY style="background:url(image/Fly a kite.jpg) ;background-size:cover; ">
<br>
<a href="shouye.jsp"><img src="image/Arrow.png"/>Back to the front page</a>
<div align="center">
<FORM action="registerServlet" method="post" name=form>
<table>
<br>
<br>
    User name and password are created by3More than letters, numbers and underscores,*The item of the note must be filled in.<br>
   <tr><td>*User name:</td><td><Input type=text name="logname" ></td>
       <td>Mailing address:</td><td><Input type=text name="address"></td>
       </td></tr>
   <tr><td>*User password:</td><td><Input type=password name="password"></td>
       <td>Contact number:</td><td><Input type=text name="phone"></td></tr>
   <tr><td>*Repeat password:</td><td><Input type=password name="again_password">
       <td>Real name:</td><td><Input type=text name="realname"></td></tr>
   <tr><td><input type="submit" name="submit_button" value="Submission" style="background:url(imagepath) no-repeat" /> 
       <td><Input type=reset name="g" value="Reset" style="background:url(imagepath) no-repeat"></td> </tr>           
</table>
</Form>
</div>
<div align="center">
<p> Registration feedback:
<jsp:getProperty name="userBean"  property="backNews" /> 
<table border=3>
     <tr><td>User name:</td>
     <td><jsp:getProperty name="userBean" property="logname"/></td>
     </tr>
     <tr><td>Real name:</td>
     <td><jsp:getProperty name="userBean" property="realname"/></td>
     </tr>
     <tr><td>Mailing address:</td>
     <td><jsp:getProperty name="userBean" property="address"/></td>
     </tr>
     <tr><td>Contact number:</td>
     <td><jsp:getProperty name="userBean" property="phone"/></td>
     </tr>
</table></div >
</Body></HTML>

In the feedback of registration, users can see whether they have successfully registered. If they have successfully registered, they can log in. Of course, readers can also design their own pages according to their own ideas, such as successfully registering to jump directly to the login page.
Where registerServlet is the servlet name you configured in xml.

——Register operation to implement back-end Code:

Connect the database to realize the data insertion

servlet:

package myservlet.control;
import mybean.data.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HandleRegister extends HttpServlet {
	
   public void init(ServletConfig config) throws ServletException { 
      super.init(config);
      try {  Class.forName("com.mysql.jdbc.Driver");
      }
      catch(Exception e){} 
   }
   public String handleString(String s)
   {   try{ byte bb[]=s.getBytes("iso-8859-1");
            s=new String(bb);
       }
       catch(Exception ee){} 
       return s;  
   }
   
   public  void  doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
      String uri="jdbc:mysql://127.0.0.1:3306/toy_db?"+"user=root&password=123456&characterEncoding=gb2312";
      Connection con; 
      PreparedStatement sql; 
      
      
      // < jsp: usebean id = "userbean" class = "full class name of register"
      // 			scope="request" />
      Register userBean=new Register();  //Java bean model created
      request.setAttribute("userBean",userBean);
      // Temporary variables, saving form data
      // Save to member variable after validity verification
      String logname=request.getParameter("logname").trim();
      String password=request.getParameter("password").trim();
      String again_password=request.getParameter("again_password").trim();
      String phone=request.getParameter("phone").trim();
      String address=request.getParameter("address").trim();
      String realname=request.getParameter("realname").trim();
      if(logname==null)
           logname="";
      if(password==null)
           password="";
      if(!password.equals(again_password)) { 
         userBean.setBackNews("The two passwords are different, and the registration fails,");
         RequestDispatcher dispatcher= 
         request.getRequestDispatcher("register.jsp");
         dispatcher.forward(request, response);//Forward
         return;
      }
      boolean isLD=true;
      for(int i=0;i<logname.length();i++){
          char c=logname.charAt(i);
           if(!((c<='z'&&c>='a')||(c<='Z'&&c>='A')||(c<='9'&&c>='0'))) 
             isLD=false;
      } 
      boolean boo=logname.length()>3&&password.length()>3&&isLD;
      String backNews="";
      try{   con=DriverManager.getConnection(uri);
             String insertCondition="INSERT INTO client VALUES (?,?,?,?,?)";
             sql=con.prepareStatement(insertCondition);
             if(boo)
             { sql.setString(1,handleString(logname));
               sql.setString(2,handleString(password));
               sql.setString(3,handleString(phone));
               sql.setString(4,handleString(address));
               sql.setString(5,handleString(realname));
               int m=sql.executeUpdate();
               if(m!=0){
                  backNews="login was successful";   
                  userBean.setLogname(logname);
                  userBean.setBackNews(backNews);
                  userBean.setPhone(handleString(phone));
                  userBean.setAddress(handleString(address));
                  userBean.setRealname(handleString(realname));
                  RequestDispatcher dispatcher=request.getRequestDispatcher("/register.jsp");//Forward
				   dispatcher.forward(request,response);
               }
             }
             else {
                 backNews="Incomplete information or unqualified name and password";
                 userBean.setBackNews(backNews);  
             }
             con.close();
      }
      catch(SQLException exp){
             backNews="This member name has been used. Please change your name"+exp;
             userBean.setBackNews(backNews); 
      }
      RequestDispatcher dispatcher= 
      request.getRequestDispatcher("register.jsp");
      dispatcher.forward(request, response);//Forward
   }
   public  void  doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
      doPost(request,response);
   }
}


bean:

package mybean.data;
public class Register{  
   String  logname="" , phone="",
           address="",realname="",backNews="Please enter registration information"; 
   public void setLogname(String logname){  
      this.logname=logname;
   }
   public String getLogname(){  
      return logname;
   }
   public void setPhone(String phone){  
      this.phone=phone;
   }
   public String getPhone(){  
      return phone;
   }
   public void setAddress(String address){  
      this.address=address;
   }
   public String getAddress(){  
      return address;
   }
   public void setRealname(String realname){  
      this.realname=realname;
   }
   public String getRealname(){  
      return realname;
   }
   public void setBackNews(String backNews){  
      this.backNews=backNews;
   }
   public String getBackNews(){  
      return backNews;
   }
}

xml configuration:

Registration feedback:

Database:

Because I didn't directly set up to jump to the login page after successful registration, I added the connection to return to the home page in the registration page It is.

The novice has successfully tested the water (at least some models and some operations can achieve ha ha ha ha ha ha ha ha ha ha ha ha) (•̀ώ) y

Published 22 original articles, won praise 6, visited 829
Private letter follow

Posted by shoppingspree on Wed, 22 Jan 2020 00:32:56 -0800