Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ServletRequest server name programmatically

I need to change the serverName of the ServletRequest object in my Grails controller. I'm having trouble figuring out how to do this since the serverName is a read-only property.

like image 375
Akolopez Avatar asked Mar 09 '26 23:03

Akolopez


1 Answers

The most correct thing to do is probably to set up a clever filter or redirect which "fixes" your request URL before your servlet even gets involved. I know nothing about how to do that; you should ask on serverfault.com if you want to do that.

In java, you can fake it by creating your own subclass of HttpServletRequestWrapper which provides setServerName() and overrides getServerName() while delegating all other methods to the superclass. You can then provide a filter which creates an instance of your request and sends that one down the chain.

public void doFilter(ServletRequest request, ServletResponse response,
             FilterChain chain) 
             throws IOException, ServletException {
  YourHttpServletRequest yourRequest =
              new YourHttpServletRequest(request, newServerName);
  chain.doFilter(yourRequest, response);
}
like image 93
Paul Hicks Avatar answered Mar 12 '26 11:03

Paul Hicks