Solved

Create/ Edit / Delete Directory in JSP

Posted on 2002-04-04
16
398 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
what is stuck threads? 3 96
report generation frameworks 1 58
equalIsNot  challenge 43 117
Books that can get me started on JAVA 2 56
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now