How to pass an ArrayList from a Java class to JSP table

jude :

Here I tried to get xml file values to Java list and then I want to display those values inside a table. By using DOM parser I get XML values from the XML file. I want to return this output to JSP table. Please, help me to get this done!

XML reader

public class ReadXMLFile {
    public static void main(String argv[]) {
        try {
            File fXmlFile = new File("/Users/mkyong/staff.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            //optional, but recommended
            //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("staff");

            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    System.out.println("Staff id : " + eElement.getAttribute("id"));
                    System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                    System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                    System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                    System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="xmlReader.TestXmlReader"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>XML Viewer</title>
</head>
<body>
    <% TestXmlReader tc = new TestXmlReader();
    NodeList LiNew =  TestXmlReader;%>
    <p><%=LiNew %></p> //need to add in to table just for testing
</body>
</html>    
Arvind Kumar Avinash :

It doesn't work this way. You need to create a servlet program where you will have to populate the List. Inside the servlet code, you will have to put the list into the request object and forward the request to the JSP. Then, you can retrieve the list in the JSP either using scriptlet and then iterate it in the core Java way or using JSTL (recommended) library. Given below are some links you can start with:

  1. https://www.baeldung.com/intro-to-servlets
  2. https://docs.oracle.com/javaee/7/tutorial/servlets.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170898&siteId=1