[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

browse tomcat from client

project name: projectX
I wrote a struts program which has a jsp page and corresponding action page

in the jsp page i have a browse button which should browse a file from server project home directory...

(no need to browse everything on tomcat server atleast on the project directory (root-> ProjectX(pro name))

how can i browse server project home directory from jsp client page...
0
shragi
Asked:
shragi
  • 15
  • 13
  • 3
1 Solution
 
for_yanCommented:
Maybe I'm missing something, but
I think it should not be a problem - if you want to browse folders on the server from client
HTML page (or for that matter also jsp - you can still have HTML in jsp) -
you just put the link to your top directory where you want them to start and browser
will provide you with the browsing capabilities and you can also download files.

If you want to be much more fancy, you can also write an applet, and applet will
be allowed to connect to the server, once it will be loaded from the same server.
Though it would be some work, I guess.

Don't know if struts provide something on top of this, but browsing from HTML in general
does not need any special things.

Just make sure you don't have index.html inside these folders, where the client will browse
in such case it may open this page, instead of showing the folder.


0
 
shragiAuthor Commented:
@for_yan
can you provide small example with providing the link to my top directory...


my directory structure...
ProjectX is project main directory

ProjectX: src, build, WebContent, content
WebContent: JSP, Meta-Inf
content: source, old_source

the main files that customer may browse are in content directory
either in source folder or old_source folder



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
for_yanCommented:
What is the path  of your top directory you want people to see, relative to your root folder where you put your jsp ?
0
 
for_yanCommented:
I'll post a simple example foo you soon. The only thing is that if your top browsing folder is unrelated to your root folder you'll need to create a link in your document root
0
 
shragiAuthor Commented:
the top browsing folder should be my project folder...


ProjectX


0
 
for_yanCommented:
Well, while further experimenting trying to write an example for you
I discovered that Tomcat itself does not provide such browsing capability;
it is Apache web server which does this for me.
Maybe you have apache web server running somewhere, which can access the same folder
from its document root - then you just need to put such link  on your jsp or html page:

<a href="http://server.company.com:8100/projectX">click here</a>

and it will show you the list of files in the folder projectX - in this example assuming that this
folder is sitting immdeiately in the document root of your apache browser.

Perhaps Tomcat can also be configured to understand it, but
I didn't try. In my web applications, I anyway route static Html pages to
to apache.
Please, mind that this link itself may reside in your jsp and
the page containing this link itself may be served by Tomcat,
it is only that the link inside should contain reference to Apache
(like in my environement, port 8100 points to Apache,
port 8080 on the same erver points to Tomcat).






0
 
shragiAuthor Commented:
when I used the link It showed nothing it just launched the projectX

what I want is some thing like a browse button... which browse the folders in projectX

fodlers: jsp, meta-Inf

is it possible to have something like that....
0
 
for_yanCommented:

No if you put this link in your html page it will show you link "click here"
But most important question is - do you have Apache which can access the same folder from its doc root?

Button or link - this is detail, and it can be changed, but if you don't have apache (or maybe another web server)
then it will not do the browsing.

So first answer this question about web server.
0
 
shragiAuthor Commented:
I do have tomcat web server....

and when I used that link It showed "click here"

when I clicked the link it redirected to the project home page....


0
 
for_yanCommented:
Tomcat is strictly speaking not a web server - it is Java engine - it has
some functions of the web server, but not all, so it does not provide
this browing capability - maybe it can be configured, but if your server is
windows it is probably easier to install and run Apache in parallel
with Tomcat.

When you point the browser at some folder served by Apache,
then if inside the folder there exists file index.html
then browser will be directed to this file - and this
is how you get to the home page of your project.

If there inside the folder there is no file index.html
and if Apache is web server, then it will open this folder for browsing.
So if you have the right web server, you can rename your index.html
to, say projectX.html, then you'll
start your project as http://yourserver.yourcom.com:8080/projectX/projectX.html
and you'll start browing pointing the link to
 http://yourserver.yourcom.com:8080/projectX/

It is also not difficult to make it look like a button, rather than link.

So these things can be resolved. What remains important is to have a web server
which allows to do this browsing. And i don't know if it is possible
to do with tomcat without apache.









0
 
shragiAuthor Commented:
hmm i cannot install apache web browser what ever i can do i must do it using tomcat....

so i am able to list the folders in the server with the help of code given by @rrz but want to view all the files in those folders....

0
 
for_yanCommented:
apache is not a web browser, it is a web server

What is the operating system, where you are running tomcat?
0
 
for_yanCommented:
OK, I think I figured out with tomcat.

Go to your tomcat installation and then to folder conf

In that folder there should be a file called web.xml

Open this file in some simple text editor, say notepad.

Smewhere not far from the top you should see such text:

------------------------------------
<servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
---------------------------------------

Check if you have value "true" just below the param-name "listings".

Then let me know what you see - is it true or false - ?

Also let me know do you know how to restart your tomcat.


0
 
shragiAuthor Commented:
   <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

      <init-param>
         <param-name>config</param-name>
         <param-value>/WEB-INF/struts-config.xml</param-value>
      </init-param>

      <init-param>
         <param-name>debug</param-name>
         <param-value>3</param-value>
      </init-param>

      <init-param>
         <param-name>detail</param-name>
         <param-value>3</param-value>
      </init-param>

      <load-on-startup>0</load-on-startup>
   </servlet>
   
   <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
   </servlet-mapping>


I know how to restart the tomcat..
0
 
for_yanCommented:
No this is something about struts - I'm not interested in struts - I'm asking
about Tomcat itself - can you go to the Tomcat top level folder - then from that folder to cond and
there should be file web.xml (the one which is in TOMCAT_HOME/conf - or maybe CATALINA_HOME/conf)
and in that file I want section about DefaultServlet - this is all idependent of any struts.
0
 
for_yanCommented:
...from that folder to "conf" - of course - (not cond - there was a misprint above)
0
 
shragiAuthor Commented:
<init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>


this section was not there in my web.xml but I added that now...

0
 
for_yanCommented:
But you added it to DefaultServlet section - correct?
Now restart Tomcat.

Then go to your ProjectX folder and find there some file like index.html or index.jsp - something like that

Then rename that file to ProjectX.html or ProjectX.jsp - depending on which of them you find
(make sure that no users are  using your ProjectX right now).

Then go to browser and type in the address line:

http://yourserver.yourcomp.com/ProjectX

and let me know - do you see the listing of the files in ProjectX folder

0
 
for_yanCommented:

 You may probably want to have port for your tomcat, maybe like that,
http://yourserver.yourcomp.com:8080/ProjectX
or maybe your tomcat uses some other port
0
 
for_yanCommented:
If this works, then you'll be starting your project as

http://yourservre.yourcomp.com:8080/ProjectX/ProjectX.html
0
 
shragiAuthor Commented:
@for_yan

I did in the below way and it worked but few flaws can you correct them....

I have files in current_content and old_content folders.... but i am missing them...i mean they are not
displayed... only one level of things are displayed....


<select name ="loadFile">
	<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>



output: 

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

Open in new window

0
 
for_yanCommented:
Don't understand what you mean - it shows the files and folders - if you click on the folder it will go down into the folder
0
 
shragiAuthor Commented:
yup it is showing the server side files and folders... something like browse...

i thought instead of browse I thought to use this drop down list... of files and folders from server....

in this way i am getting almost all files and folders.... but missing few....

I have.. 3 folders which has sub folders and files...

content: current_content, old_content
jsp: index.jsp, index2.jsp
meta-inf: manifest.mf


in the subfolders current_content, old_content i have few more files which are not displayed using the above code
what changes should i make...
0
 
for_yanCommented:
do you have any guess why these files are nt shown?
It shows everything for me
0
 
shragiAuthor Commented:
yes I know where is the problem....  

File[] files = f.listFiles();
      out.print("<option value=\"" + name + "\">" + name + "</option>");


this code brings the folder names

content, jsp, meta-inf

for(File file:files){
      out.print("<option value=\"" + file.getName() + "\">" + file.getName() + "</option>");
      }


this code brings the subfolders in the folder...  here I think we should check again whether file.getname is a directory or not....

0
 
rrzCommented:
Here is a JSP that demonstrates what I think you want to do. It is kind of a hack but it does work. Just copy and paste into your web app.  One problem  is that if you browse to a JSP file then you will see its execution output not the actually code in the file.  
<%@ 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
 
shragiAuthor Commented:
hmm its working but it is showing complete path in the drop down list...

when i replaced

String path = f.getPath();
              out.print("<option value=\"" + path + "\">" + path + "</option>");


with

 String name = f.getName();
              out.print("<option value=\"" + name+ "\">" + name + "</option>");


i am getting just folder names... but ... when i click on browse button it shows errors can we
have just names instead of whole path...
0
 
rrzCommented:
Try this one.
<%@ 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();
              String name = f.getName();
              out.print("<option value=\"" + path + "\">" + name + "</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
 
shragiAuthor Commented:
that works... but i have a small concern... is it possible to do... it automatically instead of clicking browse folder button... because...

I am embeding this code in a form which already has an action...


I want this selected file location value in server...

how can i get that value


String file_location = (String)request.getAttribute("selectedFolder");

will it give me the path to the selected one from dropdown list...

0
 
shragiAuthor Commented:
please see the below code i just attached another submit button which calls an action class

when i click the browse button its showing an error

and I want to have the selected filepath in the loadFileName action class...
so that i can parse the file selected by customer...

so I called


String file_location = (String)request.getAttribute("selectedFolder");


but it gives me nothing...


 
<%@ 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 15
  • 13
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now