Java Web开发:在传递请求对象的同时将控制权从一个servlet转移到另一个servlet [英] Java web development: transfer control from one servlet to another while passing the request object

查看:80
本文介绍了Java Web开发:在传递请求对象的同时将控制权从一个servlet转移到另一个servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里发布了一个简短且经过修订的问题:

I have posted a somewhat shorter and revised question here: Java web development: transfer control from one servlet to another while passing the request object (Version 2)

作为Java Web开发的初学者,我不确定提交表单(POST)时应如何构建servlet/页面之间的流程.这是一个基本问题,我怀疑这对于专家来说可能是一个容易回答的问题. (不过,我的书和一些谷歌搜索并没有给出明确的答案.)我的问题有点长,这是因为我想弄清楚我的来历.谢谢您的耐心等候.

As more or less a beginner at Java web development, I’m unsure about how I should structure the flow between servlets/pages when a form is submitted (POST). It’s an elementary issue, I suspect this may be an easy question to answer for the experts. (Still, my book and some googling didn’t deliver a clear answer.) My question is a bit long, and that's because I want to make it clear where I'm coming from. Thanks for you patience.

假设我们有两个servlet A和B,每个都有其自己的" .jsp页;我们分别将这些页面称为a.jsp和b.jsp.现在只要两个页面上都没有表格(即不使用POST方法),就可以清楚地知道应该如何处理.也就是说,在显示任何.jsp页之前,相应的servlet被激活,通过设置.jsp页的相关数据元素(最值得注意的是,作为请求对象的属性)来为.jsp页做一些准备.需求,然后将请求对象(等)转发到.jsp页面,然后该页面实际显示包含数据的页面.因此,例如,页面a.jsp上的链接可能会链接到Servlet B,并且在单击该链接时,会触发针对Servlet B的GET请求,然后进行一些准备(设置一些请求属性),然后转发到其自己的.jsp页(iebjsp).

Let’s say we have two servlets A en B, with each having its ‘own’ .jsp-page; let’s call those pages a.jsp and b.jsp respectively. Now as long as there are no forms on either page (i.e., no POST method used), it’s clear how things should go. That is, before any .jsp-page is shown, the corresponding servlet is activated, doing some preparation for the .jsp-page by setting the relevant data elements (most notably, as attributes of the request object) that the .jsp-page needs, then forwarding the request object (etc.) to the .jsp-page, which then actually displays the page with the data. So for example, a link on page a.jsp may link to the servlet B, and on clicking that link a GET-request for servlet B is triggered, which then does some preparation (setting some request attributes), before forwarding to its ‘own’ .jsp-page (i.e. b.jsp).

但是现在我们假设a.jsp页面显示一个带有提交按钮,method =" POST"和action =" B"的表单.然后,是的,激活了Servlet B,并且该Servlet必须确定用户输入的数据是否有效.如果数据实际上是有效的,我们可以简单地转发到b.jsp,那里没有问题.但是,如果数据无效,该怎么办?

But now let’s assume that page a.jsp displays a form with a submit button, method="POST" and action="B". Then yes, servlet B is activated, and this servlet has to determine whether the data entered by the user is valid. If the data is in fact valid, we can simply forward to b.jsp, no problem there. But what if the data is NOT valid?

在那种情况下,我们显然希望再次显示a.jsp(表单页面),而用户第一次输入的数据仍然存在.实现此目的的一种方法是简单地将Servlet B转发到a.jsp(从而绕过Servlet A).但是,这有一个大问题:在地址栏中显示给用户的URL仍将显示为……/B".因此,用户将看到正确的页面(即包含表单的a.jsp),但网址(/B)错误.因此,例如,如果我们使用"Register"和"ThanksForRegistering"而不是"A"和"B",则用户将看到register.jsp –但URL为……/ThanksForRegistering"!不好.

In that case, we obviously want to show a.jsp (the form page) again, with the data that the user entered the first time still present. One way to achieve this, is to simply have servlet B forward to a.jsp (thus bypassing servlet A). However, there is a big problem with that: the URL shown to the user, in the address bar, will still read "……/B". So the user will see the correct page (i.e., a.jsp, containing the form), but with the wrong URL (/B). So for example, if we take "Register" and "ThanksForRegistering" instead of "A" and "B", the user will see register.jsp – but with URL "……/ThanksForRegistering"! Not good.

在请求分配器上调用"include()"而不是"forward()"似乎也不起作用.如果这样做,不仅会导致GET请求(而不是我们想要的POST请求),而且实际上会丢失带有其属性的整个(原始)请求对象(毕竟,我们需要它,重新填写表格).至少,这就是我自己的实验所显示的.因此,使用"include()"似乎根本不是可行的选择.

And calling ‘include()’ instead of ‘forward()’ on the request-dispatcher doesn’t seem to work either. If we do that, not only does it result in a GET-request (as opposed to the POST-request we want), but we actually lose the whole (original) request-object with its attributes (which we need, after all, to re-populate the form). At least, that’s what my own experimentation seems to show. So using ‘include()’ doesn’t seem like a viable option at all.

另一个明显的想法是提交的内容为"action = A"(而不是"action = B").然后,servlet A本身可以处理验证,如果验证失败,它可以简单地再次转发到a.jsp,这没有问题.但是,如果验证成功怎么办?然后,我们要显示后续页面b.jsp,但是该页面很可能再次需要原始请求对象(来自表单提交)的属性;例如,让用户检查他输入的数据实际上是否正确.因此,基本上我们遇到了与以前相同的问题,但是A和B(及其各自的.jsp页)的角色相反.因此,这似乎也不是真正的解决方案.

Another obvious idea is to have "action=A" (instead of "action=B") for the submit. Then the servlet A itself can handle the validation, and if validation fails it can simply forward to a.jsp again, no problem. BUT then what if validation succeeds? Then we want to show the follow-up page b.jsp, but that page may well need the attributes from the original request-object (from the form-submit) again; for example, to have the user check that his entered data was in fact all correct. So basically we have the same problem as before, but with the roles of A and B (and their respective .jsp-pages) reversed. So this doesn't seem like a real solution either.

我没有其他选择.

因此,基本上,我只是希望一个servlet将控制权交还给另一个servlet,但是将请求对象从前一个servlet传递到后一个servlet.或者,如果不可能的话,我希望能够直接从servlet B转发到a.jsp,同时向用户显示正确 URL.或任何其他方式来实现我想要的.

So basically, I’d simply like to be able have one servlet give control back to another servlet, but with the request object being passed from the former to the latter servlet. Or, if that’s not possible, I’d want to be able to forward from servlet B to a.jsp directly, but with the correct URL shown to the user. Or any other way to accomplish what I want.

非常感谢.

推荐答案

我认为每个servlet必须有一页的假设在这里引起了问题....有一个基于输入重定向的servlet,向前或包含特定页面....您真的不需要总是为页面调用其他servlet .....您可以使用一个带有视图解析器的前端控制器,将其组合重定向或转发到页面.

I think that the assumption that there has to be one page per servlet is causing the problem here....have one servlet which based on input redirects,forwards or includes a particular page....you dont really need to always invoke a different servlet for a page.....you can have a single front controller with a view resolver the combination of which will redirect or forward to a page.

这篇关于Java Web开发:在传递请求对象的同时将控制权从一个servlet转移到另一个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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