SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to add parameter HttpServletRequest

    Hi all,

    Implement an filter:

    Code:
    public class RezezController implements Filter {
        
        private HashMap urlMaps = new HashMap();
        
        public void init(FilterConfig filterConfig) throws ServletException {
            
    	popmap();
                                    
        }
        
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                                    
    
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            
    	 httpRequest.getRequestDispatcher("dest.jsp").forward(request,response);        
    
            chain.doFilter(request, response);
    
        }
    
        public void destroy() {
        }
        
    }
    and in my dest.jsp:

    Code:
    <%= request.getParameter("myfilter") %>

    how to add parameter in "myFilter" in my filter ? for dest.jsp display it ?

  2. #2
    SitePoint Addict mmanders's Avatar
    Join Date
    Jul 2006
    Location
    Edinburgh, Scotland
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your doFilter() method you should be able to do request.setAttribute("name", value); and then access that parameter after the RequestDispatcher.forward() call in your JSP as you have done, i.e. using request.getParameter();

  3. #3
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Parameters may not be set, and are not interchangeable with attributes.

    Parameters come from form elements, or from being appended to the url query (the portion after the ? you'll see in the address bar if you use method=get)

    Part 2 sort of contradicts what I just said about not being able to set them, what I mean is that there is no setParameter defined in HttpServletRequest.

    You can add a hidden field to the form and add the parameter that way, or use set/getAttribute

  4. #4
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really want to add a parameter you can implement a new request using the real one as a delegate (see HttpServletRequestWrapper) and override the getParameter(String s) method (obviously you'll have to add the extra parameters in the constructor or by a new method). Once you've done that just do something like:
    Code:
    chain.doFilter(new MyRequest(request, newParameters), response);

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rushiku View Post
    Parameters may not be set, and are not interchangeable with attributes.

    Parameters come from form elements, or from being appended to the url query (the portion after the ? you'll see in the address bar if you use method=get)

    Part 2 sort of contradicts what I just said about not being able to set them, what I mean is that there is no setParameter defined in HttpServletRequest.

    You can add a hidden field to the form and add the parameter that way, or use set/getAttribute
    I need of parameter else if I will be need change others files and it no is good

    hum... I put in url ! and works, sample:

    Code:
    public class RezezController implements Filter {
        
        private HashMap urlMaps = new HashMap();
        
        public void init(FilterConfig filterConfig) throws ServletException {
            
    	popmap();
                                    
        }
        
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                                    
    
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            
    	 httpRequest.getRequestDispatcher("dest.jsp?param=1&param=2").forward(request,response);        
    
            chain.doFilter(request, response);
    
        }
    
        public void destroy() {
        }
        
    }

  6. #6
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I do is wrap HttpServletRequest with my own request object in which I have added the functionality to add / remove request parameters.

    You could do the same and use a filter to to the wrapping, although you might encounter the odd cast exception if you are using code that expects a specific class of Request.


Bookmarks

Posting Permissions

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