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}
- If I want to include all paramters, like “cmd”, to a new jsp page, how to do it ?
- based on No.1, if I want to add NEW attributes, like “name” to a new jsp page, how to do it ?
- In the above codes, use include or forward, the results are same. why ?
Thanks
All request parameters are in EL available by the
${param}map.You don’t need to prepare anything in the servlet.
You already did it by
request.setAttribute("name", name)and${name}.Not exactly. If you use
include(), the delegatee would not be able to control the response headers. You should be usingforward()in this case. See also the javadoc. You should useinclude()only if you want to append something before and after instead of fully delegating the request/response.See also: