Calling Main Method in Java Class From JSP

Antoin McCloskey

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!

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a class from another class with main method

From Java

calling another method from the main method in java

From Dev

Implicitly calling a method in a class from main

From Dev

Calling a default method from Interface in Main Class

From Dev

calling Java method from jsp page

From Java

Calling Java Main method from static block

From Dev

calling a method from the main to output a phrase in java

From Java

Calling a java method in jsp

From Java

calling String method from different class to main class

From Dev

Calling a parametrized method from LibraryCollection class to main Class

From Java

Java calling a void method in a different class to a main class?

From Dev

calling method to a main in java

From Dev

Call Java class method from JSP

From Dev

Calling array method in main class

From Dev

Java - Calling method from child of abstract class

From Java

java calling a method from another class

From Dev

Calling a method on a Java class from Clojure

From Dev

Calling a function in main class from different class in java

From Dev

Calling a java method with parameter value set from dropdown select in jsp

From Dev

Internal Server error while calling an Java method from JSP

From Dev

Calling a method from a class

From Dev

Calling main method with null in Java

From Dev

Calling a function from a class in main

From Dev

Calling a subclass attribute from the main class using Visual Code Java

From Dev

Calling Method of another class from Recursion Method: Java

From Dev

Java - parent class is calling a method from a child class?

From Dev

Calling main method within the class python

From Dev

In which class, main () method calling is done?

From Java

Print text from a java class method within a .jsp page

Related Related

  1. 1

    Calling a class from another class with main method

  2. 2

    calling another method from the main method in java

  3. 3

    Implicitly calling a method in a class from main

  4. 4

    Calling a default method from Interface in Main Class

  5. 5

    calling Java method from jsp page

  6. 6

    Calling Java Main method from static block

  7. 7

    calling a method from the main to output a phrase in java

  8. 8

    Calling a java method in jsp

  9. 9

    calling String method from different class to main class

  10. 10

    Calling a parametrized method from LibraryCollection class to main Class

  11. 11

    Java calling a void method in a different class to a main class?

  12. 12

    calling method to a main in java

  13. 13

    Call Java class method from JSP

  14. 14

    Calling array method in main class

  15. 15

    Java - Calling method from child of abstract class

  16. 16

    java calling a method from another class

  17. 17

    Calling a method on a Java class from Clojure

  18. 18

    Calling a function in main class from different class in java

  19. 19

    Calling a java method with parameter value set from dropdown select in jsp

  20. 20

    Internal Server error while calling an Java method from JSP

  21. 21

    Calling a method from a class

  22. 22

    Calling main method with null in Java

  23. 23

    Calling a function from a class in main

  24. 24

    Calling a subclass attribute from the main class using Visual Code Java

  25. 25

    Calling Method of another class from Recursion Method: Java

  26. 26

    Java - parent class is calling a method from a child class?

  27. 27

    Calling main method within the class python

  28. 28

    In which class, main () method calling is done?

  29. 29

    Print text from a java class method within a .jsp page

HotTag

Archive