如何在第一个 JSP servlet 中调用第二个 JSP servlet?

发布于 2024-12-11 08:56:54 字数 485 浏览 0 评论 0原文

假设我提交一个简单的页面(没有参数、没有表单等,并且我无法向此页面添加任何内容),我最终会进入第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-12-18 08:56:54

调用其他 servlet 的正常方法是使用 RequestDispatcher#include()

request.getRequestDispatcher("/secondServletURL").include(request, response);

如果您想添加额外的请求参数,并且希望将它们放在重定向页面的(可添加书签!)URL 中,那么您必须自己根据这些参数填充查询字符串在重定向之前。

response.sendRedirect(request.getContextPath() + "/secondServletURL?" + queryString);

您可以按如下方式创建查询字符串:

Map<String, String[]> params = new HashMap<String, String[]>(request.getParameterMap());
params.put("name1", new String[] {"value1"});
params.put("name2", new String[] {"value2"});
// ...

String queryString = toQueryString(params);

其中 toQueryString() 如下所示:

public static String toQueryString(Map<String, String[]> params) {
    StringBuilder queryString = new StringBuilder();

    for (Entry<String, String[]> param : params.entrySet()) {
        for (String value : param.getValue()) {
            if (queryString.length() > 0) {
                queryString.append("&");
            }

            queryString
                .append(URLEncoder.encode(param.getKey(), "UTF-8"))
                .append("=")
                .append(URLEncoder.encode(value, "UTF-8"));
        }
    }

    return queryString.toString();
}

但是,由于该 servlet 似乎在同一个容器中运行,并且您希望重用第二个 servlet 的逻辑而不暴露如果将附加参数添加到 public 中,那么可能更好的方法是将紧密耦合的第二个 servlet 的业务代码重构为另一个单独且可重用的类,您最终只需在两个 servlet 中导入并调用该类。然后,您可以使用可重用的 Javabean 对象将数据传递给该类。

例如,servlet 1:

SomeData data = new SomeData();
data.setSomeProperty1(request.getParameter("someProperty1"));
data.setSomeProperty2("some custom value");
data.setSomeProperty3("some other custom value");

SomeBusinessService service = new SomeBusinessService();
service.doSomeAction(data);

和 servlet 2:

SomeData data = new SomeData();
data.setSomeProperty1(request.getParameter("someProperty1"));
data.setSomeProperty2(request.getParameter("someProperty2"));
data.setSomeProperty3(request.getParameter("someProperty3"));

SomeBusinessService service = new SomeBusinessService();
service.doSomeAction(data);

SomeBusinessService 通常是一个 EJB。

The normal approach to invoke other servlet would be to use RequestDispatcher#include().

request.getRequestDispatcher("/secondServletURL").include(request, response);

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.

response.sendRedirect(request.getContextPath() + "/secondServletURL?" + queryString);

You could create the query string as follows:

Map<String, String[]> params = new HashMap<String, String[]>(request.getParameterMap());
params.put("name1", new String[] {"value1"});
params.put("name2", new String[] {"value2"});
// ...

String queryString = toQueryString(params);

where toQueryString() look like follows:

public static String toQueryString(Map<String, String[]> params) {
    StringBuilder queryString = new StringBuilder();

    for (Entry<String, String[]> param : params.entrySet()) {
        for (String value : param.getValue()) {
            if (queryString.length() > 0) {
                queryString.append("&");
            }

            queryString
                .append(URLEncoder.encode(param.getKey(), "UTF-8"))
                .append("=")
                .append(URLEncoder.encode(value, "UTF-8"));
        }
    }

    return queryString.toString();
}

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:

SomeData data = new SomeData();
data.setSomeProperty1(request.getParameter("someProperty1"));
data.setSomeProperty2("some custom value");
data.setSomeProperty3("some other custom value");

SomeBusinessService service = new SomeBusinessService();
service.doSomeAction(data);

And servlet 2:

SomeData data = new SomeData();
data.setSomeProperty1(request.getParameter("someProperty1"));
data.setSomeProperty2(request.getParameter("someProperty2"));
data.setSomeProperty3(request.getParameter("someProperty3"));

SomeBusinessService service = new SomeBusinessService();
service.doSomeAction(data);

The SomeBusinessService is usually to be an EJB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文