Results 1 to 8 of 8

Thread: Add parameters to the current request

  1. #1

    Default Add parameters to the current request

    Is there any way of adding parameters to a request in an interceptor prehandle method? I need to check if a paramter exist and if not add it. I am trying to avoid the browser knowing about it with a re-direct. Thank you.

    BTW: I am referring to the info after the '?' mark.

    http://192.168.1.103/servlet/URLTest...&param2=value2

  2. #2
    Join Date
    Aug 2004
    Posts
    14

    Default

    If you want to add parameters you need to generate request wrapper. Look at HttServletRequestWrapper.

  3. #3

    Default

    Quote Originally Posted by tv
    If you want to add parameters you need to generate request wrapper. Look at HttServletRequestWrapper.
    Not sure how that will help since there is no setter method in the HttpServletRequestWrapper. To use this you would have to override certain methods such as getQueryString() which seems a bit too much to just add a name=value string parameter.

  4. #4
    Join Date
    Sep 2005
    Posts
    13

    Default

    I would like to do the same. Is it possible?

    (to add parameters to a request in the preHandle method of an interceptor)

  5. #5
    Join Date
    Jan 2005
    Location
    Sofia, Bulgaria
    Posts
    38

    Default

    You can add all request parameters to request Attributes, and then you can work only with Attributes.

    Cheers
    Rostislav Georgiev

  6. #6
    Join Date
    Aug 2005
    Location
    London
    Posts
    28

    Default

    I think what tv was suggesting is to override the getParameter method on HttpServletRequestWrapper.

    For example:

    Code:
    public String getParameter ( String paramName ) {
       // We care about the param1 parameter.
       // If it doesn't exist then return 'someDefaultValue'
       if ( "param1".equals ( paramName ) ) {
          String paramValue = super.getParameter ( paramName );
          if ( null == paramValue )
             return "someDefaultValue";
          else
             return paramValue;
       }
       else
          return super.getParameter ( paramName );
    }
    There isn't a setRequestParameter because parameters, AFAIK, are immutable in the servlet API and because different containers implements it in different ways.

    Cuong.

  7. #7
    Join Date
    Jan 2005
    Location
    Sofia, Bulgaria
    Posts
    38

    Default

    Yes, but what about if have different default parameters values, base on request ?
    You don't need to override HttpServletRequestWrapper.getParameter() method.
    The more natural way is to create HandlerInterceptor, and in the preHandle() method to move all parameters to attributes.
    Rostislav Georgiev

  8. #8
    Join Date
    Dec 2004
    Location
    Minneapolis, MN
    Posts
    20

    Default

    I agree with other posts in using preHandle in an interceptor where request.setAttribute can be called to add parameters. Example:

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    String rule = request.getParameter("rule");
    Document doc = (Document)WebUtils.getSessionAttribute(request, "navigation");

    if (rule == null) {
    rule = request.getRequestURI();
    }

    if (doc != null) {
    Iterator it = doc.selectNodes( "//navigation//nav[contains(@rule,'"+rule+"')]" ).iterator();

    // process nav display
    while (it.hasNext()) {
    Element e = (Element)it.next();
    request.setAttribute("clicked", e.attributeValue("navid"));
    }

    }

    return true;
    }

Similar Threads

  1. [SOLVED] Tiles integration with Spring
    By foxmask in forum Architecture
    Replies: 8
    Last Post: Apr 15th, 2010, 12:36 AM
  2. Replies: 17
    Last Post: Jan 2nd, 2007, 01:43 PM
  3. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  4. Replies: 9
    Last Post: Nov 1st, 2005, 10:36 PM
  5. Request parameters not in command object
    By fallofrome in forum Web Flow
    Replies: 4
    Last Post: Aug 2nd, 2005, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •