3

At the top of my doPost method I grab a few parameters that I previously set in the JSP using a basic form:

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String serverName = request.getParameter("serverName");
        String destFileName = request.getParameter("destFileName");         
        String userName = request.getParameter("userName");
        String Message= request.getParameter("Message");

and at the end of the doPost method I add them back to the request object using setAttribute:

        request.setAttribute("userName ", userName );           
        request.setAttribute("destFileName", destFileName);
        request.setAttribute("serverName", serverName); 
        request.setAttribute("Message", Message);   

        request.getRequestDispatcher(page).forward(request, response);

And then I get them in my JSP via request.getParameter again:

<%  /** if the parameters are already in place, grab them **/
    String destFileName = request.getParameter("destFileName");
    String user = request.getParameter("user");
    String serverName = request.getParameter("serverName");
    String Message = request.getParameter("Message");           
%>

And this works, unless I've rewritten the value on its way through the doPost method. If that is the case, then I have to use request.getAttribute in the JSP to retrieve it since request.getParameter will retrieve the value as it was defined at the top of the doPost method, ignoring any changes that were made between the top and the bottom.

Does anyone have an explanation of this? I've got everything working, but I'd like to understand why I spent a couple of hours of frustration figuring out what was wrong.

jrhooker
  • 1,373
  • 2
  • 11
  • 14
  • the keys are same in `setParameter()` and `setAttribute()`. Isn't it? – Braj Sep 02 '14 at 15:15
  • Attributes and parameters are different things, so when you use getParameter in your jsp, you get the original parameters sent with the page request. – Jaydee Sep 02 '14 at 15:17
  • Isn't it same request? If yes then you can get it in servlet and jsp as well. Are you using `forward()` or `include()` in servlet to forward/include the jsp? – Braj Sep 02 '14 at 15:17
  • Note that both parameters and attributes will be lost if you perform a redirect. – Luiggi Mendoza Sep 02 '14 at 15:17
  • 1
    BTW scriptlets <% ... %> are deprecated. Have a look at JSTL and expression language, attributes are used a lot with the EL. – Jaydee Sep 02 '14 at 15:19

1 Answers1

6

getParameter and getAttribute are completely unrelated.

getParameter

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

getAttribute

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

In other words, returns a value that was set using setAttribute().

Braj
  • 46,415
  • 5
  • 60
  • 76
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Rightyho; I see why I was confused; I suspect that I was using setAttribute completely unnecessarily most of the time. – jrhooker Sep 04 '14 at 17:06
  • @jrhooker Use `setAttribute` (and `getAttribute`) when you need to pass objects around across many servlets (or other components) handling the same request. – Sotirios Delimanolis Sep 04 '14 at 17:11