Link to home
Start Free TrialLog in
Avatar of jbreg
jbreg

asked on

JSP Redirect or Forward Based on Host Header Value

We have tomcat set up and want to amend the index.jsp page such that if a user types in

ww1.domain.com they are redirected to ww1.domain.com/blah1
ww2.domain.com they are redirected to ww2.domain.com/blah2
etc

How can this be done via JSP?
Avatar of ysnky
ysnky
Flag of Türkiye image

String url = ((HttpServletRequest) request).getRequestURI();
if (url.toUpperCase().indexOf("ww1.domain.com")>-1 )
response.sendRedirect("ww1.domain.com/blah1");
else if (url.toUpperCase().indexOf("ww2.domain.com")>-1)
response.sendRedirect("ww2.domain.com/blah2");
<% response.sendRedirect("blah"+request.getServerName().substring(2,request.getServerName().indexOf('.'))); %>
Actually ysnky is right, you should use getRequestURI. I should go back to my beer.
Avatar of jbreg
jbreg

ASKER

Put this at the top of the page, before anything else, and nothing happens?

<%
String url = ((HttpServletRequest) request).getRequestURI();
if (url.toUpperCase().indexOf("sub1.domain.com")>-1)
        response.sendRedirect("sub1.domain.com/url1");
else if (url.toUpperCase().indexOf("sub2.domain.com")>-1)
        response.sendRedirect("sub2.domain.com/url2");
else if (url.toUpperCase().indexOf("sub3.domain.com")>-1)
        response.sendRedirect("sub3.domain.com/url3");
%>
Never worked with JSP, so may be something simple I'm missing?
if you are using toUpperCase() method must upper all domain names or you can use toLowerCase()
use this;

<%
String url = ((HttpServletRequest) request).getRequestURI();
if (url.toLowerCase().indexOf("sub1.domain.com")>-1)
        response.sendRedirect("sub1.domain.com/url1");
else if (url.toLowerCase().indexOf("sub2.domain.com")>-1)
        response.sendRedirect("sub2.domain.com/url2");
else if (url.toLowerCase().indexOf("sub3.domain.com")>-1)
        response.sendRedirect("sub3.domain.com/url3");
%>
Avatar of jbreg

ASKER

I did that exactly and still it's not doing anything??
you have to trace, check what is the url variable value is set?
Avatar of jbreg

ASKER

Can you let me know how to do this?
try this and see what url is?

String url = ((HttpServletRequest) request).getRequestURI();
out.println(url);
if (true)
return;

Avatar of jbreg

ASKER

Just prints this

/

On a blank page....
Avatar of jbreg

ASKER

That makes sense looking at the documentation

Since it's a get request and there is nothing after the "/" it returns only a "/"

getRequestURI
public java.lang.String getRequestURI()Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example: First line of HTTP request  Returned Value
POST /some/path.html HTTP/1.1  /some/path.html  
GET http://foo.bar/a.html HTTP/1.0   /a.html  
HEAD /xyz?a=b HTTP/1.1  /xyz  
ASKER CERTIFIED SOLUTION
Avatar of ysnky
ysnky
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbreg

ASKER

Almost there
now returns
http://sub.domain.com

How do I strip out the protocol and the // to just get

sub.domain.com?
Avatar of jbreg

ASKER

Never mind, it works with the code you sent, thanks!