Question

I want to pass a status parameter from servlet to jsp. I am sending it through

response.sendRedirect("newpage.jsp?status=yes");

If status = yes then show success message in <div> and then set status to null. But when the newpage.jsp loads at the first time the value of status is null and it gives null pointer exception.

Same thing happens with session also.

<% 
   String status = request.getParameter("status");
   System.out.println("Check Successful of Status"+status);
   if (status.equalsIgnoreCase("yes")) {
   System.out.println("Check Successful of Status");
%>

<div style="color: green;" align="center">Selected tenant approved successfully</div>

<script type="text/javascript"> window.location.href = window.location.href.split("?")[0]; </script>

<%
   request.setAttribute("status1", null); 
%>  
Was it helpful?

Solution

In Servlet you can use

request.setAttribute("status","yes")

in jsp, you can retrieve using

request.getAttribute("status");

Yes, I missed the point

for above u need to use

RequestDispatcher rd = request.getRequestDispatcher("somefile.jsp");
rd.forward(request,response);

If u want to use response.sendRedirect("somefile.jsp"),

u can set the variable in session as

HttpSession session = request.getSession(false);

session.setAttribute("status","yes")

and get it back as

session.getAttribute("status").

Once used, u can remove it as

session.removeAttribute("status")

OTHER TIPS

request.setAttribute("status1", yourstatus); 
getServletContext().getRequestDispatcher("yourpageyouwanttosend.jsp").forward(request, response);

your view <%= request.getAttribute("status1") %>

You are using status as parameter name and you are comparing status1 in your JSP

    if (status1.equalsIgnoreCase("yes")) {

Note:

placing javacode on jsp is not good thing, switch to JSTL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top