You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christian <cm...@famiru.de> on 2015/06/28 18:01:30 UTC

Distinct servlets for different domains in one web application

Hello all,

is it somehow possible to create a web application with multiple servlets that are registered to different domains for
the same url paths using tomcat 8?
I already crawled through the catalina code that is responsible for the servlet selection and didn't find anything that
would allow this. But this doesn't mean that it isn't possible at all.
As far as I know, java configuration for servlet registration doesn't allow passing domain names. But maybe there is an
option within context.xml.

I want to create a web application that has different domains for the application itself and its management site. Both
parts should run at the context root, at different domains. The application needs a shared (spring-)context in which the
application's beans are stored.

Regards,
Christian

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christian <cm...@famiru.de>.
Chris,

thanks for your brief description of the different options.

On 30.06.2015 19:38, Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Christian,
>
> On 6/28/15 12:01 PM, Christian wrote:
>> is it somehow possible to create a web application with multiple
>> servlets that are registered to different domains for the same url
>> paths using tomcat 8?
> Yes, but it has nothing to do with Tomcat: you have to do the work
> yourself.
>
>> I already crawled through the catalina code that is responsible
>> for the servlet selection and didn't find anything that would allow
>> this. But this doesn't mean that it isn't possible at all.
> Right: this isn't something that the spec requires, and it's usually
> not something that the server would get involved with. But, as I said,
> you can do it yourself; you have all the tools you need.
>
>> As far as I know, java configuration for servlet registration
>> doesn't allow passing domain names. But maybe there is an option
>> within context.xml.
> Nope.
>
>> I want to create a web application that has different domains for
>> the application itself and its management site. Both parts should
>> run at the context root, at different domains. The application
>> needs a shared (spring-)context in which the application's beans
>> are stored.
> If you want to support more than one (DNS) domain, you'll need to make
> sure that your application is deployed into a <Host> that is either
> the default <Host>, or one that matches all of the domains you want to
> use. Fortunately, Tomcat comes pre-configured with a single, default
> <Host> that does just that. You have nothing to do but drop your WAR
> file into webapps/ and let it auto-deploy.
>
> That's where Tomcat pretty much stops being involved, and you get to
> do the rest of the work.
>
> You mentioned that you had two servlets, and you want one to handle
> everything to one domain, and the other servlet handles the other
> stuff. For the sake of argument, I'll call those domains
> management.app.com and everythingelse.app.com.
>
> Let's assume you have these two servlets defined in web.xml:
>
>    <servlet>
>      <servlet-name>ApplicationServlet</servlet-name>
>      <servlet-class>com.app.ApplicationServlet</servlet-class>
>    </servlet>
>    <servlet>
>      <servlet-name>ManagementServlet</servlet-name>
>      <servlet-class>com.app.ManagementServlet</servlet-class>
>    </servlet>
>
> ... and you have them mapped:
>
>    <servlet-mapping>
>      <servlet-name>ApplicationServlet</servlet-name>
>      <url-pattern>/app</url-pattern>
>    </servlet-mapping>
>
>    <servlet-mapping>
>      <servlet-name>ManagementServlet</servlet-name>
>      <url-pattern>/manage</url-pattern>
>    </servlet-mapping>
>
> If you want, I suppose you could map the ApplicationServlet to /. It
> doesn't really matter.
>
> At this point, anyone visiting your application could hit either of
> the two mapped URL patterns to get to either servlet.
>
> Let's assume that all you want to do is make sure that the management
> servlet is only available when you use the management.app.com domain
> name. Write a Filter that detects the domain name and throws you out
> if it's not "management.app.com". Map this filter to "/manage" and you
> are done.

That's something I already do (with little variation). The missing step 
is just to beautify the URLs.

> Or, if you'd like for any request to any URL to go to either /app or
> /manage, then you'll want to write a Filter that does something like thi
> s:
>
>    public void doFilter(...)
>    {
>        String domain = // ...
>        if("".equals(domain))
>          application.getNamedDispatcher("ManagementServlet")
>                     .forward(request, response);
>        else
>          application.getNamedDispatcher("ApplicationServlet")
>                     .forward(request, response);
>    }
>
> Map this Filter to "/" and let it handle everything. Make sure you
> don't map it to FORWARD requests, or you'll end up with infinite
> recursion.
>
> You'll need to grab the ServletContext in the Filter's init() method
> and keep it around (as "application").
>
> At this point, you can even remove the <servlet-mapping> elements from
> your web.xml: users will have to access the application through your
> Filter no matter what.

I already tried an approach with a filter using getRequestDispatcher but 
ran into issues with spring-security. One needs to hook up 
forward-requests for the security filter. My conclusion of this approach 
was, that it works, but a second roundtrip through tomcat is an 
overkill, especially with the ugly configuration issues that arise 
within spring-security.
I need to find out if it behaves the same way with getNamedDispatcher.

> Hope that helps,

Yes it does - at least because you confirmed my assumption that there is 
no way to map a servlet to a domain name and tomcat doesn't provide a 
proprietary way to do so.

Thankful regards,

Christian


> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
>
> iQIcBAEBCAAGBQJVktP7AAoJEBzwKT+lPKRYEtAP/0PlUEpG+4A7d7LzDkHsFi/N
> 6Hsqayn8aBL6pSXMuCBhDgmzj6JnxZ2ZrzevhksYxLLtlyhJjrhTE0VdwtASz4nH
> mepVKvbaRJZ3KCeupDlTLI+YF9BRlPvUEwSuq2FW2kzH4eu7XFJ2GmoZ0HQ2xPqy
> MxQ/fkLb4HZKxnF7QVAa3FrnzuINQteEp6LCOxV7ONQMU2/s/iO/JuY/LOqTHeUQ
> 5M2mIIZPtdJa26QDEoAjn+dxaWU00Cl/whobrPOX/On7wVt9DZOxic15u7jWqixV
> XukZe4HqFI5RQop3PtODD+BzoUY02evs15TVl7tqLkmxQreH5ZBd5Q7BEfX7I69S
> KP/AQYG5cSEenNrgApVe/ikFg4C3A0rn60dHviO+8grn5SDgLXQC/50+16Tjv6ix
> 3L1qBgyD2QD4DKj9Piv/GGZqrxJoi+ubaOtTn0keWBeJ3v+gYRxMxHJ8Gww3kSW5
> NpGTSfl0mbQJCGXeBG9wPLyPRsXLLz1x3uW8z+YFr+GBYpEezEwHvRhDum/VEWd9
> UeavOdcJlj2805ZUdMi6JX4FiRKX8TO6rR8LNCes/OhzbUorj19AvKSvAKh/+14J
> hnvnyN3IcHjwl1oPOeSSH+Y0hxYkP3qolFGA8MRAJ2VVFDuP51DNqHNz0MySn5NS
> 9UyWgIqFzKXO/s/tiiPZ
> =tNIx
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Christian,

On 6/28/15 12:01 PM, Christian wrote:
> is it somehow possible to create a web application with multiple 
> servlets that are registered to different domains for the same url 
> paths using tomcat 8?

Yes, but it has nothing to do with Tomcat: you have to do the work
yourself.

> I already crawled through the catalina code that is responsible
> for the servlet selection and didn't find anything that would allow
> this. But this doesn't mean that it isn't possible at all.

Right: this isn't something that the spec requires, and it's usually
not something that the server would get involved with. But, as I said,
you can do it yourself; you have all the tools you need.

> As far as I know, java configuration for servlet registration
> doesn't allow passing domain names. But maybe there is an option
> within context.xml.

Nope.

> I want to create a web application that has different domains for
> the application itself and its management site. Both parts should
> run at the context root, at different domains. The application
> needs a shared (spring-)context in which the application's beans
> are stored.

If you want to support more than one (DNS) domain, you'll need to make
sure that your application is deployed into a <Host> that is either
the default <Host>, or one that matches all of the domains you want to
use. Fortunately, Tomcat comes pre-configured with a single, default
<Host> that does just that. You have nothing to do but drop your WAR
file into webapps/ and let it auto-deploy.

That's where Tomcat pretty much stops being involved, and you get to
do the rest of the work.

You mentioned that you had two servlets, and you want one to handle
everything to one domain, and the other servlet handles the other
stuff. For the sake of argument, I'll call those domains
management.app.com and everythingelse.app.com.

Let's assume you have these two servlets defined in web.xml:

  <servlet>
    <servlet-name>ApplicationServlet</servlet-name>
    <servlet-class>com.app.ApplicationServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ManagementServlet</servlet-name>
    <servlet-class>com.app.ManagementServlet</servlet-class>
  </servlet>

... and you have them mapped:

  <servlet-mapping>
    <servlet-name>ApplicationServlet</servlet-name>
    <url-pattern>/app</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>ManagementServlet</servlet-name>
    <url-pattern>/manage</url-pattern>
  </servlet-mapping>

If you want, I suppose you could map the ApplicationServlet to /. It
doesn't really matter.

At this point, anyone visiting your application could hit either of
the two mapped URL patterns to get to either servlet.

Let's assume that all you want to do is make sure that the management
servlet is only available when you use the management.app.com domain
name. Write a Filter that detects the domain name and throws you out
if it's not "management.app.com". Map this filter to "/manage" and you
are done.

Or, if you'd like for any request to any URL to go to either /app or
/manage, then you'll want to write a Filter that does something like thi
s:

  public void doFilter(...)
  {
      String domain = // ...
      if("".equals(domain))
        application.getNamedDispatcher("ManagementServlet")
                   .forward(request, response);
      else
        application.getNamedDispatcher("ApplicationServlet")
                   .forward(request, response);
  }

Map this Filter to "/" and let it handle everything. Make sure you
don't map it to FORWARD requests, or you'll end up with infinite
recursion.

You'll need to grab the ServletContext in the Filter's init() method
and keep it around (as "application").

At this point, you can even remove the <servlet-mapping> elements from
your web.xml: users will have to access the application through your
Filter no matter what.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJVktP7AAoJEBzwKT+lPKRYEtAP/0PlUEpG+4A7d7LzDkHsFi/N
6Hsqayn8aBL6pSXMuCBhDgmzj6JnxZ2ZrzevhksYxLLtlyhJjrhTE0VdwtASz4nH
mepVKvbaRJZ3KCeupDlTLI+YF9BRlPvUEwSuq2FW2kzH4eu7XFJ2GmoZ0HQ2xPqy
MxQ/fkLb4HZKxnF7QVAa3FrnzuINQteEp6LCOxV7ONQMU2/s/iO/JuY/LOqTHeUQ
5M2mIIZPtdJa26QDEoAjn+dxaWU00Cl/whobrPOX/On7wVt9DZOxic15u7jWqixV
XukZe4HqFI5RQop3PtODD+BzoUY02evs15TVl7tqLkmxQreH5ZBd5Q7BEfX7I69S
KP/AQYG5cSEenNrgApVe/ikFg4C3A0rn60dHviO+8grn5SDgLXQC/50+16Tjv6ix
3L1qBgyD2QD4DKj9Piv/GGZqrxJoi+ubaOtTn0keWBeJ3v+gYRxMxHJ8Gww3kSW5
NpGTSfl0mbQJCGXeBG9wPLyPRsXLLz1x3uW8z+YFr+GBYpEezEwHvRhDum/VEWd9
UeavOdcJlj2805ZUdMi6JX4FiRKX8TO6rR8LNCes/OhzbUorj19AvKSvAKh/+14J
hnvnyN3IcHjwl1oPOeSSH+Y0hxYkP3qolFGA8MRAJ2VVFDuP51DNqHNz0MySn5NS
9UyWgIqFzKXO/s/tiiPZ
=tNIx
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christian <cm...@famiru.de>.
Chuck,

On 28.06.2015 18:40, Caldarale, Charles R wrote:
> It sounds like you want virtual hosts:

somehow, but I want to deploy *one* war with two servlets and shared objects. Using virtual hosts allows to deploy the
application twice, but then the spring context isn't shared.

Regards,
Christian

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Distinct servlets for different domains in one web application

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christian [mailto:cmltc@famiru.de] 
> Subject: Distinct servlets for different domains in one web application

> is it somehow possible to create a web application with multiple servlets that are 
> registered to different domains for the same url paths using tomcat 8?

It sounds like you want virtual hosts:
http://tomcat.apache.org/tomcat-8.0-doc/virtual-hosting-howto.html

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christian <cm...@famiru.de>.
André,

On 29.06.2015 12:15, André Warnier wrote:
> Christian wrote:
>> André,
>>
>> On 28.06.2015 23:11, André Warnier wrote:
>>> Christian wrote:
>>>> Mark,
>>>>
>>>> On 28.06.2015 19:58, Mark Eggers wrote:
>>>>> -----BEGIN PGP SIGNED MESSAGE-----
>>>>> Hash: SHA1
>>>>>
>>>>> Christian,
>>>>>
>>>>> On 6/28/2015 9:01 AM, Christian wrote:
>>>>>> Hello all,
>>>>>>
>>>>>> is it somehow possible to create a web application with multiple
>>>>>> servlets that are registered to different domains for the same url
>>>>>> paths using tomcat 8? I already crawled through the catalina code
>>>>>> that is responsible for the servlet selection and didn't find
>>>>>> anything that would allow this. But this doesn't mean that it
>>>>>> isn't possible at all. As far as I know, java configuration for
>>>>>> servlet registration doesn't allow passing domain names. But maybe
>>>>>> there is an option within context.xml.
>>>>>>
>>>>>> I want to create a web application that has different domains for
>>>>>> the application itself and its management site. Both parts should
>>>>>> run at the context root, at different domains. The application
>>>>>> needs a shared (spring-)context in which the application's beans
>>>>>> are stored.
>>>>>>
>>>>>> Regards, Christian
>>>>> I'm not quite sure what your after. Sharing Spring beans across
>>>>> contexts and domains sounds like there's an underlying requirement I
>>>>> don't understand.
>>>>
>>>> ok, I'll try to explain, as aliases won't do what I need.
>>>> It's an application that can be configured via an admin interface. 
>>>> The admin interface is one part of the webapp.
>>>> The main application is accessible via www.example.com. The admin 
>>>> interface needs access to the beans within the main application (e. 
>>>> g. to create tenants that live in context scope), that's why I 
>>>> created just one webapp with two dispatcher servlets.
>>>> One dispatcher servlet is responsible for the admin interface and 
>>>> one for the main application. Until now I make tomcat select them 
>>>> by using a context path for the admin interface.
>>>> But this is redundant, because the admin interface needs to be 
>>>> accessed via admin.example.com/admin instead of just 
>>>> admin.example.com. I would like to map admin.example.com to the 
>>>> admin servlet and www.example.com to the main applications 
>>>> dispatcher servlet.
>>>> At a later stage, www.example.com/admin might be used for other 
>>>> admin tasks that must be accessible by users registered on the main 
>>>> site. But /admin is already shadowed.
>>>>
>>>> Hopefully I could explain my problem a little better.
>>>>
>>> Yes, that is much clearer.
>>> Honestly without having really figured out the details of your 
>>> explanation, I would nevertheless point you in 2 directions for that 
>>> kind of thing :
>>> a) use an Apache httpd front-end proxy, and use the httpd proxying 
>>> and/or URL rewriting capabilities to do what you want
>>
>> thanks for your suggestion! The application anyway needs a reverse 
>> proxy / ssl gateway, because tomcat isn't capable of doing TLS with 
>> SNI. Your proposed solution seems appropriate, although I have to 
>> figure out how to cleanly generate the correct urls within the 
>> application.
>
> <VirtualHost *:80>
>   ServerName admin.mydomain.com
> ..
>   ProxyPass / ajp://mytomcat.local/admin/
>   ProxyPassReverse / ajp://mytomcat.local/admin/
>   ProxyPassReverseCookieDomain "/admin" "/"
> </VirtualHost>
>
> <VirtualHost *:80>
>   ServerName www.mydomain.com
> ..
>   ProxyPass / ajp://mytomcat.local/
>   ProxyPassReverse / ajp://mytomcat.local/
> </VirtualHost>
>

thank you for the configuration excerpt!

> The ProxyPassReverse* take care of re-directs and cookies.
> But in your "admin" application pages, you should make sure that your 
> are returning only relative URLs.
> E.g. if the admin application would normally return a page with
> <img src="/admin/images/logo.jpg" />
> it should instead return
> <img src="images/logo.jpg" />
> and the browser and the proxy should then do the right thing.
>
> Or else, you could add an output filter at the httpd level, and 
> rewrite all the URLs on the fly.
> That is a bit resource-instensive, but I would imagine that your admin 
> application is not one with very high traffic.

I believe relative urls are most elegant. My second choice would be a 
configuration within the application itself when generating the links 
(it's using spring and version 4.2 gets support for base uris). A 
content filter at httpd level seems a bit brittle to me.

> Note also : in the schema above, there is nothing which prevents a 
> user on "www.mydomain.com" to request a URL like "/admin", which could 
> lead to accidents..
> You may want to add a RewriteCond/RewriteRule in that VirtualHost 
> which prevents them doing that.

Thank you for the hint - I already prevent malicious access at the 
application level. The setup is a bit more complex than I described 
here, because it is a multi tenant and multi domain application. Tenants 
get recognized by the domain within a servlet filter, that's why they 
play a big role already. At a spring security layer authorization gets 
checked for every request.

Regards,
Christian


>> Regards,
>> Christian
>>
>>> or
>>> b) use the built-in Rewrite Valve of Tomcat 8 
>>> (http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html), or if you 
>>> are at a lower Tomcat version, use the add-on URLRewrite Filter 
>>> (www.tuckey.org)
>>>
>>> A Valve is a Tomcat-specific component, so that solution is 
>>> generally non-portable to another servlet container.  The others 
>>> (httpd front-end or the URLRewriteFilter (a servlet filter)) are 
>>> portable.
>>>
>>> Personally, in this case, and considering that you want to do 
>>> different things depending on the hostname used to access Tomcat, I 
>>> would go the httpd front-end route, with 2 VirtualHosts at the httpd 
>>> level, proxying to a single Host at the Tomcat level (but in one 
>>> case, after modifying the URL).
>>> That's because I think that trying to do this at the Tomcat level 
>>> only may give you headaches in terms of self-referential URLs 
>>> returned by your application.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by André Warnier <aw...@ice-sa.com>.
Christian wrote:
> André,
> 
> On 28.06.2015 23:11, André Warnier wrote:
>> Christian wrote:
>>> Mark,
>>>
>>> On 28.06.2015 19:58, Mark Eggers wrote:
>>>> -----BEGIN PGP SIGNED MESSAGE-----
>>>> Hash: SHA1
>>>>
>>>> Christian,
>>>>
>>>> On 6/28/2015 9:01 AM, Christian wrote:
>>>>> Hello all,
>>>>>
>>>>> is it somehow possible to create a web application with multiple
>>>>> servlets that are registered to different domains for the same url
>>>>> paths using tomcat 8? I already crawled through the catalina code
>>>>> that is responsible for the servlet selection and didn't find
>>>>> anything that would allow this. But this doesn't mean that it
>>>>> isn't possible at all. As far as I know, java configuration for
>>>>> servlet registration doesn't allow passing domain names. But maybe
>>>>> there is an option within context.xml.
>>>>>
>>>>> I want to create a web application that has different domains for
>>>>> the application itself and its management site. Both parts should
>>>>> run at the context root, at different domains. The application
>>>>> needs a shared (spring-)context in which the application's beans
>>>>> are stored.
>>>>>
>>>>> Regards, Christian
>>>> I'm not quite sure what your after. Sharing Spring beans across
>>>> contexts and domains sounds like there's an underlying requirement I
>>>> don't understand.
>>>
>>> ok, I'll try to explain, as aliases won't do what I need.
>>> It's an application that can be configured via an admin interface. 
>>> The admin interface is one part of the webapp.
>>> The main application is accessible via www.example.com. The admin 
>>> interface needs access to the beans within the main application (e. 
>>> g. to create tenants that live in context scope), that's why I 
>>> created just one webapp with two dispatcher servlets.
>>> One dispatcher servlet is responsible for the admin interface and one 
>>> for the main application. Until now I make tomcat select them by 
>>> using a context path for the admin interface.
>>> But this is redundant, because the admin interface needs to be 
>>> accessed via admin.example.com/admin instead of just 
>>> admin.example.com. I would like to map admin.example.com to the admin 
>>> servlet and www.example.com to the main applications dispatcher servlet.
>>> At a later stage, www.example.com/admin might be used for other admin 
>>> tasks that must be accessible by users registered on the main site. 
>>> But /admin is already shadowed.
>>>
>>> Hopefully I could explain my problem a little better.
>>>
>> Yes, that is much clearer.
>> Honestly without having really figured out the details of your 
>> explanation, I would nevertheless point you in 2 directions for that 
>> kind of thing :
>> a) use an Apache httpd front-end proxy, and use the httpd proxying 
>> and/or URL rewriting capabilities to do what you want
> 
> thanks for your suggestion! The application anyway needs a reverse proxy 
> / ssl gateway, because tomcat isn't capable of doing TLS with SNI. Your 
> proposed solution seems appropriate, although I have to figure out how 
> to cleanly generate the correct urls within the application.

<VirtualHost *:80>
   ServerName admin.mydomain.com
..
   ProxyPass / ajp://mytomcat.local/admin/
   ProxyPassReverse / ajp://mytomcat.local/admin/
   ProxyPassReverseCookieDomain "/admin" "/"
</VirtualHost>

<VirtualHost *:80>
   ServerName www.mydomain.com
..
   ProxyPass / ajp://mytomcat.local/
   ProxyPassReverse / ajp://mytomcat.local/
</VirtualHost>

The ProxyPassReverse* take care of re-directs and cookies.
But in your "admin" application pages, you should make sure that your are returning only 
relative URLs.
E.g. if the admin application would normally return a page with
<img src="/admin/images/logo.jpg" />
it should instead return
<img src="images/logo.jpg" />
and the browser and the proxy should then do the right thing.

Or else, you could add an output filter at the httpd level, and rewrite all the URLs on 
the fly.
That is a bit resource-instensive, but I would imagine that your admin application is not 
one with very high traffic.

Note also : in the schema above, there is nothing which prevents a user on 
"www.mydomain.com" to request a URL like "/admin", which could lead to accidents..
You may want to add a RewriteCond/RewriteRule in that VirtualHost which prevents them 
doing that.



> 
> Regards,
> Christian
> 
>> or
>> b) use the built-in Rewrite Valve of Tomcat 8 
>> (http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html), or if you are 
>> at a lower Tomcat version, use the add-on URLRewrite Filter 
>> (www.tuckey.org)
>>
>> A Valve is a Tomcat-specific component, so that solution is generally 
>> non-portable to another servlet container.  The others (httpd 
>> front-end or the URLRewriteFilter (a servlet filter)) are portable.
>>
>> Personally, in this case, and considering that you want to do 
>> different things depending on the hostname used to access Tomcat, I 
>> would go the httpd front-end route, with 2 VirtualHosts at the httpd 
>> level, proxying to a single Host at the Tomcat level (but in one case, 
>> after modifying the URL).
>> That's because I think that trying to do this at the Tomcat level only 
>> may give you headaches in terms of self-referential URLs returned by 
>> your application.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christian <cm...@famiru.de>.
André,

On 28.06.2015 23:11, André Warnier wrote:
> Christian wrote:
>> Mark,
>>
>> On 28.06.2015 19:58, Mark Eggers wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> Christian,
>>>
>>> On 6/28/2015 9:01 AM, Christian wrote:
>>>> Hello all,
>>>>
>>>> is it somehow possible to create a web application with multiple
>>>> servlets that are registered to different domains for the same url
>>>> paths using tomcat 8? I already crawled through the catalina code
>>>> that is responsible for the servlet selection and didn't find
>>>> anything that would allow this. But this doesn't mean that it
>>>> isn't possible at all. As far as I know, java configuration for
>>>> servlet registration doesn't allow passing domain names. But maybe
>>>> there is an option within context.xml.
>>>>
>>>> I want to create a web application that has different domains for
>>>> the application itself and its management site. Both parts should
>>>> run at the context root, at different domains. The application
>>>> needs a shared (spring-)context in which the application's beans
>>>> are stored.
>>>>
>>>> Regards, Christian
>>> I'm not quite sure what your after. Sharing Spring beans across
>>> contexts and domains sounds like there's an underlying requirement I
>>> don't understand.
>>
>> ok, I'll try to explain, as aliases won't do what I need.
>> It's an application that can be configured via an admin interface. 
>> The admin interface is one part of the webapp.
>> The main application is accessible via www.example.com. The admin 
>> interface needs access to the beans within the main application (e. 
>> g. to create tenants that live in context scope), that's why I 
>> created just one webapp with two dispatcher servlets.
>> One dispatcher servlet is responsible for the admin interface and one 
>> for the main application. Until now I make tomcat select them by 
>> using a context path for the admin interface.
>> But this is redundant, because the admin interface needs to be 
>> accessed via admin.example.com/admin instead of just 
>> admin.example.com. I would like to map admin.example.com to the admin 
>> servlet and www.example.com to the main applications dispatcher servlet.
>> At a later stage, www.example.com/admin might be used for other admin 
>> tasks that must be accessible by users registered on the main site. 
>> But /admin is already shadowed.
>>
>> Hopefully I could explain my problem a little better.
>>
> Yes, that is much clearer.
> Honestly without having really figured out the details of your 
> explanation, I would nevertheless point you in 2 directions for that 
> kind of thing :
> a) use an Apache httpd front-end proxy, and use the httpd proxying 
> and/or URL rewriting capabilities to do what you want

thanks for your suggestion! The application anyway needs a reverse proxy 
/ ssl gateway, because tomcat isn't capable of doing TLS with SNI. Your 
proposed solution seems appropriate, although I have to figure out how 
to cleanly generate the correct urls within the application.

Regards,
Christian

> or
> b) use the built-in Rewrite Valve of Tomcat 8 
> (http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html), or if you are 
> at a lower Tomcat version, use the add-on URLRewrite Filter 
> (www.tuckey.org)
>
> A Valve is a Tomcat-specific component, so that solution is generally 
> non-portable to another servlet container.  The others (httpd 
> front-end or the URLRewriteFilter (a servlet filter)) are portable.
>
> Personally, in this case, and considering that you want to do 
> different things depending on the hostname used to access Tomcat, I 
> would go the httpd front-end route, with 2 VirtualHosts at the httpd 
> level, proxying to a single Host at the Tomcat level (but in one case, 
> after modifying the URL).
> That's because I think that trying to do this at the Tomcat level only 
> may give you headaches in terms of self-referential URLs returned by 
> your application.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by André Warnier <aw...@ice-sa.com>.
Christian wrote:
> Mark,
> 
> On 28.06.2015 19:58, Mark Eggers wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Christian,
>>
>> On 6/28/2015 9:01 AM, Christian wrote:
>>> Hello all,
>>>
>>> is it somehow possible to create a web application with multiple
>>> servlets that are registered to different domains for the same url
>>> paths using tomcat 8? I already crawled through the catalina code
>>> that is responsible for the servlet selection and didn't find
>>> anything that would allow this. But this doesn't mean that it
>>> isn't possible at all. As far as I know, java configuration for
>>> servlet registration doesn't allow passing domain names. But maybe
>>> there is an option within context.xml.
>>>
>>> I want to create a web application that has different domains for
>>> the application itself and its management site. Both parts should
>>> run at the context root, at different domains. The application
>>> needs a shared (spring-)context in which the application's beans
>>> are stored.
>>>
>>> Regards, Christian
>> I'm not quite sure what your after. Sharing Spring beans across
>> contexts and domains sounds like there's an underlying requirement I
>> don't understand.
> 
> ok, I'll try to explain, as aliases won't do what I need.
> It's an application that can be configured via an admin interface. The 
> admin interface is one part of the webapp.
> The main application is accessible via www.example.com. The admin 
> interface needs access to the beans within the main application (e. g. 
> to create tenants that live in context scope), that's why I created just 
> one webapp with two dispatcher servlets.
> One dispatcher servlet is responsible for the admin interface and one 
> for the main application. Until now I make tomcat select them by using a 
> context path for the admin interface.
> But this is redundant, because the admin interface needs to be accessed 
> via admin.example.com/admin instead of just admin.example.com. I would 
> like to map admin.example.com to the admin servlet and www.example.com 
> to the main applications dispatcher servlet.
> At a later stage, www.example.com/admin might be used for other admin 
> tasks that must be accessible by users registered on the main site. But 
> /admin is already shadowed.
> 
> Hopefully I could explain my problem a little better.
> 
Yes, that is much clearer.
Honestly without having really figured out the details of your explanation, I would 
nevertheless point you in 2 directions for that kind of thing :
a) use an Apache httpd front-end proxy, and use the httpd proxying and/or URL rewriting 
capabilities to do what you want
or
b) use the built-in Rewrite Valve of Tomcat 8 
(http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html), or if you are at a lower Tomcat 
version, use the add-on URLRewrite Filter (www.tuckey.org)

A Valve is a Tomcat-specific component, so that solution is generally non-portable to 
another servlet container.  The others (httpd front-end or the URLRewriteFilter (a servlet 
filter)) are portable.

Personally, in this case, and considering that you want to do different things depending 
on the hostname used to access Tomcat, I would go the httpd front-end route, with 2 
VirtualHosts at the httpd level, proxying to a single Host at the Tomcat level (but in one 
case, after modifying the URL).
That's because I think that trying to do this at the Tomcat level only may give you 
headaches in terms of self-referential URLs returned by your application.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Christian <cm...@famiru.de>.
Mark,

On 28.06.2015 19:58, Mark Eggers wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Christian,
>
> On 6/28/2015 9:01 AM, Christian wrote:
>> Hello all,
>>
>> is it somehow possible to create a web application with multiple
>> servlets that are registered to different domains for the same url
>> paths using tomcat 8? I already crawled through the catalina code
>> that is responsible for the servlet selection and didn't find
>> anything that would allow this. But this doesn't mean that it
>> isn't possible at all. As far as I know, java configuration for
>> servlet registration doesn't allow passing domain names. But maybe
>> there is an option within context.xml.
>>
>> I want to create a web application that has different domains for
>> the application itself and its management site. Both parts should
>> run at the context root, at different domains. The application
>> needs a shared (spring-)context in which the application's beans
>> are stored.
>>
>> Regards, Christian
> I'm not quite sure what your after. Sharing Spring beans across
> contexts and domains sounds like there's an underlying requirement I
> don't understand.

ok, I'll try to explain, as aliases won't do what I need.
It's an application that can be configured via an admin interface. The 
admin interface is one part of the webapp.
The main application is accessible via www.example.com. The admin 
interface needs access to the beans within the main application (e. g. 
to create tenants that live in context scope), that's why I created just 
one webapp with two dispatcher servlets.
One dispatcher servlet is responsible for the admin interface and one 
for the main application. Until now I make tomcat select them by using a 
context path for the admin interface.
But this is redundant, because the admin interface needs to be accessed 
via admin.example.com/admin instead of just admin.example.com. I would 
like to map admin.example.com to the admin servlet and www.example.com 
to the main applications dispatcher servlet.
At a later stage, www.example.com/admin might be used for other admin 
tasks that must be accessible by users registered on the main site. But 
/admin is already shadowed.

Hopefully I could explain my problem a little better.

Regards,
Christian

> Anyway, how about using aliases?
>
> <Host name="first.host.name">
>     <Alias>second.host.name</Alias>
> </Host>
>
> . . . just my two cents
> /mde/
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2
>
> iQEcBAEBAgAGBQJVkDWnAAoJEEFGbsYNeTwtXUsIAKFQ2UitPMWeI8PGPrDIrrIQ
> czJu0kCmE2J/cBQx28ghvvMZEBJ7EI7926Q7/a4e4TASXT3J3gka/2RYkeGy0w/0
> P0PLTCpAGIJi+v4PyZ9lNj7f/gAcU60g4EiaNz/10HTbBcFwy1W/vLg2wl54+aNt
> C6Z+3YWtaCNCDjon1PcqMTOKqDkkDjcf+ePXQiUdEIqI6wquBY0xIKkwPZtbjV+G
> hkq2tAFqzdvW4fGk16Cv6FfZJrYDVqEYCK/TyO08FEbgXtIhflIWYU+pmJ9b8Jdr
> xppp26doG3MeZMw4t9uijkRhe22PdXI73tvHiptc92V03p+Fmej8SrJZoKPPj78=
> =6QWg
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail:users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Distinct servlets for different domains in one web application

Posted by Mark Eggers <it...@yahoo.com.INVALID>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christian,

On 6/28/2015 9:01 AM, Christian wrote:
> Hello all,
> 
> is it somehow possible to create a web application with multiple 
> servlets that are registered to different domains for the same url 
> paths using tomcat 8? I already crawled through the catalina code 
> that is responsible for the servlet selection and didn't find 
> anything that would allow this. But this doesn't mean that it
> isn't possible at all. As far as I know, java configuration for
> servlet registration doesn't allow passing domain names. But maybe
> there is an option within context.xml.
> 
> I want to create a web application that has different domains for
> the application itself and its management site. Both parts should
> run at the context root, at different domains. The application
> needs a shared (spring-)context in which the application's beans
> are stored.
> 
> Regards, Christian

I'm not quite sure what your after. Sharing Spring beans across
contexts and domains sounds like there's an underlying requirement I
don't understand.

Anyway, how about using aliases?

<Host name="first.host.name">
   <Alias>second.host.name</Alias>
</Host>

. . . just my two cents
/mde/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJVkDWnAAoJEEFGbsYNeTwtXUsIAKFQ2UitPMWeI8PGPrDIrrIQ
czJu0kCmE2J/cBQx28ghvvMZEBJ7EI7926Q7/a4e4TASXT3J3gka/2RYkeGy0w/0
P0PLTCpAGIJi+v4PyZ9lNj7f/gAcU60g4EiaNz/10HTbBcFwy1W/vLg2wl54+aNt
C6Z+3YWtaCNCDjon1PcqMTOKqDkkDjcf+ePXQiUdEIqI6wquBY0xIKkwPZtbjV+G
hkq2tAFqzdvW4fGk16Cv6FfZJrYDVqEYCK/TyO08FEbgXtIhflIWYU+pmJ9b8Jdr
xppp26doG3MeZMw4t9uijkRhe22PdXI73tvHiptc92V03p+Fmej8SrJZoKPPj78=
=6QWg
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org