Search code examples
jakarta-eestrutsproject-planning

Change of Index page based on subdomain/domain name


Problem and my idea on it is just vague as I'm still in design phase. I just wanted to know something to get a head start on the problem and how/where to proceed to solve it.

Problem Part:

There's one web app built using struts-2 JSP/servlet, with the URL mywebapp.com. The requirement is every client can access this mywebapp.com using their subdomain, like webapp.abc.com, myapp.xyz.com, etc. I have to filter based on the domain name to give them a customized login page. I have saved their domain name in the database to map their details that need to be displayed on customized login page.

What I have thought is they will give the IP address of mywebapp.com to their subdomain registry so it will land on mywebapp.com, but from here, how can I filter the domain/subdomain for a customized login page?

Any possible way to start on this will be appreciated.


Solution

  • I would suggest using a Filter. With a filter, you can process any requests to your application independent of the controllers.

    For instance, if you wanted to redirect to a different page based on subdomain, your filter could manage this, either as a filter that processes before the controller call or after the controller call.

    UPDATE: There is more documentation on Struts 2 Interceptors, which can serve a similar purpose: http://java.dzone.com/articles/struts2-tutorial-part-57

     String domain = "";
     String subdomain = "";
    
     String url = request.getRequestURL();
     String[] parts = url.split(".");
    
     // subdomain.domain.com  0, 1, 2
     // subdomain1.subdomain2.domain.com  0, 1, 2, 3
     domain = (parts.length - 2 > -1) ? parts[1] : parts[];
    
     for(int i = parts.length - 1; i >= 0; i--) {
         if(i == parts.length - 2) {
             domain = parts[i];
         }
         if(i == parts.length - 3) {
             subdomain = parts[i];
         }
     }
    

    If you start from the end of the array, you know that the 2nd to last is always the second-level domain (SLD) and the 3rd from last is where the third level subdomains will be.