'How to send an arrayList in Java to HTML (Not JSP)

I created a java application which is connected to a database. The regular java console application part of it works just fine. Now I want to display my Result Set on an HTML web page. BUT I keep getting a 404 error from Ajax via the console on Chrome. Here is my Java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;

public class EmployeeDao
{
   /*  Database Parameters */
   public List<tickets> viewMyTickets(HttpServletRequest req, HttpServletResponse resp)
   {
       HttpSession  appSession = req.getSession(false); 
       PreparedStatement myStatement = null;
       String userUsername = req.getParameter("username");
       String userPassword = req.getParameter("password");
       String userEmployeeID = req.getParameter("employeeID");
       int userEmployeeID2 = Integer.parseInt(userEmployeeID);
       String usernameSession = appSession.getAttribute("username").toString();
       String userPasswordSession = appSession.getAttribute("password").toString();
       ResultSet myRs;
       tickets activeTickets;
       int userTicketNumber = 0;
       String userTicketsDescription = " ";
       String userTicketsStatus = " ";
       Double userReimbursementAmount = 0.0;
       Timestamp userDateSubmitted = null;
       Timestamp userDateResolved = null;
       String userReimbursementType = " ";
       
       
       if(userUsername.equals(usernameSession ) && userPassword.equals(userPasswordSession) )
       {
           String selectAllEmployeeRequests = "SELECT * FROM tickets WHERE employeeID = ?";
           
           try 
           {
              myStatement = AndreConnection.prepareStatement(selectAllEmployeeRequests);
              myStatement.setInt(1, userEmployeeID2);
              myRs = myStatement.executeQuery();
              List<tickets> employeeTicketsList = new ArrayList<tickets>();
              while (myRs.next()) 
              {
                  userTicketNumber = myRs.getInt("ticketNumber");
                  userTicketsDescription = myRs.getString("ticketsDescription");
                  userTicketsStatus = myRs.getString("ticketsStatus");
                  userReimbursementAmount = myRs.getDouble("reimbursementAmount");
                  userDateSubmitted = myRs.getTimestamp("dateSubmitted");
                  userDateResolved = myRs.getTimestamp("dateResolved");
                  userReimbursementType = myRs.getString("reimbursementType");
                  userEmployeeID2 = myRs.getInt("employeeID");
                  
                  activeTickets = new tickets(userTicketsDescription, userTicketsStatus, userReimbursementAmount, userDateSubmitted, userReimbursementType, userEmployeeID2);
                  employeeTicketsList.add(activeTickets);
              }
              PrintWriter printer = resp.getWriter();
              printer.write(new ObjectMapper().writeValueAsString(employeeTicketsList));
              return employeeTicketsList;
           } 
           catch (SQLException e) 
           {
              e.printStackTrace();
           } 
           catch (IOException e) 
           {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
            
       }
       else
       {
           try 
           {
             PrintWriter out = resp.getWriter();
             out.println("<html><head><title>Bad Login</title></head><body><h1> Go back and login </h1></body></html>");
             resp.sendRedirect("ERS/index.html");
             
           } 
           catch (IOException e) 
           {
             e.printStackTrace();
           }  
          
       }
    return null;
   
}

Here is my Ajax code:

window.onload = main;

function main()
{    
    ether();
    getInfo();     
}

function ether() 
{ 
    queryString = document.location.search;
    let params = new URLSearchParams(queryString);
    let username = params.get("username");
    document.getElementById("welcome").innerText = "Welcome " + username;
} 

function AndreAjax()
{ 
   document.getElementById('Display').addEventListener('load', getInfo() );
}

function getInfo()
{  
   getRequest();
   return false;
}//end of getNewFile function

function getRequest()
{  
    try
    {
       xhttp = new XMLHttpRequest();    
    }
    catch(err1)
    {
       try
       {
           xhttp = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(err2)
       {
           try
           {
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");    
           }
           catch(err3)
           {
               xhttp = false;
           }
       }
    }
    

    if(xhttp)
    {  
       xhttp.onreadystatechange = getServerData;
       
       xhttp.open(`GET`, `http://localhost:9001/ERS/employeeTicketsList` );
       
       xhttp.send();
       setTimeout("getRequest()", 5*1000);
        
    }
    else
    { 
       document.getElementById("Display").innerHTML = "Sorry I could not creat an XMLHttpRequest";   
    }
}//end of getRequest function

function getServerData()
{  
    if(xhttp.readyState==4)
    {
      alert("xhttp.status = " + xhttp.status);
    }

    if(xhttp.readyState==4 && xhttp.status==200)
    {   
       var displayContent = JSON.parse(xhttp.responseText); 
       document.getElementById("Display").innerHTML = displayContent;  
       alert(displayContent);  
    }
    else
    {
       document.getElementById("Display").innerHTML = " waiting Andre";
           
    }
     
    
}

Question: Why can't Ajax find http://localhost:9001/ERS/employeeTicketsList (this is the source of the 404 error in the console in Google Chrome and Firefox) Login for user works just fine, username displays at top of webpage



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source