URLRewrite in Tomcat 7

I plan to develop an intranet application (Java client, JSP, SQLite)

The purpose of this is that when the user clicks on the link, if the user has access (for a command that is processed in business logic), the file must be provided for download

There is a table in db that contains the information, and below is an example of a line

ID | file | team | md5 1 | D:\test\output_20140915_100012.zip | Falcon | 5cceaf4cc400fd8f5c7fb4b2af754f4093369f59 

where MD5 is the MD% checksum of the line "D: \ test \ output.zip", and not the file itself

I create MD5 just for the sake of random number. The reason I don't use RAND is to avoid a collision (this part is trivial)

I want my url to look like this

 http://mywebserver:8080/<appname>/5cceaf4cc400fd8f5c7fb4b2af754f4093369f59 

What needs to be done for

 http://mywebserver:8080/<appname>/download.jsp?id=5cceaf4cc400fd8f5c7fb4b2af754f4093369f59 

I am using Tomcat and I am wondering how we can rewrite the URLs for this scenario

+4
source share
2 answers

I would recommend doing this using Apache HTTP Server or another server. But if you want to do this only with Tomcat 7, use the Tuckey UrlRewriteFilter :

Java Web Filter for any compatible web server application (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. This is a very powerful tool like Apache mod_rewrite.

This filter allows you to define rules such as:

Clear URL

 <rule> <from>/products/([0-9]+)</from> <to>/products/index.jsp?product_id=$1</to> </rule> 

e.g. / products / 1234 will be transferred to /products/index.jsp?product_id=1234 without notifying the user

from Example ukprprita Tuckey

With Tomcat 8, you can use a rewriting valve.

The rewriting valve implements the rewriting function of the URL in such a way that it is very similar to mod_rewrite from the Apache HTTP server.

Rewrite Valve docs

+8
source

For Tomcat 7 you can also use tuckey . Tucket is a Java web filter for any compatible web application server and allows mod_rewrite just like Apache

+2
source

All Articles