How can I name the second JSP servlet during the first JSP servlet?

Let's say I present a simple page (which has no parameters, no forms, etc., and I can’t add anything to this page), I ended up in the first servlet. Now I set the time to make the material in the second servlet. The second servlet expects a lot of parameters and fields to fill in the fields, so first I will need to install them in the first servlet, and then figure out how to get these things in the second servlet. When I tried to add something to the parameter map, it with the error "without changes is allowed for the locked parameter map" (as JSP should work). I was thinking, maybe I need to create an instance of another request object, but I'm not sure how to do it (and not interfere with hot water). If in the first servlet I can ever create a request object with all the "necessary things",then I will need to run this second servlet with this request, and let it take me to any page that the second servlet redirects to me too. I think there will simply be response.sendRedirect ();

How do I get additional parameters and things defined in the first servlet, so when I do sendRedirect, the second servlet has everything it needs?

+1
source share
1 answer

The usual approach to invoking another servlet would be to use RequestDispatcher#include().

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

If you want to add additional query parameters and you would like to add them to the URL (bookmarks!) Of the redirected page, then you need to fill in the query string based on these parameters before redirecting.

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

You can create a 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()it looks like this:

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

, , , , , , , , - - , , , , . Javabean.

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

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.

+4

All Articles