Solved

Create/ Edit / Delete Directory in JSP

Posted on 2002-04-04
16
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today’s effective marketing is about coming down to the customers’ level and engaging in a whole new way. A text message is one of the most effective and influential ways that you can engage your customers. Here are eight ways that you can utilize t…
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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