Reputation: 31086
Now I have 2 following JavaScript functions :
function clearBillingCache(){
window.location = "billingSearchClear.html"
}
function clearBillingCache_1(){
<%
request.getSession().setAttribute("stickyCarrier", null);
request.getSession().setAttribute("stickyAgency", null);
%>
}
How to have a combined function, so only when request.getSession().getAttribute("stickyCarrier") != null do the following :
<%
request.getSession().setAttribute("stickyCarrier", null);
request.getSession().setAttribute("stickyAgency", null);
%>
window.location = "billingSearchClear.html"
otherwise do nothing.
Edit :
Thanks Bhaskara !
But actually it's more than what I showed, it might get into an infinite loop, because there is a redirect in :
@RequestMapping(value = "/billingSearchClear.html", method = RequestMethod.GET)
public String clearCache(HttpServletRequest request) {
String returnVal = "redirect:/billingSearch.html";
request.getSession().setAttribute("stickyCarrier", null);
request.getSession().setAttribute("stickyAgency", null);
return returnVal;
}
And I'm calling clearBillingCache()
in <body onload=...>
Upvotes: 0
Views: 804
Reputation: 601
Please note that the code:
function clearBillingCache_1(){
<%
request.getSession().setAttribute("stickyCarrier", null);
request.getSession().setAttribute("stickyAgency", null);
%>
}
will be rendered as
function clearBillingCache_1(){
}
You can check this by doing a "View Source" in your browser. Which means the session attributes defined in the scriptlets will anyway be set irrespective of the condition. I personally recommend you not to use the Scriptlets. You can use JSTL to do this. The idea is to use for conditional check and for setting the attributes and a Javascript code just to redirect. JSTL:
<c:if test="${stickyCarrier != null}">
<c:set var="stickyCarrier" value="null" scope="session" />
<c:set var="stickyAgency" value="null" scope="session" />
</c:if>
and then your javascript:
function clearBillingCache(){
window.location = "billingSearchClear.html"
}
Edit:
Ok Frank. So there is some more change required. You are being redirected from a controller/servlet. Please follow the below steps:
Change your controller code to this:
@RequestMapping(value = "/search.html", method = RequestMethod.GET)
public String clearCache(HttpServletRequest request) {
String returnVal = "redirect:/billingSearch.html";
if(request.getSession().getAttribute("stickyCarrier") != null)
{
request.getSession().setAttribute("stickyCarrier", null);
request.getSession().setAttribute("stickyAgency", null);
}
return returnVal;
Remove the javascript functions : clearBillingCache_1()
and
clearBillingCache()
Upvotes: 1