简体   繁体   English

从JSP调用Java类中的Main方法

[英]Calling Main Method in Java Class From JSP

I am a newbie to Java Web Development and have ran into a bit of difficulty. 我是Java Web开发的新手,遇到了一些困难。

I have an assignment where I have to create a JSP page that displays the contents of an XML File. 我有一个作业,我必须在其中创建一个显示XML文件内容的JSP页面。 I have created a Java application that parses the XML file and sends it to a txt file which runs perfectly. 我创建了一个Java应用程序,该应用程序解析XML文件并将其发送到运行良好的txt文件。 ReadXml is displayed below, there is a class Item which is a getter and setter and ParseXml but they are functioning as hoped. ReadXml显示在下面,有一个Item类,它是一个getter和setter以及ParseXml,但它们的功能符合预期。

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. 我还能够在我的JSP页面中显示txt文件,这是执行此操作的一种简单方法,但这是分配的要求。 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. 这意味着当对XML文件进行更改时,它应向下过滤到.txt文件,然后更改应显示在JSP页面上。 Below shows my JSP Page: 下面显示了我的JSP页面:

<%@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. 我在资源中搜索以找到解决方案,但没有成功,我希望我可以从JSP页面上获得有关如何调用Java类的答案,从本质上讲,我希望页面在加载时执行ReadXml,以便加载了XML文件和txt文件的最新版本。

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();
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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