首页 > url > URI重写添加不需要的上下文路径

URI重写添加不需要的上下文路径 (URI rewriting add unwanted context path)

问题

我有我的应用程序来查找资源

http://myhost/my-app/test/bat

它运作良好,现在有了这个新的要求

http://myhost/my-app/xxx/test/bat, 

我只是希望它重写为旧的,所以我创建了一个过滤器:

if(uri.endsWith("/xxx/test/bat")){
     uri=uri.replace("/xxx/test", "/test");
     request.getRequestDispatcher(uri).forward(req, res);
  }
 else
     chain.doFilter(req, res);

但是tomcat容器将url重写为:

http://myhost/my-app/my-app/test/bat

/ my-app是contextPath

为什么我有这个额外的我的应用程序?我哪里做错了?

解决方法

你是如何得到你的uri价值的?它的价值是什么?我怀疑它可能是“/ my-app / xxx / test / bat”。当您使用request.getRequestDispatcher时,如果路径以“/”开头,则它被解释为相对于当前上下文根。

问题

I have my app to find resource at

http://myhost/my-app/test/bat

and it works good, now have this a new request to

http://myhost/my-app/xxx/test/bat, 

I simply want it rewrited to the old one, so I creat a filter:

if(uri.endsWith("/xxx/test/bat")){
     uri=uri.replace("/xxx/test", "/test");
     request.getRequestDispatcher(uri).forward(req, res);
  }
 else
     chain.doFilter(req, res);

but the tomcat container rewrite the url to:

http://myhost/my-app/my-app/test/bat

/my-app is the contextPath

why I got this extra my-app? where did I go wrong?

解决方法

How are you getting your uri value? what is it's value? I suspect it might be "/my-app/xxx/test/bat". When you use request.getRequestDispatcher if the path begins with a "/" it is interpreted as relative to the current context root.

相似信息