简体   繁体   English

在.jsp文件中显示.java的输出

[英]display output from .java in .jsp file

I have a java file that is connecting to an ftp server and bringing back a list of directories from the server. 我有一个Java文件,该文件正在连接到ftp服务器并从服务器带回目录列表。 My question is what is the proper method of grabbing the list from the java file and displaying the results of the directories in my jsp? 我的问题是从java文件中获取列表并在jsp中显示目录结果的正确方法是什么?

Would you suggest I connect to the ftp server straight from the jsp or is this bad coding practices? 您是否建议我直接从jsp连接到ftp服务器,或者这是不良的编码做法?

example: 例:

Connect.java 连接.java

package root;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class ConnectFTP {


private void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}



public void listFTPVendors(String[] args) {
    String server = "ftpserver.com";
    int port = 21;
    String user = "username";
    String pass = "password";
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        showServerReply(ftpClient);
        int replyCode = ftpClient.getReplyCode();C
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("Operation failed. Server reply code: " + replyCode);
            return;
        }
        boolean success = ftpClient.login(user, pass);
        showServerReply(ftpClient);
        if (!success) {
            System.out.println("Could not login to the server");
            return;
        } else {
            System.out.println("LOGGED IN SERVER");
        }
        FTPFile[] files = ftpClient.listDirectories();
        for (FTPFile file : files) {
            String details = file.getName();
            if (file.isDirectory()) {
                details = "[" + details + "]";
            }
            System.out.println(details);
        }

    } catch (IOException ex) {
        System.out.println("Oops! Something wrong happened");
        ex.printStackTrace();
    }


}

}

Connect.jsp Connect.jsp

?? ??

I'm merely looking for suggestions not necessarily code. 我只是在寻找建议,不一定是代码。 Maybe even some reading material. 甚至一些阅读材料。 Fairly new and lost... 相当新的和丢失的...

Instead of iterating through files and printing it, do return files and change the return type of the method listFTPVendors(String[] args) to FTPFile[] . return files而不是遍历files并进行打印,而是return files并将listFTPVendors(String[] args)方法的返回类型更改为FTPFile[]

Now, call this method from your JSP and retrieve data in JSP code. 现在,从您的JSP调用此方法并以JSP代码检索数据。

<%FTPFile[] files = new ConnectFTP().listFTPVendors()%>

Don't forget to import the class ConnectFTP in your JSP . 不要忘记在JSP导入ConnectFTP类。

Iterate through files in JSP and show it on webpage. 遍历JSP files并将其显示在网页上。

是的,JSP用于表示逻辑,因此如果将网络代码保存在单独的Java文件中会更好:)

I see that you are using the Apache Commons FTP api. 我看到您正在使用Apache Commons FTP API。 You are pretty much doing it correctly. 您几乎可以正确执行此操作。

I also believe that you are unnecessarily testing for a directory when your collection is only made up of directories. 我还相信,当您的集合仅由目录组成时,您不必要测试目录。 This is because you are using the listDirectories() method. 这是因为您正在使用listDirectories()方法。
If you called the listFiles() then you would have to test each file is a directory, as you have done. 如果您调用listFiles(),则必须像已经完成的那样测试每个文件是一个目录。

You should be able modify your code as follows: 您应该能够如下修改代码:

...
FTPFile[] files = ftpClient.listDirectories();
for (FTPFile file : files) {
    System.out.println("["+ file.getName() +"]");
}
...

As far as connecting to the FTP server directly from your JSP page - there is nothing 'wrong' there, but usually the jsp's are used for display purposes. 至于直接从您的JSP页面连接到FTP服务器-那里没有任何“错误”,但是通常jsp用于显示目的。

I would avoid hard coding the credentials in the page - they should be externalized into a config file. 我将避免对页面中的凭据进行硬编码-应该将它们外部化为配置文件。 The config file should also be stored in a location that cannot be accessed via the browser. 配置文件也应存储在无法通过浏览器访问的位置。

Depending on what web framework you are using - you may want to wrap this functionality into a controller or service. 根据您使用的Web框架-您可能希望将此功能包装到控制器或服务中。

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

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