Solved

Create/ Edit / Delete Directory in JSP

Posted on 2002-04-04
16
405 Views
Last Modified: 2008-06-27
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
Comment
Question by:chtsang
  • 9
  • 6
16 Comments
 

Author Comment

by:chtsang
ID: 6918087
Urgent Please... ^_^
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918464
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918474
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:chtsang
ID: 6918607
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
 

Author Comment

by:chtsang
ID: 6918612
Thank you very much ^_^
Urgent also.....
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918617
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
 

Author Comment

by:chtsang
ID: 6918625
It is depends on the User side....
User can define the location of directory to be created .... in the server...
it is difficult?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918632
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
 

Author Comment

by:chtsang
ID: 6918643
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918716
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 6918717
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6918724
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
 

Author Comment

by:chtsang
ID: 6919786
Thank you so much ^_^
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6920777
no prob.. Glad I could help and thanx for the "A"
0
 

Expert Comment

by:nickl1
ID: 21901554
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 21913140
Only issue I could see with delete is perhaps permissions or it not being empty.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
getting Http 404 error in jsp forms 8 79
Fisheye tool 2 111
getting param value 7 53
sortaSum challenge java 17 171
Google always has something new and amazing up its sleeve, and the most current thing that they have been working on is another step in the evolution of Google Search, from machine learning to its brilliant successor, deep learning.
In this increasingly digital world, security hacks are no longer just a threat, but a reality. As we've witnessed with Target's big identity hack 2013, Heartbleed in 2015, and now Cloudbleed, companies and their leaders need to prepare for the unthi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question