在一个servlet中加倍转发 [英] Double forward in one servlet

查看:108
本文介绍了在一个servlet中加倍转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有表单的JSP页面. 当按下这种形式的按钮时,ID称为我的MainServlet. 这是我的Servlet的示例

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?

谢谢.

推荐答案

您不能同时转发到两个不同的资源.

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

您需要使用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.

这篇关于在一个servlet中加倍转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆