1

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}
  1. If I want to include all paramters, like "cmd", to a new jsp page, how to do it ?
  2. based on No.1, if I want to add NEW attributes, like "name" to a new jsp page, how to do it ?
  3. In the above codes, use include or forward, the results are same. why ?

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user595234
  • 6,007
  • 25
  • 77
  • 101

3 Answers3

3

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.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • still don't understand "append something before and after ", do you have an example ? – user595234 Nov 14 '11 at 15:28
  • See the last "See also" link. – BalusC Nov 14 '11 at 15:29
  • do you mean appending something to Response ? then we should use include(), is it correct ? – user595234 Nov 14 '11 at 15:34
  • If I change to this, still works . response.setContentType("text/plain"); RequestDispatcher rd = request.getRequestDispatcher("process.jsp"); rd.forward(request, response); – user595234 Nov 14 '11 at 15:58
  • I have no idea what exactly your problem now is. If you want to pass the control fully to JSP to present the view, just use `RequestDispatcher#forward()`. That's all. You don't need to set the content type beforehand. The JSP will already do that. In general, you don't need to use `include()` if you're using JSPs as view. – BalusC Nov 14 '11 at 16:01
  • I googled this topic, I got this conclusion. most cases we just need forward(), because we always use servlet to handle logic and use another web component to generate html. When we changed response, for example, use Respoonse to generate html, same web component handles logic AND all/part of html, then we need include(). My understanding is correct ? – user595234 Nov 14 '11 at 16:26
1

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

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

  • you are not adding any specific content to the response
  • when you forward a request, you are forwarding the control to handle the response to another component (in your case process.jsp) when you include a jsp in your request, you are executing the included component and have the option of adding something extra to the stream (which you aren't doing). thats why both the actions show you the same result.
Community
  • 1
  • 1
Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35