简体   繁体   中英

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. 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?

Would you suggest I connect to the ftp server straight from the jsp or is this bad coding practices?

example:

Connect.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

??

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[] .

Now, call this method from your JSP and retrieve data in JSP code.

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

Don't forget to import the class ConnectFTP in your JSP .

Iterate through files in JSP and show it on webpage.

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

I see that you are using the 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.
If you called the listFiles() then you would have to test each file is a directory, as you have done.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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