Reputation: 6259
In the Java Servlet, how to include original paramters after response ?
Servlet
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cmd = request.getParameter("cmd");
System.out.println("service , cmd="+cmd);
request.setAttribute("name", "John"+System.currentTimeMillis());
RequestDispatcher rd = request.getRequestDispatcher("process.jsp");
rd.include(request, response);
}
JSP
main ${name}<br>
cmd ${cmd}
Thanks
Upvotes: 1
Views: 3608
Reputation: 10098
you can access all the request params using the request.getParameterMap() another question which can help you method, this returns a map of all params (key-value pair) which you can iterate and set the attribute.
request.setAttribute("name", "John"+System.currentTimeMillis());
What you've done here does add a new attribute called name (provided another entry doesn't exist with the key as name).
The result of include and forward are the same as
Upvotes: 0
Reputation: 160191
It's the same request, you don't need to do anything at all.
A forward means you can't have committed any response (no output to client). Include doesn't allow any response status code or header changes.
See the docs for forward/include.
Upvotes: 1
Reputation: 1108722
If I want to include all paramters, like "cmd", to a new jsp page, how to do it ?
All request parameters are in EL available by the ${param}
map.
${param.cmd}
You don't need to prepare anything in the servlet.
based on No.1, if I want to add NEW attributes, like "name" to a new jsp page, how to do it ?
You already did it by request.setAttribute("name", name)
and ${name}
.
In the above codes, use include or forward, the results are same. why ?
Not exactly. If you use include()
, the delegatee would not be able to control the response headers. You should be using forward()
in this case. See also the javadoc. You should use include()
only if you want to append something before and after instead of fully delegating the request/response.
Upvotes: 3