The implementation of registration function in Java Web

Implementation of registration function Experimental steps 1. Create the database mydb with the character code of utf-8. 2. Create a tb_user table in...
Experimental steps
After the experiment
Implementation of registration function

Experimental steps

1. Create the database mydb with the character code of utf-8.
2. Create a tb_user table in the mydb database. The structure of the table is as follows:

3. Import the project of Experiment 1 in eclipse. The project structure after import is as follows:

4. Copy the register folder to the WebContent directory, which contains the registration page and related resources.
5. Write a string encryption class Md5Encrypt.
Create a package named "swu.xxj.util" in the src directory, and create a class named "Md5Encrypt"; the code of "Md5Encrypt" class is as follows:

package swu.xxj.util; import java.security.MessageDigest; public class Md5Encrypt { public String Encrypt(String strSrc) { MessageDigest md = null; Md5Encrypt mept = new Md5Encrypt(); String strDes = null; byte[] bt = strSrc.getBytes(); try { md = MessageDigest.getInstance("MD5"); md.update(bt); strDes = mept.bytes2Hex(md.digest()); //to HexString } catch (Exception e) { System.out.println("Invalid algorithm.\n" + e.getMessage()); return null; } return strDes; } private String bytes2Hex(byte[] bts) { String des = ""; String tmp = null; for (int i = 0; i < bts.length; i++) { tmp = (Integer.toHexString(bts[i] & 0xFF)); if (tmp.length() == 1) { des += "0"; } des += tmp; } return des; } }

6. Add a method addUser method to the UserService class in the swu.xxj.service package to save the registered user information. The complete code for the method is as follows:

public static boolean addUser(String name,String password, String phone){ Connection conn=null; conn=DbConnect.getConnection(); boolean flag=false; String sql="insert into tb_user(name,password,phone) values(?,?,?)"; try { PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, password); ps.setString(3, phone); int n=ps.executeUpdate(); if(n>0){ flag=true; } ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ DbConnect.closeConnection(conn); } return flag; }

7. Create a Servlet named UserRegister in the swu.xxj.control package. And write the following code under the doPost method of the Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); String password=request.getParameter("password"); Md5Encrypt md5encrypt=new Md5Encrypt(); password=md5encrypt.Encrypt(password); boolean flag=UserService.checkUserExist(name, password); if(flag){ HttpSession session=request.getSession(); session.setAttribute("name", name); session.setAttribute("password",password); response.sendRedirect("index.jsp"); } else{ response.sendRedirect("mylogin/login.jsp"); } }

After the experiment

1. Experiment 2 program block diagram

2. Database creation

3. Experimental results

4. Precautions
·In order to match the password, both places need to be encrypted.


·The index.jsp file is in the WebContent directory

·Set auto increment at id

[ps]
All of the above are the experimental results under the guidance of teachers. Welcome to discuss and exchange. Please download the project resources by yourself.

3 December 2019, 04:47 | Views: 9122

Add new comment

For adding a comment, please log in
or create account

0 comments