Jsp forward does not work as expected

I have jsp depending on some state that I am doing on another page.

I have /myjsp/first.jsp which has code like,

<% if(some condtion){ somelogic } else { application.getRequestDispatcher("/myjsp/another.jsp").forward(request,response); } %> <div> <jsp:include page='header.jsp' /> </div> 

In this code, if the stream comes to a different state, condition control transfers to another .jsp.

But also it appears below the div tag from /myjsp/first.jsp.

I want to maintain control over another.jsp when it goes there, and it should not show anything from the current page, i.e. div tag from / myjsp / first.jsp

+4
source share
4 answers

Put the div inside the if .

Or even better, do not use scripts and do not switch from JSP. JSP should be used as presentation components and only markup should be created. The workflow must be executed from within the servlet or action of the preferred MVC framework.

+1
source

Using:

 <jsp:forward page="pageName.jsp"></jsp:forward> 

instead of application.getRequestDispatcher ("/myjsp/another.jsp") forward (request, response) ;.

or

response.sendRedirect ("pagenName.jsp");

+1
source

Using

  response.sendRedirect(); 

instead

  forward(); 

I think this will solve your problem.

0
source

I agree, you should not use "forward" (or sendRedirect) in jsp, because there is HTML code with scriptlets in the response buffer, you can fix (or hide) the problem by increasing the buffer size with <% @page autoFlush = "true "buffer =" 2000kb "%>; I use JavaBeans instead.

0
source

Source: https://habr.com/ru/post/1447305/


All Articles