Solved

extract path

Posted on 2011-02-15
10
442 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
Receive file in Servlet 1 36
wordappend challenge 8 85
Unable to start eclipse ? 17 84
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

705 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

19 Experts available now in Live!

Get 1:1 Help Now