How to add parameter HttpServletRequest

Hi all,

Implement an filter:


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:


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

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

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();

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

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:


chain.doFilter(new MyRequest(request, newParameters), response);

I need of parameter else if I will be need change others files and it no is good :slight_smile:

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


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() {
    }
    
}

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.