Solved

extract path

Posted on 2011-02-15
10
448 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
[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
  • 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
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: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
 

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

Technology Partners: 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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