Вопрос

I have one JSP page that have a form. When the button in this form is pushed, id called my MainServlet. This is an example of my Servlet

/***** MainServlet *****/    

/* Call the servlet that comunicate with database */  
request.getRequestDispatcher("Servlet1").forward(request,response)

/* Return on the same JSP that have invoke MainServlet */
request.getRequestDispatcher("myJsp.jsp").forward(request,response);

return;

This is wrong because when I push the button in the form, my server return an error: "Cannot forward after response has been committed"

How can I solve this problem?

Thanks.

Это было полезно?

Решение

You cannot forward to two different resources at the same time.

You need to again forward from Servlet1 to myJsp.jsp using request.getRequestDispatcher("myJsp.jsp").forward(request,response);

You cannot just directly forward two times because when you do it once, your response is already committed and client will be served with the first resource.

You can use conditional statements which will forward to proper resource depending on the proper request.

Другие советы

Once a request has been forward, the remaining codes are not executed. Its same like calling a return statement twice one after another in a method. If you want both forwards to work. You should use conditions, depending upon which, one of the forward statement will be executed.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top