java - How to inspect JSP request URL for String -
i have following processor.jsp
file:
<% response.sendredirect("http://buzz.example.com"); %>
i want change inspects http request url presence of word "fizz" and, if exists, redirect user http://fizz.example.org
instead.
so like:
<% string requrl = request.geturl().tolowercase(); string token = null; if(requrl.contains("fizz")) { token = "fizz"; } else { token = "buzz"; } string respurl = "http://%%%token%%%.example.com".replace("%%%token%%%", token); response.sendredirect(respurl); %>
however doesn't work. ideas on should using instead of request
, or if i'm doing else wrong?
always try avoid scriplet instead use javaserver pages standard tag library , expression language more easy use , less error prone.
sample code using jstl <c:choose>
equivalent java switch
case.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ... <c:set var="defaulturl" value="xyz"/> <c:choose> <c:when test="${fn:containsignorecase(request.getrequesturi(), 'fizz')}"> <c:redirect url="http://fizz.example.com" /> </c:when> <c:when test="${fn:containsignorecase(request.getrequesturi(), 'buzz')}"> <c:redirect url="http://buzz.example.com" /> </c:when> <c:otherwise> <c:redirect url="http://${defaulturl}.example.com" /> </c:otherwise> </c:choose>
read more all tags | functions , oracle tutorial - using jstl
Comments
Post a Comment