【问题标题】:(Revised) Root folder path for JSP project (using Tomcat 7) in Eclipse EE(修订)Eclipse EE 中 JSP 项目(使用 Tomcat 7)的根文件夹路径
【发布时间】:2015-02-16 18:25:00
【问题描述】:

好的。我需要修改我的问题。我之前的问题太模糊,无法显示我的问题。

上面的屏幕截图显示了我的动态 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();
%>

PF...blahblah 代码与最初在 main 函数中的代码相同。因此,单击“在服务器上运行”意味着仅使用 .jsp 运行我原来的独立程序。 (只是使用jsp作为控制台类似,以后会添加更多接口。)

然后,我的原始 java 程序尝试使用 Java 文件 I/O 读取“BankingDataExample.dat”。 (你可以在上面的截图中找到这个文件。我只是把它放在项目根文件夹和 WebContent 文件夹中。)

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

...

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

然后,我的程序一直说找不到“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;

}

【问题讨论】:

  • 请记住 WEB-INF 是完全私有的,不能用于访问除 Web 资源以外的任何内容,在您的情况下,它是一个简单的 java 文件,您不能从中访问 WEB-INF。
  • 所以要么使用 servlet,要么将要访问的文件放在类路径或 java 文件的同一个包中

标签: java eclipse jsp jakarta-ee tomcat7


【解决方案1】:

我不确定您是在 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();
    }
}
}

【讨论】:

  • 我修改了我的问题。正如我在回答 Ye Win 的回答时,我对如何使用 Servlet 不太了解。看来我应该使用GET接口来使用'request'变量,对吗?
  • 查看我修改后的答案
【解决方案2】:

试试这个:

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"));

如果您的文件位于 /WEB-INF/classes/BankingDataExample.dat 下

【讨论】:

  • 我修改了我的问题。我不太了解如何使用 Servlet,所以我不确定我可以将您的解决方案放在哪里。
  • 哦,我明白了。你能提供 SourceFilter.java 和 PFGeneralFilter.java 完整源吗??
  • 我添加了两个文件的完整代码。它将帮助您更好地理解我的代码。
【解决方案3】:

我们可以将要读取的文件直接放在主项目文件夹下。就像在下面一样。

【讨论】:

  • 我的程序还是找不到输入文件。
【解决方案4】:

不要把它当成一个文件,而是开始把它当作你需要的内容。将它放在源文件夹的根目录中(它现在可以用于 Web 浏览器)并调用 getClass().getClassLoader().getResourceAsStream(String)。然后,如果您也将 Web 应用程序打包为 .war,它将继续工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多