Map different url to same web application in Tomcat - java

I am not clear on the following:
If we have a web application named: SomeWebApp under Tomcat's webapp directory, the url to access it is:
http://localhost:8080/SomeWebApp
My question is, is it possible to configure Tomcat so that other URLs would point to that web application?
E.g.
http://localhost:8080/ADifferentApp will also point to the SomeWebApp?
From the web.xml I think is not possible since it is about the url patterns when you are inside the SomeWebApp scope.
So what is the proper way to do it? If it is possible that is.

The approach I found to work best is to install Apache2 on the server and proxy all requests. Tomcat is surprisingly difficult to configure in other ways than intended. In my experience, Tomcat doesn't provide this functionality declaratively.

I'd rather recommend Nginx than Apache as proxy. I'm recently working on a project that incorporates tomcat and nginx works as proxy.
Once you've got nginx you can acctualy map as many url's to access the same web application as you want.

Yes,its possible to map different context path to single application edit conf/server.xml file
> **> <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp"
> > path="/SomeWebApp" />
> > <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp" path="/ADifferentApp "/>**
Access application with 2 URL's

Related

Deploy tomcat webapp with different web.xml

Is there a way of deploying a tomcat webapp with different web.xml (different name and context)?.
The problem:
I have a web.xml which i constantly have to modify (comment out and uncomment) stuff as i develop things - and this makes it a little annoying. Want i want to have is lets say two files:
web.xml
web-dev.xml
And I want my tomcat on my local machine to use web-dev.xml. Ofcouse for production release (i.e. Hosted server Tomcat will be using normal web.xml - web-dev.xml won't even be published). Its just for the development.
Any ideas where i can specify within tomcat to use web-dev.xml instead of web.xml ?
Thanks
Similarly to #HumbertoPinheiro, I think Maven profiles are a way to go.
Alternatively, this seems like a possible solution (Reference link : Specify a custom web.xml to an embedded tomcat):
Context webContext = tomcat.addWebapp("/yourContextPath", "/web/app/docroot/");
webContext.getServletContext().setAttribute(Globals.ALT_DD_ATTR, "/path/to/custom/web.xml");
Tested on Tomcat 7.0.42.

How to know if my application is running over a JBoss server?

I am developing a GWT application.
And I would like to know if I am running over a Jboss instance or over a Jetty instance.
This is because if I am running over Jetty it means that I am running the dev mode and I need to redirect to MyModule.html?codeserver... but if I am running over Jboss I need to redirect to MyModule.html
But I can't figure how can I know if I am running over Jetty or over Jboss.
You should not try to find out the webserver on which your application is running, in your application code. Instead you can have a discriminating parameter set in the context of your application with different values for different servers.
E.g.
For JBoss, the server.xml can contain a parameter in the context definition as follows:
<Context ...>
...
<Parameter name="applicationStage" value="prod" />
...
</Context>
and for Jetty, the same context parameter would go into its jetty.xml but the value as "devo".
If you just want to know wether you are running production or development mode, then try
GWT.isProdMode();
Only works client-side of course.
There's not really any simple way to determine what application server you're running on (to my knowledge). You could check some system properties to determine this.
JBoss:
jboss.server.name
Jetty:
jetty.home
Although I do not encourage having logic determined by your environment as Vikdor mentions in his answer. His suggestion would be a better approach.
In a JSP,
Server is <%= application.getServerInfo()%>
or use
Server is ${pageContext.servletContext.serverInfo}
in a Servlet,
String server = getServletContext().getServerInfo();

Spring security: using relative path

I have an application with name test.war.
Because of Apache installed on my server I have to use another port number for Tomcat applications so after deployment this application available at domain.com:8080/test/.
I decided to create a subdomain in order to remove that ugly 8080 from url, so I setted up the server like described here. So now test.domain.com reffers to domain.com:8080/test/.
Everything seems fine except of one problem - because my application is not ROOT.war and I am using spring:url function every url in the application is translated to /test/bla-bla. So I removed that function. But still have a problem with spring security because it still translates an urls relative to app name i.e. /test/bla-bla.
How can I solve that problem?
Thank you
UPD: I don't want to deploy it as a ROOT application because I have two or three such applications and I wanted to create a subdomain for each one of them
Spring Security doesn't "translate" URLs. In fact this isn't specific to Spring Security. You'll run into similar issues with any application which does redirects. If you want to hide the context paths of applications which are behind a proxy, then you need to rewrite the URLs at the proxy.
I'd recommend you read the Tomcat Generic Proxy Howto and the section on URL rewriting in particular, as it specifically addresses this issue.

Short url or alias for deployed application in tomcat 6

I have a web application project which is deployed in tomcat 6.
I can access my application using the url:
http://localhost:8082/MyApplication
I also wan't to be able to access this application by another url like:
http://localhost:8082/myapp
Is this possible ? if yes what alternatives do i have ?
Off course, I don't want to change the original name of the application('MyApplication').
Thanks,
Abhishek.
If you add the Context within server.xml it will work as you want. Give the path attribute you wish.
<Context docBase="MyApplication" path="/myapp" />
Though it works, this approach is not recommended by the Tomcat docs, since any changes to server.xml means restarting the server disturbing all the web apps.
But, on the flip side, the practice of keeping this in Catalina_Home/conf/Catalina/localhost/context.xml (which is recommended by the docs) has some unreliabilities as others have reported - when you redeploy the war you can lose the context.xml too
See Why-does-tomcat-replace-context-xml-on-redeploy and
Why does tomcat like deleting my context.xml file?

Multiple contexts pointing to a single webapp

Is there anyway of configuring tomcat to point more than one context at a webapp?
I need to point these two urls:
http://server.com/abc
http://server.com/def
to a webapp running under the context abc.
Tomcat does not allow direct configuring of multiple <Context> elements to point to the same path.
So your options are either deploying the same web app twice with different Context (Not great idea)
or create a webapp called def that has one custom servlet filter declared in the web.xml that re-writes all requests to abc.
If your requirement is for a production app, I would recommend having an Apache Web Server before the tomcat so that you can do this and more.

Categories