Non-static webapp alias

醉酒当歌 提交于 2020-01-05 23:59:05

问题


I would like to know, if there is a clean way to have a request answered by a webapp whose name does not appear in the request url. In a way, the url should be an alias of the webapp. Also, the alias should not be static or fixed with this single webapp but rather I need to be able to change the webapp behind the alias easily for version increments. The things, that come to my mind are

  • build a "facade" webapp to redirect requests
  • rename the complete webapp

Both ideas don't lead to the desired result. I would like to have a more lightweight solution.

Is this possible?


回答1:


If you want to do it with tomcat means only, you can statically map your webapp's path in tomcat context.xml defining a context and map the request path ("path") to a webapp dir ("docBase").

Just add this to your <tomcatDir>/conf/context.xml:

<Context 
  path="" 
  docBase="/<pathToYourWebapp>/<yourApp>" 
/>

This has the side-effect that you can only have one webapp, of course, because every request und / is mapped to your webapp.

See http://tomcat.apache.org/tomcat-8.0-doc/config/context.html#Common_Attributes for further information.




回答2:


Thanks to the post from Alexander I found the right track but I used some additional work, which is more or less the facade webapp mentioned in point 1) from my original question. I will show my entire approach for anyone interested:

First, I have all my standard webapps deployed in /tomcat/webapps with one standard context.xml and autoDeploy mode on. To catch all other requests, that do not match one of the deployed contexts, I set up a new webapp which acts as a default webapp for not matched requests. Let's call it dispatcher-app.

To make this one work, I have to set it as ROOT. To deploy it under its original name, I located it under /tomcat/webapps2 and its associated context.xml in /tomcat/conf/Catalina/localhost/ROOT.xml. It's necessary to put it outside of /webapps to prevent double deployment in tomcat with the autoDeploy mode used for the other webapps. Additional info on that can be found here: http://tomcat.apache.org/tomcat-7.0-doc/config/context#Naming

The ROOT.xml looks like this:

<Context
    path=""
    docBase="/path/to/tomcat-base/webapps2/dispatcher-app"
    crossContext="true"
/>

In web.xml of the dispatcher-app make sure, it uses

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

The dispatcher-app inside looks for the original path that was requested by the user using uriInfo.getAbsolutePath(). It then matches the application from the url with applications that run in tomcat, known from a custom configuration file. If there is a match, the configuration file knows, to which application the forward should be made. For example, the user requests myserver/webapp-1/test?param=value, and is forwarded to myserver/webapp-1.1.2/test?param=value. The webapp to forward to can be updated in the configuration file manually if application version changes.

The actual forward should be made using

RequestDispatcher dispatcher=servletContext.getRequestDispatcher(redirectTo);
dispatcher.forward(request, response);

because the forward will not be visible to the user.

As I could not make that run (see Cross-context request forwarding in tomcat results in java.lang.ClassCastException: org.glassfish.jersey.message.internal.TracingLogger), I simply used a 307 redirect for now.

return Response.status(307).location(new URI(redirectTo)).build();

The drawback of this one is, the browser will show the redirect URL in the address bar.

UPDATE: The RequestDispatcher works now, just not for this special case/webapp mentioned above. So I prefer that over the redirect approach.

One thing to note is, that container-based authentication (e.g. realm) on the second webapp will not be effective (as we do the forward 'behind' tomcat facade) so the dispatcher-app needs an authentication method itself or authentication needs to be made with another mechanism, such as a RequestFilter.



来源:https://stackoverflow.com/questions/29401153/non-static-webapp-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!