如何在第一个 JSP servlet 中调用第二个 JSP servlet?
假设我提交一个简单的页面(没有参数、没有表单等,并且我无法向此页面添加任何内容),我最终会进入第一个 servlet。现在我决定是时候在第二个 servlet 中执行这些操作了。第二个 servlet 需要填充一堆参数和表单字段,因此首先我需要在第一个 servlet 中设置这些内容,然后弄清楚如何将这些内容传递到第二个 servlet。当我尝试向参数映射中添加某些内容时,它出错并显示“不允许对锁定的参数映射进行任何修改”(这就是 JSP 应该工作的方式)。我在想也许我应该实例化另一个请求对象,但我不确定如何做到这一点(并让自己远离热水)。如果在第一个 servlet 中,我能够使用所有“正确的内容”构造一个请求对象,那么我需要使用该请求运行第二个 servlet,并让它带我到第二个 servlet 重定向的任何页面我到。我认为这只是一个response.sendRedirect();
如何获取第一个 servlet 中定义的附加参数和内容,以便当我执行 sendRedirect 时,第二个 servlet 拥有它所需的一切?
Say I submit a simple page (that has no parameters, no forms, etc, and I can't add anything to this page), I end-up in the first servlet. Now I determine it's time to do the stuff in the second servlet. The second servlet expects a bunch of parameters and form fields populated, so first I'd need to set those up in the first servlet, then figure out how to get those things to the second servlet. When I tried to add something to the parameter map, it errored-out with "no modifications are allowed to a locked parameter map" (which is the way JSP is supposed to work). I was thinking maybe I should instantiate up another request object, but I'm not sure how to do that (and keep myself out of hot water). If, in the first servlet, I ever am able to construct a request object with all "the right stuff", then I'd need to run that second servlet with that request, and let it take me to whatever page the second servlet redirects me to. I think that one would just be a response.sendRedirect();
How do I get additional parameters and things defined in the first servlet so when I do the sendRedirect, the second servlet has everything it needs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用其他 servlet 的正常方法是使用
RequestDispatcher#include()
。如果您想添加额外的请求参数和,并且希望将它们放在重定向页面的(可添加书签!)URL 中,那么您必须自己根据这些参数填充查询字符串在重定向之前。
您可以按如下方式创建查询字符串:
其中
toQueryString()
如下所示:但是,由于该 servlet 似乎在同一个容器中运行,并且您希望重用第二个 servlet 的逻辑而不暴露如果将附加参数添加到 public 中,那么可能更好的方法是将紧密耦合的第二个 servlet 的业务代码重构为另一个单独且可重用的类,您最终只需在两个 servlet 中导入并调用该类。然后,您可以使用可重用的 Javabean 对象将数据传递给该类。
例如,servlet 1:
和 servlet 2:
SomeBusinessService
通常是一个 EJB。The normal approach to invoke other servlet would be to use
RequestDispatcher#include()
.If you'd like to add extra request parameters and you would like to end up with them in the (bookmarkable!) URL of redirected page, then you have to populate a query string based on those parameters yourself before redirecting.
You could create the query string as follows:
where
toQueryString()
look like follows:However, as that servlet seems to be running in the same container and you'd like to reuse the second servlet's logic without exposing the additional parameters into public, then likely a much better way is to refactor the business code of that tight coupled second servlet into another, separate and reuseable class which you finally just import and call in your both servlets. You can then pass the data to that class using a reuseable Javabean object.
For example, servlet 1:
And servlet 2:
The
SomeBusinessService
is usually to be an EJB.