如何在第一个JSP Servlet中调用第二个JSP Servlet? [英] How do I call a second JSP servlet while in the first JSP servlet?

查看:117
本文介绍了如何在第一个JSP Servlet中调用第二个JSP Servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我提交了一个简单的页面(该页面没有参数,没有表单等,并且无法向该页面添加任何内容),最后我进入了第一个servlet.现在,我确定是时候在第二个servlet中做这些事情了.第二个servlet需要填充一堆参数和表单字段,因此首先我需要在第一个servlet中进行设置,然后弄清楚如何将这些东西添加到第二个servlet中.当我尝试向参数映射中添加某些内容时,它出错了,不允许对锁定的参数映射进行任何修改"(这是JSP的工作方式).我当时在想也许我应该实例化另一个请求对象,但是我不确定如何做到这一点(并避免陷入困境).如果在第一个servlet中,我曾经能够用所有正确的东西"构造一个请求对象,那么我需要对该请求运行第二个servlet,并让我进入第二个servlet重定向的任何页面.我也是.我认为那只是一个response.sendRedirect();

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();

如何获取第一个servlet中定义的其他参数和内容,以便在执行sendRedirect时,第二个servlet具有所需的一切?

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?

推荐答案

调用其他servlet的常规方法是使用

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

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

如果您想添加额外的请求参数并且,并希望在重定向页面的URL(可标记!)中添加它们,那么您必须根据以下内容填充查询字符串这些参数需要您自己重定向.

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);

其中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("&amp;");
            }

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

    return queryString.toString();
}

但是,由于该servlet似乎在同一个容器中运行,并且您想重用第二个servlet的逻辑而不将其他参数公开给公众,因此,更好的方法可能是重构该紧密的业务代码.将第二个servlet耦合到另一个单独且可重用的类中,您最终只需导入并调用这两个servlet.然后,您可以使用可重用的Javabean对象将数据传递给该类.

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.

例如servlet 1:

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);

和servlet 2:

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);

SomeBusinessService通常是一个EJB.

这篇关于如何在第一个JSP Servlet中调用第二个JSP Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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