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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -