Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JSP get hostname (url) and then redirect if not

Tags:

java

jsp

I'm not a jsp developer but i would like to make a small adjustment to a jsp page in some open source software i have.

All i want to do is find out what url i used to get to the page, let's say https://old.example.com, and if i did then i would like to redirect the user to https://new.example.com, but i don't want to get in a redirect loop obviously...

how would i go about this ...

like image 307
Tobias Hagenbeek Avatar asked Feb 16 '23 10:02

Tobias Hagenbeek


1 Answers

You could try something like this:

<% 
    if(request.getRequestURL().toString().equals("https://old.example.com")){    
        String redirectURL = "https://new.example.com"; 
        response.sendRedirect(redirectURL);
    }
%>

And in case you don't prefer java code inside your JSP (which is a bad programming practice), you could try something like this using JSTL

<c:if test="${pageContext.request.requestURL == 'https://old.example.com'}">
    <jsp:forward page="https://new.example.com"/>
</c:if>
like image 50
fujy Avatar answered Feb 20 '23 10:02

fujy