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

x
?
Solved

extract path

Posted on 2011-02-15
10
Medium Priority
?
450 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 28

Accepted Solution

by:
rrz earned 2000 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 28

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

 

Author Comment

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

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 28

Assisted Solution

by:rrz
rrz earned 2000 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 28

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 28

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 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