Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Using Tomcat to view an existing html base slideshow

 
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a folder full of html files with relative links to html files in subfolders that link to images.  
It was set up for viewing pictures from various trips I have taken in a browser.  It's  a slideshow.

I would like to use a Tomcat server to make those images available to others via a connection to Tomcat running as a server on my PC.  The user would enter something like: http://192.168.1.32/Norms/SlideShow to get the top level list of html files that could be used to look at any of the images referred to by the lower level html files.

I tried to use a servlet to start the process, but have run into the need to modify the contents of the html files that are read so that the links will be sent to the servlet to be able to control and connect and return the files as needed.
It is not working well because the links from the html files do not refer to the servlet so tomcat returns a 404.
The only solution I can think of is to inject the servlet name with a query string in the HREF= fields.

Is there another way to have Tomcat read from an external folder without letting the user get outside the folder?

Thanks,
Norm
 
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what you mean by "top level list of html files" and "lower level html files". If your static html code works fine and has only relative paths, why can you simply copy them in your folder called "Norm" and access them directly? I dont think you a servlet for this.
I think a WEB-INF folder is needed with a web.xml in it to specify your welcome page (e.g. index.html).
 
Saloon Keeper
Posts: 21248
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you're going to do is serve up static content, I wouldn't use JEE (Tomcat) for that. I'd use a standard server like Apache, Nginx or IIS. Aside from the extra overhead required for JEE that you're not using, the standard servers are more secure when connected directly to the Internet.

On the other hand, if it must be Tomcat, you have 3 options:

1. Embed the HTML content as file within the WAR of the webapp that they will be served from.

2. Provide a servlet that uses the incoming URL to tell the servlet which static resource you want returned and have the servlet open and copy that static resource. For example, a URL of http://myserver:8080/mywebapp/myfiles/index.html might be translated by your custom servlet to open file /var/lib/mywebapp/html/index.html and copy its contents to the servlet output stream.

3. Do like as in #1, but make the static resource directorie(s) be a softlink(s) to external file directorie(s). This is not as secure as option 1 or 2 and will not work with default Tomcat setup. You have to explicitly tell Tomcat to follow softlinks.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
I'm looking at #2


2. Provide a servlet that uses the incoming URL to tell the servlet which static resource you want returned and have the servlet open and copy that static resource. For example, a URL of http://myserver:8080/mywebapp/myfiles/index.html might be translated by your custom servlet to open file /var/lib/mywebapp/html/index.html and copy its contents to the servlet output stream.


How does the servlet intercept that URL?  What is the name of the servlet and how does the tomcat give it control with that URL?
I have a servlet named snoop.  When I entered a URL with snoop in the path I get this:
HTTP Status 404 - /Norms/snoop/morestuff/aFile.html
 
Tim Holloway
Saloon Keeper
Posts: 21248
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to write your own resource-copy servlet. It's not hard.

Servlets are defined in web.xml (or via annotations) and given logical IDs. The web.xml file also maps one or more URL patterns to a servlet's logical ID. So for example, the webapp context path "/flanger/" might be mapped to a logical ID of FlangerServlet, and that migh be mapped to com.coderanch.ourwebapp.servlets.FlangerServlet.

Once this mapping is established, the FlangerServlet will receive all incoming URL requests for that webapp which start with "/flanger/". The FlangerServlet can then dissect the URL to determine which resource it wants to copy back to the client. You might, for example, pull apart a URL ending with "/flanger/index.html" to get a resource name of "index.hml" If you have your HTML pages in a directory named /var/lib/myservlet/html", then you could build a File object:

 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a servlet named snoop.  When I entered a URL with snoop in the path I get this:
HTTP Status 404 - /Norms/snoop/morestuff/aFile.html


How do I get tomcat to call my snoop servlet.  It gives a 404 for the above URL.
If I change the URL to /Norms/snoop?morestuff/aFile.htm
making the path a query string, the snoop servlet code is called.

From my web.xml file located at: D:\apache-tomcat-8.0.32\webapps\Norms\WEB-INF\web.xml
 

<servlet-mapping>
       <servlet-name>        snoop        </servlet-name>
       <url-pattern>        /snoop        </url-pattern>
   </servlet-mapping>

   <servlet>
       <servlet-name>        snoop        </servlet-name>
       <servlet-class>       SnoopServlet </servlet-class>
   </servlet>


Once this mapping is established, the FlangerServlet will receive all incoming URL requests for that webapp which start with "/flanger/"


How is that mapping made?  I thought I had it.  With the URL at the top of this post I get a 404.  Is the sub domain: /Norms/ the problem?

Here's my actual code that is a work in progress:


The web.xml


   <servlet>
     <servlet-name>          SlideShow        </servlet-name>
     <servlet-class>         Testing.SlideShow   </servlet-class>
       <init-param>
           <param-name>ssFolder</param-name>
           <param-value>E:/DigitalImages/</param-value>
       </init-param>
   </servlet>

   <servlet-mapping>
       <servlet-name>     SlideShow        </servlet-name>
       <url-pattern>      /SlideShow       </url-pattern>
   </servlet-mapping>



 
Tim Holloway
Saloon Keeper
Posts: 21248
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your URL pattern has to be "/snoop/*" to work the way you want. It's a wild-card expression.

Also, "Testing" is a bad package name. Package names should begin with a lower-case letter. Only class names begin with upper-case according to the recommended Java conventions.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

a wild-card expression.


Thanks for that.  One problem solved.  Now for finishing the URL/HREF injections.

I created the Testing folder about 11 years ago with many programs in it.  I haven't gotten around to converting them all yet.  Most are for one time standalone tests.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!