yegor256
yegor256

Reputation: 105133

Can I assign a servlet to a particular domain?

I have two CNAME-s pointed to the same server. I want to assign one servlet to the first CNAME and another one to the second CNAME. Can I do it in web.xml (or somehow else without manual parsing of ServletRequest)?

Upvotes: 1

Views: 72

Answers (1)

Nishant
Nishant

Reputation: 55866

One of the idea to have a filter, and in it have a condition based on ServletRequest#getServerName() and dispatch the request to appropriate servlet, will do.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
throws IOException, ServletException {
    ...
    [other processing/validations]
    ...
    if(request.getServerName().equals("domain1.com"))
      request.getRequestDispatcher("/servlet1").forward(request, response)
    else
      request.getRequestDispatcher("/servlet2").forward(request, response)

}

obviously, you could have <init-param> in your web.xml to dynamically set the domains, so that you could change these values based on your build profile.

Upvotes: 2

Related Questions