简体   繁体   English

Servlet ReqeustDispatcher转发给调用者或先前的Servlet

[英]Servlet ReqeustDispatcher forward to caller or previous servlet

I have a SERVLET_A which forward to a form(form.jsp page). 我有一个SERVLET_A,它转发到一个表单(form.jsp页)。 The form submits to SERVLET_B. 表单提交到SERVLET_B。 After values are inserted into database by SERVLET_B, I have to show the message "Values Inserted successfully" or error messages on the form". Now Here is where the problem lies. 通过SERVLET_B将值插入数据库后,我必须显示消息“已成功插入值”或表单上的错误消息。现在,这就是问题所在。

I tried to make the requestdispatcher to forward to SERVLET_A to from SERVLET_B. 我试图使requestdispatcher从SERVLET_B转发到SERVLET_A。 But it does not work and also will not give me an error . 但这不起作用,也不会给我一个错误。

SERVLET_A -----------> FORM.jsp ----------->SERVLET_B----------------->SERVLET_A SERVLET_A -----------> FORM.jsp ----------- >> SERVLET_B -----------------> SERVLET_A

My question is not how to get this done . 我的问题不是如何做到这一点。 because it works if I use sendRedirect instead of requestDispatcher.My questions are below 因为如果我使用sendRedirect而不是requestDispatcher可以工作,我的问题如下

  1. Is my understanding right that a servlet cannot forward back to the servlet that called it in the first place.? 我的理解正确吗,一个servlet无法转发回最初调用它的servlet。

  2. I got this to work by setting my message in session and using sendredirect . 我通过在会话中设置消息并使用sendredirect来使其工作。 Is this approach correct ? 这种方法正确吗?

  3. I was told that it is always better to show form from a servlet. 有人告诉我,从servlet显示表单总是更好。 ( hides url and always use controller ) So that is why I have a servlet_A forwarding to form.jsp. (隐藏url并始终使用控制器)所以这就是为什么我将servlet_A转发到form.jsp。 I can access form.jsp directly but wanted to do it via a servlet so that is why SERVLET_A was created. 我可以直接访问form.jsp,但想通过servlet来完成,所以才创建了SERVLET_A。 Coming from a php background this all seems to0 much to process a form. 来自php的背景,这似乎对处理表单很有帮助。 Can anybody please let me know how forms are usually processesed in jsp MVC architecture .( without framework) 任何人都可以让我知道在jsp MVC体系结构中通常如何处理表单。(无框架)

There is some problem with your code. 您的代码有问题。 It would have been better if you had posted the code. 如果您发布了代码,那就更好了。 I will answer your questions one by one. 我将一一回答您的问题。
1. Yes ServletB can again forward the request back to servletA I am posting sample code below 1.是,ServletB可以再次将请求转发回ServletA,我在下面发布示例代码
ServletA GET method urlpattern="sa" ServletA GET方法 urlpattern =“ sa”

RequestDispatcher view=request.getRequestDispatcher("form.jsp");
view.forward(request, response);

form.jsp form.jsp

<body>
   <form action="./sb">
      <input type="text" name="fn" />
      <input type="submit"  value="Submit" />
    </form>
    <%= "fn : "+(String)session.getAttribute("fn") %>
</body>

ServletB GET method urlpattern="sb" ServletB GET方法 urlpattern =“ sb”

String fn = request.getParameter("fn");
request.getSession().setAttribute("fn",fn);
RequestDispatcher view=request.getRequestDispatcher("sa");
view.forward(request, response);

2.How to process forms using MVC without frameworks 2如何在不使用框架的情况下使用MVC处理表单
- Here V stand for view. -这里V代表观点。 Its the jsp or html page. 它是jsp或html页面。 You display everything in that. 您将显示所有内容。 No processing should be done here. 此处无需进行任何处理。
- C stands for Controller. -C代表控制器。 Its your servlet. 它是您的servlet。 All data submitted in form are collected by servlet. 表单提交的所有数据均由servlet收集。 No processing here also, just collect all data. 这里也没有处理,只收集所有数据。 Then create an object a plain Java class which is actually your M ie model class that will process data and return the result. 然后,创建一个普通Java类的对象,它实际上是您的M即模型类,它将处理数据并返回结果。 In your case you should do the database part as a Model class specially using DAO design pattern 在您的情况下,应专门使用DA​​O设计模式将数据库部分作为Model类进行处理
- The servlet will take the result and pass it to a JSP or HTML page for showing it. -servlet将获取结果并将其传递到JSP或HTML页面以显示它。

What you should do 你应该怎么做
As i understood from your approach its a wrong one. 据我了解,您的方法是错误的。 You should wither use Ajax or Asynchronous Processing of Request using Servlet 3.0. 您应该使用Servlet 3.0停止使用Ajax或请求的异步处理。 I mean that you should not do request forwarding back to ServletA ie to jsp after the processing is done. 我的意思是,处理完成后,您不应该请求转发回ServletA,即jsp。 Instead you should generate an asynchronous request to ServletB for submitting data. 相反,您应该向ServletB生成异步请求以提交数据。 Now when the result is generated ServletB sends the response as JSON string which is displayed by the javascript on your form.jsp page 现在,当生成结果时,ServletB将响应作为JSON字符串发送,该字符串由javascript在form.jsp页面上显示

Using a framework or not does not make much difference. 是否使用框架并没有多大区别。

This is the way I do : 这是我的方法:

  • use a servlet (method doGet) to pre-process request, get values from a database, etc., put values in request attributes and forward to a servlet for displaying HTML - ok, it is servlet A + form.jsp 使用servlet(方法doGet)预处理请求,从数据库中获取值等,将值放入请求属性中并转发到servlet以显示HTML-好的,它是servlet A + form.jsp
  • use a post to process the submitted form ; 使用帖子处理提交的表格; that way, you can use same servlet but doPost method and your form give : <form method="POST"> with no action - using same url is largely matter of opinion ... 这样,您可以使用相同的servlet,但是doPost方法和您的form给出: <form method="POST">而无需采取任何操作-使用相同的url在很大程度上取决于意见...
  • at the end of doPost, redirect to servletA or another url ( response.senRedirect(...) ). 在doPost的末尾,重定向到servletA或另一个URL( response.senRedirect(...) )。 That way, even if user reload its page or press back button of its browser, it will never post again, but simply get again the redirected URL. 这样,即使用户重新加载其页面或按其浏览器的“后退”按钮,它也不会再发布,而只需再次获取重定向的URL。

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

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