Receive Request Parameters with JavaBean

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to use JavaBean to directly receive multiple parameters from a request. You will implement a login process where the login page has a form with multiple input fields, and the backend will receive and process the form data using a JavaBean object.

👀 Preview

  • Login page

  • Login result

🎯 Tasks

In this project, you will learn:

  • How to implement the UserVO class to represent the user information and receive the parameters from the request
  • How to implement the UserController class to handle the login request and use the UserVO object to receive the parameters
  • How to test the application and verify the received parameters are correctly printed to the backend console

🏆 Achievements

After completing this project, you will be able to:

  • Create a JavaBean class to represent and receive multiple parameters from a request
  • Use Spring MVC to handle HTTP requests and process the received data
  • Test the application and verify the expected behavior

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/ConcurrentandNetworkProgrammingGroup(["`Concurrent and Network Programming`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ConcurrentandNetworkProgrammingGroup -.-> java/working("`Working`") subgraph Lab Skills java/classes_objects -.-> lab-300376{{"`Receive Request Parameters with JavaBean`"}} java/oop -.-> lab-300376{{"`Receive Request Parameters with JavaBean`"}} java/working -.-> lab-300376{{"`Receive Request Parameters with JavaBean`"}} end

Implement the UserVO Class

In this step, you will learn how to implement the UserVO class to receive multiple parameters from the request.

  1. Navigate to the org.labex.vo package and create a new Java class named UserVO.
  2. In the UserVO class, declare the following private attributes:
    • username: a String to store the username
    • password: a String to store the password
  3. Generate the getter and setter methods for the username and password attributes.
  4. Implement the toString() method to return a string representation of the UserVO object.

Your UserVO.java file should look like this:

package org.labex.vo;

public class UserVO {
    private String username;
    private String password;

    @Override
    public String toString() {
        return "UserVO{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    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;
    }
}

Implement the UserController Class

In this step, you will learn how to implement the UserController class to handle the login request and use the UserVO object to receive the parameters.

  1. Navigate to the org.labex.controller package and create a new Java class named UserController.
  2. Annotate the UserController class with @RestController to make it a Spring MVC controller.
  3. Implement a method named login() that handles the HTTP POST request to the "/login" endpoint.
  4. Annotate the login() method with @PostMapping("/login") to map it to the "/login" endpoint.
  5. Declare a single parameter for the login() method, which should be a UserVO object.
  6. Inside the login() method, print the received UserVO object to the backend console using System.out.println().
  7. Return the username from the UserVO object as the response.

Your UserController.java file should look like this:

package org.labex.controller;

import org.labex.vo.UserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @PostMapping("/login")
    public String login(UserVO userVO) {
        System.out.println(userVO);
        return userVO.getUsername();
    }
}

Now, you have completed the implementation of the UserVO class and the UserController class. In the next step, you will test the application.

Test the Application

To test the application, follow these steps:

  1. In your terminal, navigate to the SpringMVCJavaBean project directory using the following command:
cd ~/project/SpringMVCJavaBean/
  1. Ensure that the web application is running. You can start the application by running the following command:
mvn clean tomcat7:run
  1. Open a web browser and navigate to http://localhost:8080/login.jsp. You should see the login page.

  2. Enter a username and password, then click the "Login" button.

  3. In the backend console, you should see the received UserVO object printed, similar to the following output:

UserVO{username='taylorJoy', password='123'}
  1. The response from the server should be the username, which will be displayed on the web page.

Congratulations! You have successfully implemented the JavaBean Multiple Parameters project.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like