简体   繁体   English

Servlet包含预期转发后的响应

[英]Servlet includes response after forward is it expected

Servlet includes response after forward is it expected? Servlet包含转发后的响应是预期的吗? Find the code below in my servlet. 在我的servlet中找到以下代码。 Here print.html contains only "say Hello to print". 在这里,print.html仅包含“ say Hello to print”。 My expectation here that i should get response only "say Hello to print". 我在这里的期望是,我只能得到“说你好打印”的答复。 But i get response "say Hello to print" and "add in response after forward" also. 但我也得到答复“说你好要打印”和“在转发后添加答复”。 is it expected? 是预期的吗?

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   resp.setContentType("text/html");
   PrintWriter writer = resp.getWriter();
   req.getRequestDispatcher("/test/print.html").forward(req, resp);
   writer.println("add in response after forward");
}

该图说明了所有前进方法

What forward method does is, it redirects you to different page with the current request and response object and final response is thus returned by redirected page. 转发方法的作用是,它将使用当前请求和响应对象将您重定向到不同的页面,并通过重定向的页面返回最终响应。

consider an example, you submitted the form to some servlet say "XYZ" now you want to process some data of that for in "XYZ" and after that you want to do remaining processing in servlet "PQR". 考虑一个示例,您将表单提交给了一个名为“ XYZ”的servlet,现在您想在“ XYZ”中处理该数据的某些数据,然后再在servlet“ PQR”中进行剩余的处理。 This is the main significance of calling forward method with current request and response object 这是使用当前请求和响应对象调用前向方法的主要意义

Ask in comments if you have any other doubt. 如有其他疑问,请在评论中提问。

Reference link: http://www.javatpoint.com/requestdispatcher-in-servlet 参考链接: http : //www.javatpoint.com/requestdispatcher-in-servlet

Happy Coding!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM