Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create/ Edit / Delete Directory in JSP

Posted on 2002-04-04
16
Medium Priority
?
416 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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