Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

get complete list

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That looks like a BAD idea, exposing your server-side like that. Why do it?
Avatar of shragi

ASKER

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();
 


Avatar of shragi

ASKER

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....
>>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
Is this a continuation of
https://www.experts-exchange.com/questions/26820259/browse-folder.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

Avatar of shragi

ASKER

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
Avatar of shragi

ASKER

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

>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.  
Avatar of shragi

ASKER

@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

SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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">
Avatar of shragi

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

String selectedValue = (String)session.getAttribute("selected");  


it returned null value...
>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.
Avatar of shragi

ASKER

<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
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.  
Avatar of shragi

ASKER

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";


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");
Avatar of shragi

ASKER

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...
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");  
Or
String selectedPath = (String)request.getAttribute("selectedPath");  
Avatar of shragi

ASKER

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

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.
>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();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of shragi

ASKER

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