Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Create/ Edit / Delete Directory in JSP

Hi, All

Who know how to Create/ Edit / Delete Directory in JSP
on the server side machine.
Please give example with coding....

Urgent!!!

Thank You...
0
chtsang
Asked:
chtsang
  • 9
  • 6
1 Solution
 
chtsangAuthor Commented:
Urgent Please... ^_^
0
 
cheekycjCommented:
Just use the java.io.File API

To create a directory:
<%
// create File object
File myDir = new File("/full/path/to/directory");
// create directory
myDir.mkdir();
%>

To delete a directory:
<%
// create File object
File myDir = new File("/full/path/to/directory");
// check if it is a directory
boolean deleteSuccessful;
if (myDir.isDirectory())
   deleteSuccessful = myDir.delete();

if (deleteSuccessful)
   // display directory deleted
else
  // display directory delete unsucessful
%>
I am not sure what you mean by edit??
0
 
cheekycjCommented:
if edit means rename then do this:

<%
File myDir = new File("/full/path/to/directory");
// check if it is a directory
boolean renameSuccessful;
if (myDir.isDirectory()) {
  File newDir = new File("/full/path/to/newDirectoryname");
  renameSuccessful= myDir.renameTo(newDir);
}
if (renameSuccessful)
  // display directory renamed
else
 // display directory rename unsucessful
%>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chtsangAuthor Commented:
So, How Can I combine with "FORM" of HTML with above JSP use together?
That mean... Push a button to create a Directory with JSP...
Push a button to delete a Directory
Push a button to Rename a Directory ???
0
 
chtsangAuthor Commented:
Thank you very much ^_^
Urgent also.....
0
 
cheekycjCommented:
well.. is the directory always going to be created in the same directory on the server.. or will the user be allowed to specify where the directory will be created?
0
 
chtsangAuthor Commented:
It is depends on the User side....
User can define the location of directory to be created .... in the server...
it is difficult?
0
 
cheekycjCommented:
Well its easier if user can delete or create dirs only in one location.. if you want them to do it anywhere.. they either have to select the location or you have to verify the path.

CJ
0
 
chtsangAuthor Commented:
Well., I will specify a location only for them to access, and edit.
but how can I combine the Form of HTML together with JSP?
I am sorry... I am a very beginer in web-page development
Thank you ^_^
0
 
cheekycjCommented:
0
 
cheekycjCommented:
The html form would be like:
<form action="directoryHandler.jsp" method="post">
Directory Name (new or current): <input type="text" name="directoryname"><br>
Directory action: <input type="radio" name="directoryaction" value="create" checked> Create
<input type="radio" name="directoryaction" value="create"> Delete
<input type="radio" name="directoryaction" value="create"> Rename<br>
New Directory Name (if action is rename)<input type="text" name="directoryrename"><br>
<input type="submit" name="submit" value="Process">
</form>


directoryHandler.jsp will be like:
<%@ page import="java.io.File"%>
<%
// error messages (if any)
String errorMsg = null;
// boolean to track success (init to false)
boolean success = false;
try {
     // Base directory path
     String baseDirLocation = "/full/path/to/base/directory";
     String dirName = request.getParameter("directoryname");
     if (dirName == null || dirName.equals("")) {
          success = false;
          errorMsg = "No Directory Name provided by User";
     }
     else {
          // first check if base directory exists
          File baseDir = new File(baseDirLocation);
          if (baseDir.exists() && baseDir.isDirectory()) {
               String requestedAction = request.getParameter("directoryaction");
               String directoryName = request.getParameter("directoryname");
              if ("create".equals(requestedAction)) {
                    // create File object
                    File newDir = new File(baseDirLocation + "/" + directoryName);
                    // create directory
                    success = newDir.mkdir();
               }
               else if ("delete".equals(requestedAction)) {
                    // create File object
                    File currDir = new File(baseDirLocation + "/" + directoryName);
                    if (currDir.isDirectory()) {
                         success = currDir.delete();
                    }
                    else {
                         success = false;
                         errorMsg = "Directory does not exist:" + directoryName;
                    }
               }
               else if ("rename".equals(requestedAction)) {
                    // create File object
                    File currDir = new File(baseDirLocation + "/" + directoryName);
                    if (currDir.isDirectory()) {
                         String newDirectoryName = request.getParameter("directoryrename");
                         if (newDirectoryName == null || newDirectoryName.equals("")) {
                              success = false;
                              errorMsg = "No Rename Directory Name provided by User";
                         }
                         else {
                              File newDir = new File(baseDirLocation + "/" + newDirectoryName);
                              success= currDir.renameTo(newDir);
                         }
                    }
                    else {
                         success = false;
                         errorMsg = "Directory does not exist:" + directoryName;
                    }
               }
               else {
                 success = false;
                 errorMsg = "Invalid Requested Action of: " + requestedAction;
               }
          }
          else {
             success = false;
             errorMsg = "Base Directory doesn't exist or isn't valid for path:" + baseDirLocation;
          }
     }
} catch (Exception e) {
     success = false;
     errorMsg = "Exception occured: " + e.getMessage();
  }
if (!success && errorMsg == null) {
   errorMsg = "Not Available";
}
%>
<% if (success)  { %>
  Action successful!
<% } else { %>
  Action unsuccessful due to following error:<br>
  <b><%=errorMsg%></b>
<% } %>

0
 
cheekycjCommented:
the only var in directoryHandler.jsp you have to modify is baseDirectoryLocation.. to your local path.

Now this is for unix.. you have to change "/" to "\\" and add drive letters to the path to make this work for windows.

CJ
0
 
chtsangAuthor Commented:
Thank you so much ^_^
0
 
cheekycjCommented:
no prob.. Glad I could help and thanx for the "A"
0
 
nickl1Commented:
I think the form needed a couple minor corrections:

<form action="directoryHandler.jsp" method="post">
Directory Name (new or current): <input type="text" name="directoryname"><br>
Directory action: <input type="radio" name="directoryaction" value="create" checked> Create
<input type="radio" name="directoryaction" value="delete"> Delete
<input type="radio" name="directoryaction" value="rename"> Rename<br>
New Directory Name (if action is rename)<input type="text" name="directoryrename"><br>
<input type="submit" name="submit" value="Process">
</form>

Create and Rename seem to work, but does anyone see anything wrong with Delete? I can't seem to get it working. Thanks
0
 
cheekycjCommented:
Only issue I could see with delete is perhaps permissions or it not being empty.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now