Solved

browse tomcat from client

Posted on 2011-02-14
31
372 Views
Last Modified: 2012-06-22
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
Comment
Question by:shragi
  • 15
  • 13
  • 3
31 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 34891417
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34891447
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
 

Author Comment

by:shragi
ID: 34891756
@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
 
LVL 47

Expert Comment

by:for_yan
ID: 34891795
What is the path  of your top directory you want people to see, relative to your root folder where you put your jsp ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34891884
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
 

Author Comment

by:shragi
ID: 34891897
the top browsing folder should be my project folder...


ProjectX


0
 
LVL 47

Expert Comment

by:for_yan
ID: 34892463
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
 

Author Comment

by:shragi
ID: 34892539
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34892584

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
 

Author Comment

by:shragi
ID: 34892655
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34892722
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
 

Author Comment

by:shragi
ID: 34892814
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34892848
apache is not a web browser, it is a web server

What is the operating system, where you are running tomcat?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34892943
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
 

Author Comment

by:shragi
ID: 34893997
   <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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 47

Expert Comment

by:for_yan
ID: 34894012
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34894015
...from that folder to "conf" - of course - (not cond - there was a misprint above)
0
 

Author Comment

by:shragi
ID: 34894082
<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
 
LVL 47

Expert Comment

by:for_yan
ID: 34894107
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34894113

 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
 
LVL 47

Expert Comment

by:for_yan
ID: 34894119
If this works, then you'll be starting your project as

http://yourservre.yourcomp.com:8080/ProjectX/ProjectX.html
0
 

Author Comment

by:shragi
ID: 34897328
@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
 
LVL 47

Expert Comment

by:for_yan
ID: 34897451
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
 

Author Comment

by:shragi
ID: 34897518
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34897726
do you have any guess why these files are nt shown?
It shows everything for me
0
 

Author Comment

by:shragi
ID: 34897791
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
 
LVL 27

Expert Comment

by:rrz
ID: 34899128
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
 

Author Comment

by:shragi
ID: 34899465
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
 
LVL 27

Accepted Solution

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

Author Comment

by:shragi
ID: 34899826
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
 

Author Comment

by:shragi
ID: 34900051
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordlen challenge 3 47
Systems talking to each other 5 110
backtracking recursion  code 19 40
.htaccess file settings 4 36
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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 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

21 Experts available now in Live!

Get 1:1 Help Now