문제

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); 
%>  
도움이 되었습니까?

해결책

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")

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top