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