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

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

问题描述

为清楚起见,将文件名更改为'follow-up.jsp'.

changed file name to 'follow-up.jsp' for clarity.

这是我今天早些时候发布的较长问题的较短版本,希望是更好的版本(链接:

This is a shorter, and hopefully better version of a longer question I posted earlier today (link: Java web development: transfer control from one servlet to another while passing the request object).

我有一个提交后发布的表格.在要执行POST操作的相应servlet中,我想再次显示表单页面或后续页面,具体取决于验证是否成功. 同时表单页面后续页面需要用户提交的表单数据;表单页面需要它来重新填充表单字段,后续页面需要它来显示某些数据(例如,让用户再次检查它是否正确).因此,我希望包含提交数据的请求对象可用于两个页面.

I have a form that is POST-ed upon submit. In the corresponding servlet to which the POST action is done, I want to show either the form page again or a follow-up page, depending on whether validation succeeds. BOTH the form page AND the follow-up page need the form data submitted by the user; the form page needs it to re-populate the form fields, and the follow-up page needs it to show some of this data (for example, to have the user double-check that it is correct). Therefore, I want the request object containing the submitted data to be available to BOTH pages.

转发回表单不是问题:

RequestDispatcher rd = getServletContext().getRequestDispatcher("/form.jsp"); rd.forward(请求,响应);

RequestDispatcher rd = getServletContext().getRequestDispatcher("/form.jsp"); rd.forward(request, response);

但是如何以这样一种方式到达后续页面(例如,follow-up.jsp),使得在该后续页面中仍可以访问包含已提交表单数据的请求对象?

But how do I reach the follow-up page (say, follow-up.jsp) in such a way that the request object containing the submitted form data is still accessible in that follow-up page?

以下内容似乎无效:

response.sendRedirect("/MyProject/follow-up.jsp");

这是因为,如果名称"是在表单处理Servlet中分配给请求对象的属性的表单参数,并且该属性也方便地称为名称",则在后续操作中使用以下行. jsp:

This is because if "name" is a form parameter that was assigned to an attribute of the request object in the form handling servlet, with this attribute conveniently also being called "name", then the following line in follow-up.jsp:

您的名字是:$ {name}.

Your name is: ${name}.

不打印用户提交的名称(相反,它不打印任何内容,即仅显示您的名字是:").换句话说,原始请求对象的属性在follow-up.jsp中不再可用.这在response.sendRedirect触发一个 new HTTP请求(具有其自己的请求和响应对象)中是有道理的.但这不是我想要的.

does NOT print the user-submitted name (instead it prints nothing, i.e. only "Your name is:"). In other words, the attributes of the original request object are no longer available in follow-up.jsp. This makes sense in that response.sendRedirect triggers a new HTTP-request (with its own request and response objects). But is not what I want.

另一个(次要)问题是sendRedirect无法发出POST请求(它仅使用GET;请参阅[link]

Another (secondary) concern is that sendRedirect is unable to issue POST-requests (it only uses GET; see [link] response.sendRedirect() from Servlet to JSP does not seem to work).

另外,我意识到我可以实际使用:

Also, I realise I could actually use:

rd.forward(请求,响应);

rd.forward(request, response);

对于两个请求.但是,这将导致两个页面在地址栏中显示相同的URL (即,路由servlet的名称,例如"...../Register").这绝对是不好的,因为表单页面和后续页面是完全不同的页面.后续页面与表单无关(除了显示一些已提交的表单数据).

for BOTH requests. However, that would result in both pages having the same URL showing in the address bar (i.e. the name of the routing servlet, like "...../Register"). And that is definitely bad, because the form page and the follow-up are completely different pages; the follow-up page has little to do with the form (except for displaying some of the submitted form data).

谢谢.

推荐答案

在这种情况下,我认为您不需要将数据存储在请求中,而是存储在对话范围中....或会话范围中...关于重定向不支持发布...那么理想情况下,根据定义,所有重定向都应该仅是获取请求.....如果您想使用POST请求进行重定向,请重新评估您的方法...

In this case I believe you need to store the data not in request, but in a conversation scope....or in the session scope...regarding redirection not supporting post...then ideally all redirections by definition should be get requests only.....if you want to redirect using a a POST request please re-evaluate your approach...

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

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