Announcement Announcement Module
Collapse
No announcement yet.
Add parameters to the current request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    If you want to add parameters you need to generate request wrapper. Look at HttServletRequestWrapper.

    Comment


    • #3
      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.

      Comment


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

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

        Comment


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

          Cheers

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment


              • #8
                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;
                }

                Comment

                Working...
                X