(Fixed) Root folder path for JSP project (using Tomcat 7) in Eclipse EE

OK. I need to reconsider my question. My previous question is too vague to show my problem. enter image description here

The above screenshot shows my dynamic web project. It has several Java codes that work correctly as standalone.

Also, I have an index.jsp file that is inside:

<!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=UTF-8">
<title>PFBankingSystem</title>
</head>
<body>
<%
    PFSystemTemplate PF = new PFSystemTemplate();
    PF.init();
    PF.filterConnection();
    PF.filterExecution();
%>

      

PF ... the blahblah code is the same as the code originally inside the main function. So hitting "run on server" simply means launching my original standalone program, just using .jsp. (This is similar to using jsp as a console. I'll add an interface later.)

Then my original java program tries to read "BankingDataExample.dat" using Java I / O files. (You can find this file in the screenshot above. I just put it in both the Project root folder and the WebContent folder.)

BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));

      

...

public static final String BANKING_DATA_FILE = "BankingDataExample.dat";

      

Then my program keeps saying it can't find "BankingDataExample.dat".

enter image description here

I don't know how to use a servlet, but if using a servlet is just a solution, I'll give it a try. But for now, I just want to find a suitable place where I can put my input file.

================================================== ================================================== ================================================== ================================================== ========================== / p>

SourceFilter.java

public class SourceFilter extends PFGeneralFilter {

public SourceFilter() {
    super();
}

@Override
public void compute() throws EndOfStreamException {
    try {
        BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));
        String bankingDatum = "";
        while((bankingDatum = brForBankingData.readLine()) != null) {
            this.outPorts[Constant.DEFAULT_PORT].write(Utility.convertStringToByteArray(bankingDatum));
        }
        brForBankingData.close();
        throw new EndOfStreamException();
    } catch (IOException e) {
        throw new EndOfStreamException();
    }
}
}

      

PFGeneralFilter.java

public abstract class PFGeneralFilter implements Runnable, PFFilterInterface {
protected PipedInputStream[] inPorts;
protected PipedOutputStream[] outPorts;

public PFGeneralFilter() {
    this.inPorts = new PipedInputStream[]{new PipedInputStream()};
    this.outPorts = new PipedOutputStream[]{new PipedOutputStream()};
}

public PFGeneralFilter(int numberOfInputPorts, int numberOfOutputPorts) {
    this.inPorts = new PipedInputStream[numberOfInputPorts];
    for(int i = 0; i < numberOfInputPorts; i++)
        this.inPorts[i] = new PipedInputStream();

    this.outPorts = new PipedOutputStream[numberOfOutputPorts];
    for(int i = 0; i < numberOfOutputPorts; i++)
        this.outPorts[i] = new PipedOutputStream();
}

private void closePorts() {
    try {
        for(int i = 0; this.inPorts != null && i < this.inPorts.length; i++)
            this.inPorts[i].close();
        for(int i = 0; this.outPorts != null && i < this.outPorts.length; i++)
            this.outPorts[i].close();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

@Override
public void run() {
    while(true) {
        try {
            compute();
        } catch (Exception e) {
            if (e instanceof EndOfStreamException)
                return;
        }
    }
}

@Override
public void connect(int indexForOutputPortInThisFilter, PipedInputStream connectedInputPortInNextFilter) throws IOException {
    this.outPorts[indexForOutputPortInThisFilter].connect(connectedInputPortInNextFilter);
}

@Override
public PipedInputStream getInputPort(int index) {
    return this.inPorts[index];
}

@Override
public PipedOutputStream getOutputPort(int index) {
    return this.outPorts[index];
}

protected class EndOfStreamException extends Exception {
    private static final long serialVersionUID = 1L;

    public EndOfStreamException() {
        closePorts();
    }
}

abstract public void compute() throws EndOfStreamException;

      

}

+3


source to share


4 answers


I'm not sure if you are doing this in Servlet or JSP, say you only put the file in webContent directory, you can access file like this in Servlet or JSP

String path = request.getServletContext().getRealPath("/") + BANKING_DATA_FILE;
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));

      

I don't see the JAVA class PFSystemTemplate, but in jsp you really need to go through the path where the application is deployed, you need to make a class and use it like this:

PFSystemTemplate PF = new PFSystemTemplate(request.getServletContext().getRealPath("/"));

      



parmameter will need to be passed to the SourceFilter constructor. and of course you need to add a constructor to the PFSystemTemplate class.

The modified SourceFilter might look like this:

public class SourceFilter extends PFGeneralFilter {
private String appPath;
public SourceFilter(String appPath) {
    super();
    this.appPath = appPath;
}

@Override
public void compute() throws EndOfStreamException {
    try {
        BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(appPath + Constant.BANKING_DATA_FILE)));
        String bankingDatum = "";
        while((bankingDatum = brForBankingData.readLine()) != null) {
            this.outPorts[Constant.DEFAULT_PORT].write(Utility.convertStringToByteArray(bankingDatum));
        }
        brForBankingData.close();
        throw new EndOfStreamException();
    } catch (IOException e) {
        throw new EndOfStreamException();
    }
}
}

      

+1


source


Try the following:

ServletContext servletContext = request.getSession().getServletContext();

String path = servletContext.getRealPath("/BankingDataExample.dat");

BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));

      

If the code above doesn't work, try the following:



BufferedReader br = new BufferedReader(new InputStreamReader(
        getClass().getResourceAsStream("/BankingDataExample.dat"), "Windows-1252"));

      

In case your file is located at / WEB-INF / classes / BankingDataExample.dat

+1


source


We can put the readable file directly into the main project folder. As shown below. enter image description here

0


source


Stop seeing it as a file and start thinking of it as something you need. Place it in the root of your source folder (where it is currently available for web browsers) and invoke getClass().getClassLoader().getResourceAsStream(String)

. Then it will continue to work if you package your web app as .war too.

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

0


source







All Articles