Sneha Goswami Sneha Goswami - 17 days ago 5
Java Question

Perform an action on JSP if redirected from a servlet

I want to perform an action on a JSP if redirected from a specific servlet only else do nothing.Is it possible?

In my JSP there are different errors defined. This JSP calls a servlet (with contentType as application/pdf) which opens in a new tab and searches for a PDF for 25 seconds and then if PDF is not found redirects to same JSP which shows the error message "File not found". I want to show the error if called from servlet only else do nothing.

JSP Code:

<%}else if(hPP!=null && hPP.get("errorcode")!=null && hPP.get("errorcode").toString().equalsIgnoreCase("Issue")){%>
<c:if test="${cameFromServlet}">
<div class="SplInputField">
<label class="FontBlod">Download fail</label>
</div>
</c:if>


servlet code

if (content == null) {
request.setAttribute("cameFromServlet", true);
String redirectJspUrl = request.getParameter("homeRedirect");
String strReceiptPage =
redirectJspUrl.substring(0, redirectJspUrl.lastIndexOf("/")) +
"/GetQReceiptPage";
response.sendRedirect(strReceiptPage);
}

Sam Sam
Answer

Add an attribute to the request in the servlet like this

httpservletRequest.setAttribute("cameFromServlet", true)

then in your JSP check for it

<c:if test="${cameFromServlet}">
  DO STUFF HERE
</c:if>