Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Eclipse EE中JSP项目(使用Tomcat 7)的根文件夹路径(修订版)_Java_Eclipse_Jsp_Jakarta Ee_Tomcat7 - Fatal编程技术网

Java Eclipse EE中JSP项目(使用Tomcat 7)的根文件夹路径(修订版)

Java Eclipse EE中JSP项目(使用Tomcat 7)的根文件夹路径(修订版),java,eclipse,jsp,jakarta-ee,tomcat7,Java,Eclipse,Jsp,Jakarta Ee,Tomcat7,嗯。我需要修改我的问题。我前面的问题太模糊,无法说明我的问题。 上面的屏幕截图显示了我的动态Web项目。它有几个Java代码,可以作为独立代码正常工作 此外,我还有“index.jsp”文件,它位于: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http

嗯。我需要修改我的问题。我前面的问题太模糊,无法说明我的问题。

上面的屏幕截图显示了我的动态Web项目。它有几个Java代码,可以作为独立代码正常工作

此外,我还有“index.jsp”文件,它位于:

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

然后,我的程序一直说它找不到“BankingDataExample.dat”

我不知道如何使用servlet,但如果使用servlet是唯一的解决方案,我会尝试。但在这一点上,我只想找到适当的位置,我可以把我的输入文件

=====================================为了提供更多信息,我将两个完整的源代码=============

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;
公共抽象类PFGeneralFilter实现可运行的PFFilterInterface{
受保护的PipedInputStream[]输入端口;
受保护的PipedOutStream[]输出端口;
公共PFGeneralFilter(){
this.inport=new PipedInputStream[]{new PipedInputStream()};
this.outPorts=new PipedOutputStream[]{new PipedOutputStream()};
}
公共PFGeneralFilter(int numberOfInputPorts,int numberOfOutputPorts){
this.inPorts=新的PipedInputStream[输入端口数];
for(int i=0;i

}我们可以将要读取的文件直接放在主项目文件夹下。如下图所示。

我不确定您是在servlet还是在JSP中这样做,比如说您将文件放在webContent目录中,您可以在servlet或JSP中这样访问文件

String path = request.getServletContext().getRealPath("/") + BANKING_DATA_FILE;
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));
我没有看到JAVA类PFSystemTemplate,但在jsp中,您确实需要传递一个路径,该路径是应用程序部署的位置,您需要创建类并像这样使用它:

PFSystemTemplate PF = new PFSystemTemplate(request.getServletContext().getRealPath("/"));
需要将参数表传递给SourceFilter的构造函数。当然,您需要向类PFSystemTemplate添加构造函数

修改后的SourceFilter可能如下所示:

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();
    }
}
}
试试这个:

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

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

BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(path)));
BufferedReader br = new BufferedReader(new InputStreamReader(
        getClass().getResourceAsStream("/BankingDataExample.dat"), "Windows-1252"));
如果上述代码不正确,请尝试以下操作:

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

在这种情况下,您在/WEB-INF/classes/BankingDataExample.dat下的文件停止将其视为文件,并开始将其视为您需要的内容。将它放在源文件夹的根目录中(它现在所在的位置可供web浏览器使用),并调用
getClass().getClassLoader().getResourceAsStream(String)
。如果你把你的web应用打包成一个.war,它就会继续工作


我修改了我的问题。我不太知道如何使用Servlet,所以我不确定我可以把你的解决方案放在哪里。哦,我明白了。你能提供SourceFilter.java和PFGeneralFilter.java完整的源代码吗?我为两个文件添加了完整的代码。这将帮助你更好地理解我的代码。我修改了我的问题。当我回答叶温的回答时,我对如何使用Servlet知之甚少。似乎我应该使用GET接口来使用'request'变量,对吗?请参阅我的修订答案。我的程序仍然找不到输入文件。请记住WEB-INF是完全私有的,不能用于访问除WEB资源以外的任何内容,在您的例子中,它是一个简单的java文件,您无法从该文件访问WEB-INF。因此,请使用servlet或将您要访问的文件放在java文件的类路径或相同包中
BufferedReader br = new BufferedReader(new InputStreamReader(
        getClass().getResourceAsStream("/BankingDataExample.dat"), "Windows-1252"));