struts2 framework + mysql to realize user login and registration

Introduction to Demo

This Demo specifically implements the following functions:
1. Based on struts2 framework + MySQL database validation, user login and registration functions are realized.
2. User registration, respectively, uses client-side verification and server-side verification to achieve the verification of user input information. For information that does not meet the requirements, prompts are given and registration is not allowed, and registration needs to be filled in again.

II. Major Codes

1.login.jsp: This page implements multiple action s in a Form form. See the method and principle of implementation. http://blog.csdn.net/u012829611/article/details/70243230

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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</title>
</head>
<body background="images/xxx.gif">
<center>

<script type="text/javascript">
    function regist(){
        myform.action="register.action";
        myform.submit();
    }
</script>

<s:form action="login" method="post" name="myform">
    <s:textfield name="username" label="User name" cssStyle="width:160px;heigh:26px;"/>
    <s:password name="password" label="Password" cssStyle="width:160px;heigh:26px"/>

    <!--  
    <s:submit type="image" src="images/login.gif"/> 
    <s:submit value="Sign in"/> 
    -->

    <input type=submit value="Sign in">
    <input type="button" value="register" onclick="regist()"/>
</s:form>

</center>
</body>
</html>

2.LoginAction.java: Login action for database validation of usernames and passwords

package com.cy.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.cy.domain.User;
import com.cy.service.UsersService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private String username;
    private String password;
    private HttpServletRequest request; 

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String execute() throws Exception {

        UsersService usersService=new UsersService();
        User user=new User();

        request=ServletActionContext.getRequest();

        user.setUsername(username);
        user.setPassword(password);

        if(usersService.checkUser(user)!=null){

            request.getSession().setAttribute("userInfo", user);

            return SUCCESS;
        }else{

            return ERROR;
        }       

    }

    public String register(){

        return "regist";
    }

}

3.RegistAction.java: Register action to add user information in the database. At the same time, verify the input information of user registration.

package com.cy.action;

import java.util.regex.Pattern;

import com.cy.utils.SqlHelper;
import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

    private String username;
    private String password;
    private String ip="10.160.104.199";

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String execute() throws Exception {

        String sql="insert into user(username,password,ip) values(?,?,?)";
        String []paras={username,password,ip};
        SqlHelper sqlHelper=new SqlHelper();
        sqlHelper.executeUpdate(sql, paras);
        return SUCCESS;

    }

    //Server-side validation: override the validate() function in struts2 framework to verify the input information when registering users (recommended use of this method)
    //Client-side validation: Use script script to implement validation in register.jsp page (see register.jsp page for related code)
    @Override
    public void validate() {

        if(username==null||"".equals(username.trim())){
            this.addFieldError("username", "User name cannot be empty!");

        }else if(!Pattern.compile("^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$").matcher(username.trim()).matches()){
            this.addFieldError("username", "User name is student number! Length 5~20 Between, and begin with letters!");

        }else if(password==null||"".equals(password.trim())){
            this.addFieldError("password", "Password cannot be empty!");

        }else if(password.length()>20||password.length()<6){
            this.addFieldError("password", "Password length must be 6~20 Between!");

        }

    }

}

4. Screenshots of user input errors:

5. SQL statement created by database: database name is lms, table name is user

create database lms;
use lms;
create table user(username char(20) primary key,
                  password varchar(30),
                  ip varchar(30)
                  )charset=utf8;

III. Explanation

1. This article lists some of the main codes, which are available in full Demo.
http://download.csdn.net/detail/u012829611/9819232 After decompression, it can be directly imported into eclipse to run.
2. The main code above lists the SQL statements created by the database, and the database can be created by input.
3. How to configure multiple action s in a form? Detailed Implementation Principle and Code http://blog.csdn.net/u012829611/article/details/70243230
4. In the implementation of database operations (connection, query, addition, deletion, modification), a very good encapsulation, high rate of code universality, basic can be used directly.

Reprint please note! Thank you!

Keywords: Database Java SQL JSP

Added by LTJason on Mon, 08 Jul 2019 05:08:15 +0300