简体   繁体   中英

Calling Main Method in Java Class From JSP

I am a newbie to Java Web Development and have ran into a bit of difficulty.

I have an assignment where I have to create a JSP page that displays the contents of an XML File. I have created a Java application that parses the XML file and sends it to a txt file which runs perfectly. ReadXml is displayed below, there is a class Item which is a getter and setter and ParseXml but they are functioning as hoped.

package xml.reader;

import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; 
import java.io.PrintStream;

import xml.reader.Item;

public class ReadXml {

     public static void main(String args[]) throws FileNotFoundException{

    File file = new File("PageOutput.txt");
    FileOutputStream fos = new FileOutputStream(file);
    PrintStream ps = new PrintStream(fos);

    XmlParser read = new XmlParser();
    List<Item> readConfig = read.readConfig("UnderMaintenanceConfig.xml");

    for (Item item : readConfig)
    {
        System.setOut(ps);
        System.out.println(item);
    }


  }
} 

I have also been able to display the txt file within my JSP Page, this is a bit of a roundabout way of doing this however it is a requirement of the assignment. This means that when changes are made to the XML file, it should filter down to the .txt file and the changes should then display to the JSP page. Below shows my JSP Page:

<%@page import ="java.io.*" %>
<%@page import ="java.util.List" %>
<%@page import ="xml.reader.ReadXml" %>
<%@page import ="xml.reader.Item" %>
<%@page import ="xml.reader.XmlParser" %>

<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HoldingPage</title>
</head>
<body>

<jsp:useBean id="test" class="xml.reader.ReadXml" /> 

<%
ReadXml a = new ReadXml();
a.main(null);
%>

<% 
 InputStreamReader in = new InputStreamReader(new FileInputStream("C:\\workspace\\WS\\HoldingPage\\PageOutput.txt"));
        BufferedReader br = new BufferedReader(in);
        String line = br.readLine();

        while(line!=null){
        out.println(line);
        line = br.readLine();
        out.println("<br>");
        }
%>
</body>
</html>

I searched through resources to find a solution but have not been successful, I was hoping I could get some answers on how to call a java class from with a JSP Page, essentially I want my page to execute ReadXml when it is loaded so that the latest version of the XML file and txt file are loaded.

Thanks for your help in advance!

public class ReadXml {

    public void doSomeThing(){

    File file = new File("PageOutput.txt");
    FileOutputStream fos = new FileOutputStream(file);
    PrintStream ps = new PrintStream(fos);

    XmlParser read = new XmlParser();
    List<Item> readConfig = read.readConfig("UnderMaintenanceConfig.xml");

    for (Item item : readConfig)
    {
        System.setOut(ps);
        System.out.println(item);
    }


  }
} 

You can call functions. Change main method to a function and use like this.

<%
ReadXml a = new ReadXml();
a.doSomeThing();
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM