javascript - Tomcat servlet, redirect all urls to a single webpage - Stack Overflow

I deployed a servlet 'csvreports' in tomcat. 'index.html' in csvreports picks csv f

I deployed a servlet 'csvreports' in tomcat. 'index.html' in csvreports picks csv file from data/ folder and displays as html table.

The url format is

localhost:8080/csvreports/?csv=test.csv

I am parsing the url in index.html to get csv file name and read from /data/test.csv in the code.

Now, the url is changed to like localhost:8080/csvreports/folder1/?csv=test.csv, localhost:8080/csvreports/folder2/?csv=test.csv etc.

folder1 and folder2 gets generated dynamically and assuming the folders are already present in tomcat/webapps/csvreports/

I need to execute the same index.html for all urls. My idea is to parse url to get the path and finally use the path to read csv.

I wanted to how to redirect localhost:8080/csvreports/*/?csv=test.csv to a single webpage.

Any other ideas on how to acplish this would be appreciated.

I deployed a servlet 'csvreports' in tomcat. 'index.html' in csvreports picks csv file from data/ folder and displays as html table.

The url format is

localhost:8080/csvreports/?csv=test.csv

I am parsing the url in index.html to get csv file name and read from /data/test.csv in the code.

Now, the url is changed to like localhost:8080/csvreports/folder1/?csv=test.csv, localhost:8080/csvreports/folder2/?csv=test.csv etc.

folder1 and folder2 gets generated dynamically and assuming the folders are already present in tomcat/webapps/csvreports/

I need to execute the same index.html for all urls. My idea is to parse url to get the path and finally use the path to read csv.

I wanted to how to redirect localhost:8080/csvreports/*/?csv=test.csv to a single webpage.

Any other ideas on how to acplish this would be appreciated.

Share Improve this question edited Nov 12, 2015 at 18:51 Altmish-E-Azam 1,5832 gold badges13 silver badges25 bronze badges asked Nov 12, 2015 at 18:04 androidbeginnerandroidbeginner 612 silver badges8 bronze badges 4
  • you are trying to get file path in index.html? What method are you using to get file path in index.html? I think you need to use server side programming to get file path e.g. php or jsp. – Altmish-E-Azam Commented Nov 12, 2015 at 18:12
  • Why don't you put the 'folder1' and 'folder2' part in the csv path variable, rather than the url itself? For example: localhost:8080/csvreports/?csv=folder1/test.csv – Andrew Mairose Commented Nov 12, 2015 at 18:18
  • @Altmish-E-Azam, I am using javascript code to get the file path and csv name. Here is the code ` function getCSVPath() { var url = window.location.href; alert(url); var csvfile = url.substring(url.lastIndexOf("=")+1,url.length); alert(url.split('/')); return 'data/'+csvfile; }` – androidbeginner Commented Nov 12, 2015 at 18:43
  • @AndrewMairose, I checked and unfortunately we can't modify as the url is being populated by other web application. [localhost:8080/csvreports/folder1/?csv=test.csv] is the format. – androidbeginner Commented Nov 12, 2015 at 18:46
Add a ment  | 

1 Answer 1

Reset to default 4

Mapping all URLs to same page

Use an index.jsp instead of an index.html. You could literally just rename the file, without changing any of the contents.

Then in your web.xml, you can specify that all URLs starting with /csvreports should be routed to your index.jsp.

<servlet>
    <servlet-name>index</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern>/csvreports/*</url-pattern>
</servlet-mapping>

Regex for extracting path

In your index.jsp, you could use a regex on your URL with two capturing groups.

var regex = /.*csvreports\/(.*)\?csv=(.*)/g;

The first .* will tell it to match any characters before csvreports. This way, it will match no matter what your host name is. For example, currently it is localhost:8080, but if you deploy it to a test or production server, or if someone else hits your web server from another machine, it will be something different.

Next, it matches csvreports, which means that after the host name, the URL must contain the exact string csvreports.

Next, it looks for a single slash /.

Then, es the first capturing group (.*). The parentheses indicate that it is a matching group. The .* tells it to match any character. So, it will match all characters between the first / in your URL and the question mark ?.

Then, it will look for ?csv=.

And finally, a second capture group (.*) is used to capture any characters after the =.


Now that you have the regex all set to match your URL, you can call the .exec() method on the regex, and pass it your URL.

var match = regex.exec(url);

Finally, you can extract the captured groups from the match variable that is returned from the call to .exec().

var directories = match[1];
var csvfilename = match[2];

You can get your path by concatenating those two matched groups. You may also want to check to ensure that there is a / at the end of your directories. For example, the regex should match localhost:8080/csvreports/folder1?csv=file.csv, but in this case directories='folder1' and csvfilename=file.csv, so when you concatenate them, you would get 'folder1file.csv', when what you want is 'folder1/file.csv'. So, before you concatenate them, check if the last character in directories is a /, and if not, put a / between directories and csvfilename.


Example

Here is a simple example to show this regex in action.

function getPathFromUrl() {
  var url = document.getElementById('url').value;

  var regex = /.*csvreports\/(.*)\?csv=(.*)/g;
  var match = regex.exec(url);
  
  var span = document.getElementById('path');
  
  
  var directories = match[1];
  var csvfilename = match[2];
  
  var path = directories;
  
  if (path[path.length-1] !== '/') {
    path += '/'
  }
  
  path += csvfilename;    
  
  span.innerHTML = path;
}
<input id="url" type="text" value="localhost:8080/csvreports/folder1/?csv=test.csv" size="50">
<button id="button" onclick="getPathFromUrl()">Get Path</button>
<br><br>
<label for="path"><strong>Path:</strong></label>
<span id="path"></span>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583059a4634367.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信