Tomcat servlet,将所有URL重定向到单个网页 [英] Tomcat servlet, redirect all urls to a single webpage

查看:464
本文介绍了Tomcat servlet,将所有URL重定向到单个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tomcat中部署了一个servlet' csvreports '。 csvreports中的' index.html '从 data / 文件夹中选择csv文件,并显示为html表格。

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

网址格式为

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

我正在解析中的网址index.html 获取csv文件名并从代码中的 /data/test.csv 中读取。

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

现在,网址更改为 localhost:8080 / csvreports / folder1 /?csv = test.csv,
localhost:8080 / csvreports / folder2 /?csv = test.csv 等。

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

folder1和folder2动态生成并假设文件夹已经存在于 tomcat / webapps / csvreports /

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

我需要对所有网址执行相同的 index.html 。我的想法是解析url以获取路径并最终使用路径来读取csv。

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.

我想如何将 localhost:8080 / csvreports / * /?csv = test.csv 重定向到单个网页。

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

如何实现这一点的任何其他想法将不胜感激。

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

推荐答案

将所有网址映射到同一页面



使用 index.jsp 而不是 index.html 。您可以直接重命名该文件,而无需更改任何内容。

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.

然后在您的web.xml中,您可以指定所有以 / csvreports 应该路由到 index.jsp

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>






提取路径的正则表达式



index.jsp 中,您可以在URL上使用带有两个捕获组的正则表达式。


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;

第一个。* 会告诉它匹配csvreports之前的任何字符。这样,无论您的主机名是什么,它都会匹配。例如,目前它是localhost:8080,但如果您将其部署到测试或生产服务器,或者如果其他人从另一台计算机上攻击您的Web服务器,则会有所不同。

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.

接下来,它匹配 csvreports ,这意味着在主机名之后,URL必须包含确切的字符串 csvreports

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, comes 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 ?.

然后,它会查找?csv =

最后,第二个捕获组(。*)用于捕获 = 之后的任何字符。

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

现在您已将所有正则表达式设置为与您的URL匹配,您可以调用 .exec()正则表达式上的方法,并将其传递给您的URL。

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);

最后,您可以从匹配中提取捕获的组 .exec()的调用返回的变量。

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];

您可以通过连接这两个匹配的组来获取您的路径。您可能还需要检查以确保目录末尾有 / 。例如,正则表达式应匹配 localhost:8080 / csvreports / folder1?csv = file.csv ,但在这种情况下 directories ='folder1' csvfilename = file.csv ,所以当你连接它们时,你会得到'folder1file.csv',当你想要的是'folder1 / file.csv'。因此,在连接它们之前,检查目录中的最后一个字符是否为 / ,如果没有,则放入 / 目录 csvfilename 之间。

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.

这是一个显示此正则表达式的简单示例。

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>

这篇关于Tomcat servlet,将所有URL重定向到单个网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆