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?
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?
<% response.sendRedirect("bla h"+request .getServer Name().sub string(2,r equest.get ServerName ().indexOf ('.'))); %>
Actually ysnky is right, you should use getRequestURI. I should go back to my beer.
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.dom ain.com")> -1)
response.sendRedirect("sub 1.domain.c om/url1");
else if (url.toUpperCase().indexOf ("sub2.dom ain.com")> -1)
response.sendRedirect("sub 2.domain.c om/url2");
else if (url.toUpperCase().indexOf ("sub3.dom ain.com")> -1)
response.sendRedirect("sub 3.domain.c om/url3");
%>
Never worked with JSP, so may be something simple I'm missing?
<%
String url = ((HttpServletRequest) request).getRequestURI();
if (url.toUpperCase().indexOf
response.sendRedirect("sub
else if (url.toUpperCase().indexOf
response.sendRedirect("sub
else if (url.toUpperCase().indexOf
response.sendRedirect("sub
%>
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.dom ain.com")> -1)
response.sendRedirect("sub 1.domain.c om/url1");
else if (url.toLowerCase().indexOf ("sub2.dom ain.com")> -1)
response.sendRedirect("sub 2.domain.c om/url2");
else if (url.toLowerCase().indexOf ("sub3.dom ain.com")> -1)
response.sendRedirect("sub 3.domain.c om/url3");
%>
<%
String url = ((HttpServletRequest) request).getRequestURI();
if (url.toLowerCase().indexOf
response.sendRedirect("sub
else if (url.toLowerCase().indexOf
response.sendRedirect("sub
else if (url.toLowerCase().indexOf
response.sendRedirect("sub
%>
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?
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;
String url = ((HttpServletRequest) request).getRequestURI();
out.println(url);
if (true)
return;
ASKER
Just prints this
/
On a blank page....
/
On a blank page....
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Almost there
now returns
http://sub.domain.com
How do I strip out the protocol and the // to just get
sub.domain.com?
now returns
http://sub.domain.com
How do I strip out the protocol and the // to just get
sub.domain.com?
ASKER
Never mind, it works with the code you sent, thanks!
if (url.toUpperCase().indexOf
response.sendRedirect("ww1
else if (url.toUpperCase().indexOf
response.sendRedirect("ww2