Solved

extract path

Posted on 2011-02-15
10
444 Views
Last Modified: 2012-05-11
it is a continuation to my previous question folder structure

content_folder
    -> old_folder
         ->file1.xml
         ->file2.xml
    -> new_folder
         ->file3.xml
         ->file4.xml

by using below code if i select...
file1.txt

the code opens the file instead how to select that file...

i need to know from server side in method loadFileName what file is selected....

I think
request.getAttribute("selectedFolder");

just tells me what folder is selected but not the file selected..

i want the file selected... so that i can parse that file.... on server side to get the values....



<%@ page import="java.io.File" %>
<%
   String appPath = application.getRealPath("/");
   int appPathLength = appPath.length();
   String folderPath = request.getParameter("selectedFolder");
   if(folderPath == null || "Please make a selection or go back to app root folder".equals(folderPath)){
         folderPath = appPath;
   }
   File folder = new File(folderPath);
   String requestURL = request.getRequestURL().toString();
   String servletPath = request.getServletPath();
   String appURL = requestURL.substring(0, requestURL.indexOf(servletPath));
%>
<html>
<body>
<form method ="post">
<select name="selectedFolder" >
          <option value="Please make a selection or go back to app root folder" >
                    Please make a selection or go back to app root folder
          </option>
<%
   File[] folderList = folder.listFiles();
   for(File f : folderList){
       if(f.isDirectory() && !f.getName().endsWith("WEB-INF")){
              String path = f.getPath();
              String name = f.getName();
              out.print("<option value=\"" + path + "\">" + name + "</option>");
       }
   } 
%>
</select>
<input type="submit" value="Browse folder" />
<input class="button" value="Submit" type="button" onclick="performAction()" />

</form>
Selected folder path is <%=folderPath%><br/>
<%
       File[] files = folder.listFiles();
       String foldersPath = folderPath.substring(appPathLength);
       foldersPath = foldersPath.replaceAll("[\\\\]","/");
       for(File file : files){
          if(!file.isDirectory()){
           out.print("<a href=\"" + appURL + "/" + foldersPath + "/" 
                     + file.getName() + "\">" + file.getName() + "</a><br/>");
          }
       }
%>
</body>
<script language="JavaScript" type="text/javascript">
	function performAction() {
		document.forms[0].action = "formsData.po?method=loadFileName";
		document.forms[0].submit();
	}
	
	
</script>
</html>

Open in new window

0
Comment
Question by:shragi
  • 6
  • 4
10 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 34901550
How about something like the following. It just lists all your files. Did you want a tree ?
<%@ page import="java.io.File,java.util.*" %>
<%!
  ArrayList<File> folderList = new ArrayList<File>();
  public void listFolders(File folder){
          File[] array = folder.listFiles();
          for(File f : array){
               if(f.isDirectory() && !f.getName().endsWith("WEB-INF")){
                    listFolders(f);
               }
               if(!f.isDirectory())folderList.add(f);
          }
  }
%>
<%
   String appPath = application.getRealPath("/");
   File appFolder = new File(appPath);
   folderList.clear();
   listFolders(appFolder);
   String selectedPath = request.getParameter("selectedFile");
   if(selectedPath == null || "Please make a selection".equals(selectedPath))selectedPath = "not selected";
%>
<html>
<body>
<form name="formsData" >
<select name="selectedFile" >
  <option value="Please make a selection" >Please make a selection</option>
<%
   
   for(File f : folderList){
              String path = f.getPath();
              String name = f.getName();
              out.print("<option value=\"" + path + "\">" + name + "</option>");
   } 
%>
</select>
<input type="submit" value="Select a file"/>
<input class="button" value="Submit" type="button" onclick="performAction()" />
</form>
Selected folder path is  <%=selectedPath%>
</body>
<script language="JavaScript" type="text/javascript">
	function performAction() {
		document.forms[0].action = "formsData.po?method=<%=selectedPath%>";
		document.forms[0].submit();
	}	
</script>
</html>

Open in new window

0
 

Author Comment

by:shragi
ID: 34901609
tree would be gud... becoz here ... the customer cannot distinguish between folder and file...

and as you suggested... in the other thread... session did not work...

it returned null value...
0
 
LVL 27

Expert Comment

by:rrz
ID: 34901673
>tree would be gud... becoz here ... the customer cannot distinguish between folder and file...  
Only files are listed. The problem is that they all strung together. If we go with a tree, then we have to decide how it should look.  
Do you want the customer to be able to select only files and not folders ?
0
 

Author Comment

by:shragi
ID: 34901729
yup they should select only files not folders..
0
 
LVL 27

Expert Comment

by:rrz
ID: 34901877
Try this demo JSP. Just copy and paste. If you like that look, then we change it to your code.
<%@ page import="java.io.File,java.util.*" %>
<%!
  ArrayList<File> folderList = new ArrayList<File>();
  public void listFolders(File folder){
          File[] array = folder.listFiles();
          for(File f : array){
               if(f.isDirectory() && !f.getName().endsWith("WEB-INF")){
                    folderList.add(f);
                    listFolders(f);
               }
               if(!f.isDirectory())folderList.add(f);
          }
  }
%>
<%
   String appPath = application.getRealPath("/");
   File appFolder = new File(appPath);
   folderList.clear();
   listFolders(appFolder);
   String selectedPath = request.getParameter("selectedFile");
   if(selectedPath == null || "Please make a selection".equals(selectedPath))selectedPath = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFile" >
  <option value="Please make a selection" >Please make a selection</option>
<%
   
   for(File f : folderList){
         String name = f.getName();
         if(f.isDirectory()){
              out.print("<option disabled=\"disabled\">" + name + "</option>");
         } else {
                   String path = f.getPath();
                   out.print("<option value=\"" + path + "\">-->  " + name + "</option>");
                }
   } 
%>
</select>
<input type="submit" value="Select a file"/>
</form>
Selected folder path is <%=selectedPath%>
</body>
</html>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shragi
ID: 34902022
yup that looks gud....
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 34903377
I found some time to improve my code. It will work better for concurrent users.
<%@ page import="java.io.File,java.util.*" %>
<%!
  public ArrayList listFiles(File folder,ArrayList list){
          File[] array = folder.listFiles();
          for(File f : array){
               if(f.isDirectory() && !f.getName().endsWith("WEB-INF")){
                    list.add(f);
                    listFiles(f,list);
               }
               if(!f.isDirectory())list.add(f);
          }
          return list;
  }
%>
<%
   String appPath = application.getRealPath("/");
   File appFolder = new File(appPath);
   ArrayList<File> fileList = new ArrayList<File>();
   fileList = listFiles(appFolder,fileList);
   String selectedPath = request.getParameter("selectedFile");
   if(selectedPath == null || "Please make a selection".equals(selectedPath))selectedPath = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFile" >
  <option value="Please make a selection" >Please make a selection</option>
<%
   for(File f : fileList){
         String name = f.getName();
         if(f.isDirectory()){
              out.print("<option disabled=\"disabled\">" + name + "</option>");
         } else {
                   String path = f.getPath();
                   out.print("<option value=\"" + path + "\">-->  " + name + "</option>");
                }
   } 
%>
</select>
<input type="submit" value="Select a file"/>
</form>
Selected folder path is <%=selectedPath%>
</body>
</html>

Open in new window

0
 

Author Comment

by:shragi
ID: 34907955
how about using hidden fields in jsp and
calling them in servlet using

form.getparameter(hiddenfield)

but i dont know how to use hidden fields...

can u help me with that...

how to create hidden field in jsp and how to call them in servlet
0
 
LVL 27

Expert Comment

by:rrz
ID: 34909505
Good idea! You may have found our problem. We were appending to query string and then using the post method to send the form. I am sorry that I didn't notice that before now. I responded to your other question and gave you  the javascript code there.
By the way the getParameter method returns a String so no casting is necessary.
>String selectedvalue = (String)request.getParameter("hidden");
you can use

String selectedvalue = request.getParameter("hidden");
0
 
LVL 27

Expert Comment

by:rrz
ID: 34909707
Thanks for the points. Sorry I didn't see that problem earlier. I was so focused on the JSP that I didn't think about the HTML or javascript.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

937 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

15 Experts available now in Live!

Get 1:1 Help Now