0

I am working on a personal project to get some coding practice with JSP and servlets. I am using MySql for storing data and I want to render 'user' data to user_list.jsp. Even though the page is rendering properly, It's not loading with proper images and data. I checked the code multiple times but was unable to find the root cause of this issue. I debugged my java code and it's working as expected as it's fetching the data from DB.

I would be happy to get some help to resolve this issue. Code files are as follows

ListUserServlet.java

package com.bookstore.controller.admin.user;


import com.bookstore.entity.Users;
import com.bookstore.service.UserServices;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/list_users")
public class ListUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ListUserServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServices userServices = new UserServices();
        List<Users> listUsers = userServices.listUser();
        request.setAttribute("listUsers",listUsers);
        String listPage = "user_list.jsp";
        RequestDispatcher requestDispatcher = request.getRequestDispatcher(listPage);
        requestDispatcher.forward(request, response);
    }
}

UserServices.java

package com.bookstore.service;

import com.bookstore.dao.UserDAO;
import com.bookstore.entity.Users;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class UserServices {

    private UserDAO userDAO;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Users user;
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    public UserServices(){
        entityManagerFactory = Persistence.createEntityManagerFactory("BookStoreWebsite");
        entityManager = entityManagerFactory.createEntityManager();
        userDAO = new UserDAO(entityManager);
    }

    
    public List<Users> listUser() throws ServletException, IOException {
        List<Users> listUsers = userDAO.listAll();
        return listUsers;

    }

}

Users.java

package com.bookstore.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


/**
 * Users generated by hbm2java
 */

//Entity represents object that maps to the db table
@Entity
@NamedQueries(value = { @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u ORDER BY u.fullName"),
        @NamedQuery(name = "Users.countAll", query = "SELECT Count(*) FROM Users u"),
        @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
        @NamedQuery(name = "Users.checkLogin", query = "SELECT u FROM Users u WHERE u.email = :email AND password = :password")})
@Table(name = "users", catalog = "bookstoredb")
public class Users implements java.io.Serializable {

    public Users() {
        super();
    }

    public Users(String email, String password, String fullName) {
        super();
        this.email = email;
        this.password = password;
        this.fullName = fullName;
    }

    public Users(Integer userId, String email, String password, String fullName) {
        super();
        this.userId = userId;
        this.email = email;
        this.password = password;
        this.fullName = fullName;
    }


    //need primitive value here? like Integer
    private int userId;
    private String email;
    private String password;
    private String fullName;

    @Id
    @Column(name = "user_id", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getUserId() {
        return this.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Column(name = "email", nullable = false, length = 30)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "password", nullable = false, length = 16)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "full_name", nullable = false, length = 30)
    public String getFullName() {
        return this.fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

}

UserDAO.java

package com.bookstore.dao;

import com.bookstore.entity.Users;


import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDAO extends JpaDAO<Users> implements GenericDAO<Users> {

    public UserDAO(EntityManager entityManager) {
        super();
    }

    @Override
    public Users create(Users user) {
        return super.create(user);
    }

    @Override
    public Users update(Users user) {
        return super.update(user);
    }

    @Override
    public Users get(Object userid) {
        return super.find(Users.class, userid);
    }

    @Override
    public void delete(Object userid) {
        super.delete(Users.class, userid);
    }

    @Override
    public List<Users> listAll() {
        return super.findWithNamedQuery("Users.findAll");
    }

    @Override
    public long count() {
        return super.countWithNamedQuery("Users.countAll");
    }

    public Users findByEmail(String email) {
        List<Users> listUsers = super.findWithNamedQuery("Users.findByEmail", "email", email);
        if (listUsers != null && listUsers.size() > 1) {
            return listUsers.get(0);
        }
        return null;
    }

    public boolean checkLogin(String email, String password) {
        Map<String, Object> parameters = new HashMap();
        parameters.put("email", email);
        parameters.put("password", password);
        List<Users> listUsers = super.findWithNamedQuery("Users.checkLogin", parameters);
        return listUsers.size() == 1;
    }

}

user_list.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Manage Users - Evergreen Bookstore Administration</title>
    <link rel="stylesheet" href="../css/style.css">
    <script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script>
<%--    <script type="text/javascript">--%>
<%--        $(document).ready(function () {--%>
<%--            $(".deleteLInk").each(function () {--%>
<%--                $(this).on("click", function () {--%>
<%--                    userId = $(this).attr("id");--%>
<%--                    if (confirm("Are you sure you want to delete the user with id " + userId + "?")) {--%>
<%--                        window.location = "delete_user?id=" + userId;--%>
<%--                    }--%>
<%--                });--%>
<%--            })--%>
<%--        });--%>
<%--    </script>--%>
</head>
<body>
<jsp:include page="header.jsp"/>
<div align="center">
    <h2 class="pageHeading">Users Management</h2>
<%--    <a href="user_form.jsp">Create New User</a>--%>
</div>

<c:if test="${message != null}">
    <div align="center">
        <h4 class="message">${message}</h4>
    </div>
</c:if>

<div align="center">
    <table border="1" cellpadding="5">
        <tr>
            <th>Index</th>
            <th>ID</th>
            <th>Email</th>
            <th>Full Name</th>
            <th>Actions</th>
        </tr>

        <c:forEach items="${listUsers}" var="user"  varStatus="status">
            <tr>
                <td>${status.index + 1}</td>
                <td>${user.userId}</td>
                <td>${user.email}</td>
                <td>${user.fullName}</td>
                <td><a href="edit_user?id=${user.userId}">Edit</a> &nbsp;
                    <a href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a></td>
            </tr>
        </c:forEach>
    </table>

</div>

<jsp:include page="footer.jsp"/>


</body>

</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.bookstore</groupId>
  <artifactId>BookStoreWebSite2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>BookStoreWebSite2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>6.0.0.Alpha4</version>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
        <scope>runtime</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>com.paypal.sdk</groupId>
        <artifactId>rest-api-sdk</artifactId>
        <version>1.14.0</version>
      </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.2.Final</version>
    </dependency>
<!--    <dependency>-->
<!--      <groupId>javax.servlet</groupId>-->
<!--      <artifactId>servlet-api</artifactId>-->
<!--      <version>9.0.27</version>-->
<!--    </dependency>-->
  </dependencies>

  <build>
    <finalName>BookStoreWebSite2</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

DB data(example) enter image description here

This is what I am getting for now after running it locally. enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sid
  • 122
  • 6

0 Answers0