How to set a parameter in a HttpServletRequest?

后端 未结 7 713
感动是毒
感动是毒 2021-02-01 03:16

I am using a javax.servlet.http.HttpServletRequest to implement a web application.

I have no problem to get the parameter of a request using the getParamete

7条回答
  •  心在旅途
    2021-02-01 03:41

    If you really want to do this, create an HttpServletRequestWrapper.

    public class AddableHttpRequest extends HttpServletRequestWrapper {
    
       private HashMap params = new HashMap();
    
       public AddableingHttpRequest(HttpServletRequest request) {
               super(request);
       }
    
       public String getParameter(String name) {
               // if we added one, return that one
               if ( params.get( name ) != null ) {
                     return params.get( name );
               }
               // otherwise return what's in the original request
               HttpServletRequest req = (HttpServletRequest) super.getRequest();
               return validate( name, req.getParameter( name ) );
       }
    
       public void addParameter( String name, String value ) {
               params.put( name, value );
       }
    
    }
    

提交回复
热议问题