🚧 Servlet User Information Query

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to implement a user query feature using servlet and database technologies. You will complete the code for the web page, servlet, and Java bean to display the user's information.

Tasks

In this project, you will learn to:

  • Complete the showUser.jsp file to display the user's name, age, and address
  • Complete the SelectServlet.java file to handle the user query request and retrieve the user information from the database
  • Complete the User.java file to represent the user information
  • Add the servlet mapping to the web.xml file

Achievements

In this project, you will learn:

  • How to use JSP to display dynamic data
  • How to use servlets to handle user requests and interact with the database
  • How to create Java beans to represent and manage data
  • How to configure the web application using the web.xml file

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ConcurrentandNetworkProgrammingGroup(["`Concurrent and Network Programming`"]) java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java/ConcurrentandNetworkProgrammingGroup -.-> java/net("`Net`") java/FileandIOManagementGroup -.-> java/io("`IO`") java/ConcurrentandNetworkProgrammingGroup -.-> java/working("`Working`") subgraph Lab Skills java/net -.-> lab-300344{{"`🚧 Servlet User Information Query`"}} java/io -.-> lab-300344{{"`🚧 Servlet User Information Query`"}} java/working -.-> lab-300344{{"`🚧 Servlet User Information Query`"}} end

Complete the showUser.jsp

In this step, you will learn how to complete the showUser.jsp file, which is used to display the user information.

  1. Open the showUser.jsp file located in the ServletMvc/src/main/webapp directory.

  2. The JSP file should display the user's name, age, and address. You can use the following code to achieve this:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.labex.entity.User" %>
<html>
<head>
    <title>Show User Information</title>
</head>
<body>
<h1>User Information</h1>
<%
    User user = (User) session.getAttribute("user");
    if (user != null) {
%>
<p>Username: <%= user.getUsername() %></p>
<p>Age: <%= user.getUserAge() %></p>
<p>Address: <%= user.getUserAddress() %></p>
<% } else { %>
<p>User information not found.</p>
<% } %>
</body>
</html>

In this code, we first import the User class from the org.labex.entity package. Then, we retrieve the user object from the session, and check if it is not null. If the user object is not null, we display its username, age, and address. If the user object is null, we display a message indicating that the user information is not found.

Complete the SelectServlet.java

In this step, you will learn how to complete the SelectServlet.java file, which is responsible for handling the user query request and retrieving the user information from the database.

  1. Open the SelectServlet.java file located in the ServletMvc/src/main/java/org/labex/servlet directory.

  2. The SelectServlet class should handle the user query request and retrieve the user information from the database. You can use the following code to achieve this:

package org.labex.servlet;

import org.labex.dao.UserDao;
import org.labex.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SelectServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // Receive parameters sent by the view
        String username = request.getParameter("uname");

        // create the JavaBean for database operations
        UserDao userDao = new UserDao();

        // Pass the parameters to the getUserByName method in userDao and pass in the query parameters
        User user = userDao.getUserByName(username);

        // Put user information into the session
        request.getSession().setAttribute("user", user);

        // Upon completion, redirect to the showUser.jsp page
        request.getRequestDispatcher("/showUser.jsp").forward(request, response);
    }
}

In this code, we first receive the username parameter sent by the view. Then, we create a UserDao object, which is responsible for interacting with the database. We call the getUserByName method of the UserDao object, passing in the username parameter, to retrieve the user information. Finally, we store the user object in the session and forward the request to the showUser.jsp page.

Complete the User.java

In this step, you will learn how to complete the User.java file, which is used to represent the user information.

  1. Open the User.java file located in the ServletMvc/src/main/java/org/labex/entity directory.

  2. The User class should have a constructor that takes three parameters: username, userAge, and userAddress. You can use the following code to add the constructor:

package org.labex.entity;

public class User {
    private String username;
    private String userPassword;
    private int userAge;
    private String userAddress;

    public User() {
    }

    // Add a constructor with three parameters: username, userAge, userAddress
    public User(String username, int userAge, String userAddress) {
        this.username = username;
        this.userAge = userAge;
        this.userAddress = userAddress;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
}

In this code, we added a constructor that takes three parameters: username, userAge, and userAddress. This constructor will be used to create a User object with the necessary information.

Add the Servlet Mapping to the web.xml

In this step, you will learn how to add the servlet mapping to the web.xml file, which is located in the ServletMvc/src/main/webapp/WEB-INF directory.

  1. Open the web.xml file.

  2. Add the servlet mapping for the SelectServlet class. You can use the following code to achieve this:

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <!-- add servlet mapping -->
    <servlet>
        <servlet-name>SelectServlet</servlet-name>
        <servlet-class>org.labex.servlet.SelectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SelectServlet</servlet-name>
        <url-pattern>/SelectServlet</url-pattern>
    </servlet-mapping>
</web-app>

In this code, we added a <servlet> element to define the SelectServlet class, and a <servlet-mapping> element to map the servlet to the /SelectServlet URL pattern.

After completing these four steps, you should have a working user query feature using servlet and database technologies.

Summary

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

Other Java Tutorials you may like