Solved

get complete list

Posted on 2011-02-15
29
928 Views
Last Modified: 2012-05-11
when I run the below code I got below output

Content
current_content
old_content
jsp
index.jsp
index2.jsp
images
Meta-Inf
MANIFEST.MF




but current_content and old_content and images are folders which also contains files which are not displayed when i use the below code
<%@ page import="java.io.File" %>
<%
  String selectedFolder = request.getParameter("selectedFolder");
  if(selectedFolder == null || "Please make a selection".equals(selectedFolder))selectedFolder = "not selected";
%>
<html>
<body>
<form>
<select name ="selectedFolder%">
	<option value="second Please make a selection" >Please make a selection</option>
	<%
	String requestURL = request.getRequestURL().toString();
	String servletPath = request.getServletPath();
	String folderURL = requestURL.substring(0, requestURL.indexOf(servletPath));
	for(File f : list){
	if(f.isDirectory() && !f.getPath().endsWith("WEB-INF")){
	String name = f.getName();
	File[] files = f.listFiles();
	out.print("<option value=\"" + name + "\">" + name + "</option>");
	for(File file:files){
	out.print("<option value=\"" + file.getName() + "\">" + file.getName() + "</option>");
	}
	}
	}
	%>
</select>
<input type="submit" />
</form>
Selected folder is <%=selectedFolder%>
</body>
</html>

Open in new window

0
Comment
Question by:shragi
  • 15
  • 12
  • 2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34898643
That looks like a BAD idea, exposing your server-side like that. Why do it?
0
 

Author Comment

by:shragi
ID: 34898709
its a bad idea... but my customer saves files on the server and he should be able to load that files when ever he need...

and i forgot to add the below two lines in the code...

  File appFolder = new File(application.getRealPath("/"));
   File[] list = appFolder.listFiles();
 


0
 

Author Comment

by:shragi
ID: 34898772
I have a small question...

i have many clients and one server....

if a client tell to save file in his location machine path C://users//local           directory

then i send this path to server and server does operation and writes the values to
a txt file and stores that text file in the location selected by client...

this works well when client and server runs on same machine...
how about server on other machine where it has C://users//local directory  
then it will store in the server...

how to overcome such situation... to avoid that i am giving client an option
to save in the server itself....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34898899
>>this works well when client and server runs on same machine.

That should never be the case with a professional setup, so let's start from the POV of their being different machines:
there's no relation between the file systems on client and server and the server knows nothing about the client. Please try to reframe the question in the light of this
0
 
LVL 27

Expert Comment

by:rrz
ID: 34899471
Is this a continuation of
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_26820259.html  
?  
Anyway, here is a hack that kind of does what you want to do.  
<%@ 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>
<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();
              out.print("<option value=\"" + path + "\">" + path + "</option>");
       }
   } 
%>
</select>
<input type="submit" value="Browse folder" />
</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>
</html>

Open in new window

0
 

Author Comment

by:shragi
ID: 34899608
the problem is I had a form which needs to fill by customers...

after filling the form i am saving it in xml format and storing that file..


that's main task...


so before filling the form i am asking customers to select a location
to save the form... (server side...) for this i created a few folders on server side
to store all types of forms...  so customers may select any folder to
save their form...

now after selecting the destination folder... the customer needs to fill
the form... here i gave them an option to load an existing file (which they
had already created) which on loading automatically fills the form...
that reduces the burden to fill the form...

so to browse already saved forms.. (these are stored in server) i need to
show them the folders that having all the forms..

i am hiding all the jsp forms... and web-inf and meta-inf folders...

but with using above code i did not get complete list of folders....

i.e.,  

content : old_content, current_content

old_content: index.xml, fiel1.txt, file2.txt

with using above code i am able to see just

content, its subfolders old_content, current_content
but not index.xml, file1.txt or file2.txt
0
 

Author Comment

by:shragi
ID: 34899688
hmm here i am asking for an idea... is there any way that instead of showing server side folders to customer
can i do some thing else for the problem...


showing folders... is done by for_yan in another question....

so is there any way to solve this problem...

0
 
LVL 27

Expert Comment

by:rrz
ID: 34899823
>with using above code i am able to see just
>content, its subfolders old_content, current_content
>but not index.xml, file1.txt or file2.txt  
You can browse them. Just go back to the dropdown list a second or third time or as many times as necessary.
Anyway, maybe I'll find time to come up with another solution.  
0
 

Author Comment

by:shragi
ID: 34900391
@rrz

plz check the below code...

when i click the browse button it is calling the form action class... formData....  but i need to select the file and then call the action class...  


<%@ 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 action="/formsData" 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
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 34900454
If you just want to list all folders in your web app and pick one, then try this JSP. Just copy and paste.
<%@ 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);
               }
          }
  }
%>
<%
   String appPath = application.getRealPath("/");
   File appFolder = new File(appPath);
   folderList.clear();
   listFolders(appFolder);
   String selectedPath = request.getParameter("selectedFolder");
   if(selectedPath == null || "Please make a selection".equals(selectedPath))selectedPath = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFolder" >
  <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"/>
</form>
Selected folder path is <%=selectedPath%>
</body>
</html>

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 34900505
>when i click the browse button it is calling the form action class... formData....  but i need to select the file and then call the action class...
You should replace  
><form action="/formsData" method ="post">
with
<form method ="post">
0
 

Author Comment

by:shragi
ID: 34900873
I think the previous one is better than this... it shows all the files in the folder...

the below code is gud and i am able to select the files... i needed...but it is opening the files... when i selected...

it should just be selected and should not open the files...

and how to call from servlet... the value that it selected....
<%@ 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>
<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();
              out.print("<option value=\"" + path + "\">" + path + "</option>");
       }
   } 
%>
</select>
<input type="submit" value="Browse folder" />
</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>
</html>

Open in new window

0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 34901307
>it should just be selected and should not open the files...  
If you don't want to open files then take out the links.  
>and how to call from servlet... the value that it selected....  
You can set it in session-scoped object.  
Try the following JSP. It lists all folders and files in your web app(excluding WEB-INF folder). It sets the selected value into a session-scope object. In your servlet you can access with
String selectedValue = (String)session.getAttribute("selected");  
Maybe there is a better way to do it using Struts. I don't know Struts.
<%@ 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("selectedFolder");
   if(selectedPath == null || "Please make a selection".equals(selectedPath))selectedPath = "not selected";
     else session.setAttribute("selected", selectedPath);
%>
<html>
<body>
<form>
<select name="selectedFolder" >
  <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"/>
</form>
Selected folder path is ${selected}
</body>
</html>

Open in new window

0
 

Author Comment

by:shragi
ID: 34901524
String selectedValue = (String)session.getAttribute("selected");  


it returned null value...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 27

Expert Comment

by:rrz
ID: 34901633
>it returned null value...  
Where ? In your servlet ? Did you define session  ? In a JSP its an implicit object. But in a servlet  you need to define it. Also the client has to join session.
Your idea of passing in query string

<script language="JavaScript" type="text/javascript">
      function performAction() {
            document.forms[0].action = "formsData.po?method=<%=selectedPath%>";
            document.forms[0].submit();
      }      
</script>

is a good idea.
0
 

Author Comment

by:shragi
ID: 34901723
<script language="JavaScript" type="text/javascript">
      function performAction() {
            document.forms[0].action = "formsData.po?method=loadFileName";
            document.forms[0].submit();
      }
      
</script>


and in my servlet i defined session in the below way

HttpSession session = request.getSession(true);
filelocation = (String)session.getAttribute("selected");
System.out.print(filelocation);


output: null
0
 
LVL 27

Expert Comment

by:rrz
ID: 34901959
Let's go with your script idea of submitting to Struts.
>document.forms[0].action = "formsData.po?method=loadFileName";
Where is loadFIleName defined ?  That is not right. It would have to be a javascript variable.  
Please try it my way.  
document.forms[0].action = "formsData.po?method=<%=selectedPath%>";  
This way the selectedPath variable from server is written into the javascript. You can check that in the page source that is sent to the browser.  When the customer clicks the submit button the value will be sent back to the server.  
0
 

Author Comment

by:shragi
ID: 34902047
document.forms[0].action = "formsData.po?method=<%=selectedPath%>";  

I dont know about this...

but when I define loadFileName in my action class
its one of the method in my action class

loadFileName(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
                  throws Exception { }
If i declare in my way I can use same action class for many methods...
document.forms[0].action = "formsData.po?method=loadFileName";


0
 
LVL 27

Expert Comment

by:rrz
ID: 34902127
Maybe you could use
document.forms[0].action = "formsData.po?method=loadFileName&selectedPath=<%=selectedPath%>";
and in the loadFileName method in your action class  use
String selectedPath = request.getParameter("selectedPath");
0
 

Author Comment

by:shragi
ID: 34902619
I tried that one but no use

the value is still null

i think there should be another way to get path value in server other than using session variables...
0
 
LVL 27

Expert Comment

by:rrz
ID: 34902769
That last one should work. You are submitting the form  and making a new request. Putting selectedPath into the query string should unless Struts does something different. If using  
String selectedPath = request.getParameter("selectedPath");  
doesn't work, then try
String selectedPath = request.getAttribute("selectedPath");  
0
 
LVL 27

Expert Comment

by:rrz
ID: 34902777
Or
String selectedPath = (String)request.getAttribute("selectedPath");  
0
 

Author Comment

by:shragi
ID: 34902888
its not working but I have a small doubt...

If i select a file file2.txt in
content
 old_content
index.xml
 current_content
 fiel1.txt
 file2.txt

when i use
(String)request.getAttribute("selectedPath");  


will it give path to file2.txt or path to its folder...

C://tomcat6/webapps/projectx/content/current_content/file2.txt

or gives something else

0
 
LVL 27

Expert Comment

by:rrz
ID: 34902968
I don't why it's not working. You could try submitting to another servlet or another JSP.
document.forms[0].action = "testing.jsp?selectedPath=<%=selectedPath%>";  
then in testing.jsp  use
String selectedPath = request.getParameter("selectedPath");  
That should work. If you get that working, then that tell you that you are doing something wrong with Struts.
0
 
LVL 27

Expert Comment

by:rrz
ID: 34902990
>will it give path to file2.txt or path to its folder...  
The selectedPath variable is the complete path of file. You can use
File file = new File(selectedPath);
File folder = file.getParentFile();
String folderPath = folder.getPath();
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 34903365
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
 
LVL 27

Expert Comment

by:rrz
ID: 34907550
shragi,
I read your new question.  Hopefully a Struts expert will respond there.
You put the selectedPath variable in the query string and then submitted the form to server. So you should be looking for a request parameter and not a session-scoped object. You never put selectedPath  into session-scope.
Also you are using a deprecated method request.getRealPath.
0
 

Author Comment

by:shragi
ID: 34907734
when i replaced the performAction class with the below thing

function show_filename()
      {
      var filename = document.getElementById('selectedFile').value;
      alert(filename);
      }

for

(<select name="selectedFile" id ="selectedFile"> )

i got the exact path... that means jsp page is getting the correct path...

<You never put selectedPath  into session-scope.>
Yup I just tried with that option i tried request parameter no use

filelocation = (String)request.getParameter("selectedFile");


this is not exactly struts... its more of servlets....




0
 
LVL 27

Expert Comment

by:rrz
ID: 34907917
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now