2

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dale
  • 5,520
  • 4
  • 43
  • 79

1 Answers1

5

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

            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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks! I should have said the "second servlet" was extending something that's extending something that's hooked to vendor code that I don't have the source code to that's finally extending HttpServlet. Thus, refactoring tends to get pretty tricky. Adding query parameters to the redirect URL is not real pretty, but it works! Thanks. – Dale Oct 21 '11 at 19:11
  • Ah I see. Well, you could create a new map based on the request parameter map, put new params in it and pass it to some utility method. See also answer update. – BalusC Oct 21 '11 at 19:21