0

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

3 Answers 3

1

Yes, JSP is for presentation logic so it will be better if you keep your networking code in separate Java file :)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

8 Comments

how might that look with what i have?
Just the minor changes which I mentioned in answer.
What should args evaluate to then in the jsp?
You don't use the "args" parameter in your method... just remove it! It should be public void listFTPVendors() {...}
Exactly, args is not even used inside the method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.