Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S06L05 - Displaying data from database

JSP and Servlets Articles
0 views

S06L05 - Displaying data from database


May 14, 2025

Listing Users on a Webpage: A Comprehensive Guide

Table of Contents

  1. Introduction ................................................................. 1
  2. Setting Up the Home Controller ............... 3
  3. Updating JSP Files .......................................... 6
    1. Modifying `listusers.jsp`
    2. Refining `index.jsp` and `error.jsp`
  4. Handling Attributes and Titles .................. 10
  5. Displaying Users in an HTML Table ........... 13
  6. Running and Testing the Application ........ 17
  7. Conclusion .................................................................. 20

Introduction

In the world of web development, efficiently displaying user information is a fundamental feature for many applications. Whether you're building a simple website or a complex enterprise system, the ability to list users dynamically on a webpage enhances user experience and functionality.

This guide delves into the process of listing users on a webpage using Java Servlets and JSP (JavaServer Pages). We'll explore how to connect to a database, fetch user data, and present it neatly in an HTML table. By the end of this eBook, you'll have a clear understanding of setting up controllers, updating JSP files, handling attributes, and ensuring your application runs smoothly.

Importance of Listing Users

  • User Management: Essential for administrative panels and user management systems.
  • Dynamic Content: Provides real-time data updates without manual intervention.
  • Enhanced UX: Offers users a clear view of available user profiles or members.

Pros and Cons

  • User Table:
    <table border="1">
        <tr>
            <th>User ID</th>
            <th>User Name</th>
            <th>Email</th>
        </tr>
        <% for(User user : users){ %>
        <tr>
            <td><%= user.getId() %></td>
            <td><%= user.getName() %></td>
            <td><%= user.getEmail() %></td>
        </tr>
        <% } %>
    </table>
    
        
    - Iterates through the user list and displays each user's ID, Name, and Email in table rows.
  • Refining index.jsp and error.jsp

    Consistency across JSP files enhances the user experience and maintains a uniform interface.

    index.jsp

    <!DOCTYPE html>
    <html>
    <head>
        <title><%= request.getAttribute("title") != null ? request.getAttribute("title") : "Home Page" %></title>
        <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    </head>
    <body>
        <h1>Welcome to the Home Page</h1>
        <a href="listusers">View Users</a>
    </body>
    </html>
    
    

    error.jsp

    <!DOCTYPE html>
    <html>
    <head>
        <title>Error Page</title>
        <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    </head>
    <body>
        <h1>An Error Occurred</h1>
        <p>Sorry, something went wrong.</p>
    </body>
    </html>
    
    

    Handling Attributes and Titles

    Managing dynamic content in JSPs involves handling attributes and titles effectively. This ensures that the pages are both functional and user-friendly.

    Setting Attributes in the Controller

    In the Home.java controller, attributes are set to pass data to the JSP:

    request.setAttribute("listUsers", users);
    request.setAttribute("title", "List of Users");
    
    
    • listUsers: Carries the list of users fetched from the database.
    • title: Sets the page title, making it dynamic and flexible.

    Accessing Attributes in JSP

    Within the JSP files, these attributes are accessed to render dynamic content:

    <%= request.getAttribute("title") %>
    
    
    • Retrieves the title set by the controller, allowing for dynamic page titles.

    Conditional Title Rendering

    To handle cases where the title might not be set, conditional checks ensure a default title is displayed:

    <title><%= request.getAttribute("title") != null ? request.getAttribute("title") : "Home Page" %></title>
    
    
    • If the title attribute is null, it defaults to "Home Page".

    Displaying Users in an HTML Table

    Presenting user data in a tabular format enhances readability and organization. Here's how to structure and populate the table dynamically.

    Creating the Table Structure

    <table border="1">
        <tr>
            <th>User ID</th>
            <th>User Name</th>
            <th>Email</th>
        </tr>
        <!-- Dynamic Rows Will Be Inserted Here -->
    </table>
    
    
    • <table border="1">: Creates a table with borders.
    • Table Headers (<th>): Define the columns for User ID, User Name, and Email.

    Populating Table Rows Dynamically

    Using JSP scriptlets, iterate through the user list and insert rows:

    <%
        for(User user : users){
    %>
    <tr>
        <td><%= user.getId() %></td>
        <td><%= user.getName() %></td>
        <td><%= user.getEmail() %></td>
    </tr>
    <%
        }
    %>
    
    
    • Looping: The for loop iterates over each User object in the users list.
    • Data Insertion: <%= user.getId() %>, <%= user.getName() %>, and <%= user.getEmail() %> retrieve and display respective user details.

    Enhancing Table Presentation

    For better aesthetics, consider adding CSS styles:

    table {
        width: 80%;
        margin: auto;
        border-collapse: collapse;
    }
    
    th, td {
        padding: 12px;
        text-align: left;
    }
    
    th {
        background-color: #f2f2f2;
    }
    
    
    • Table Styling: Enhances the table's appearance with padding, background colors, and centralized alignment.

    Running and Testing the Application

    Ensuring that your application runs smoothly is crucial for a seamless user experience. Follow these steps to deploy and test your application.

    Setting Up the Development Environment

    1. Install Java Development Kit (JDK): Ensure JDK is installed and configured.
    2. Set Up Apache Tomcat: Use Tomcat as your servlet container.
    3. Import Project into IDE: Use an IDE like Eclipse or IntelliJ IDEA for efficient development.

    Building the Project

    Compile the project to ensure there are no syntax or compilation errors.

    mvn clean install
    
    
    • Maven Command: Cleans previous builds and compiles the project afresh.

    Deploying to Tomcat

    1. Generate WAR File: Maven will produce a WAR (Web Application Archive) file.
    2. Deploy WAR to Tomcat: Place the WAR file in the Tomcat webapps directory.
    3. Start Tomcat Server: Launch the server to deploy the application.

    Testing the Application

    1. Access the Home Page: Navigate to http://localhost:8080/demo (replace demo with your project name).
    2. View Users: Click on the "View Users" link to see the list of users.
    3. Validate Data Display: Ensure that the user table displays all entries correctly.

    Troubleshooting Common Issues

    Data Not Displaying Incorrect attribute names Ensure consistency between controller and JSP
    Compilation Errors Syntax mistakes Review error logs and fix highlighted lines

    Conclusion

    Listing users on a webpage is a fundamental feature that enhances the functionality and user experience of web applications. By leveraging Java Servlets and JSP, developers can create dynamic, responsive, and organized user interfaces.

    Key Takeaways

    • Controller Setup: Essential for managing data flow between the backend and frontend.
    • JSP Customization: Vital for presenting data in a user-friendly manner.
    • Dynamic Content Handling: Attributes and conditional rendering ensure flexibility and responsiveness.
    • Robust Testing: Ensures application reliability and performance.

    Embarking on this journey equips you with the skills to manage and display user data effectively, laying the foundation for more advanced web development projects.

    SEO Keywords: Java Servlets, JSP tutorial, list users on webpage, dynamic user listing, web development guide, Java web application, JSP attributes, HTML table in JSP, Java Controller setup, database integration JSP, beginner Java JSP, user management system, Java web tutorials, servlet and JSP integration, displaying data in JSP

    Note: This article is AI generated.

    Share Article

    Back to Blog

    Single logo

    Empowering Education, One Course at a Time

    About
    About usContact usFAQs
    Legal
    Terms of ServicePrivacy Policy

    © 2026 StudyEasy Organization. All rights reserved.