How to add a parameter to the existing HttpServletRequest of my Java Servlet?

前端 未结 2 2018
臣服心动
臣服心动 2020-12-29 22:25

I want to add a new parameter to the parameter map of my HttpServletRequest.

The following code

 request().getParameterMap().put(\"j_use         


        
相关标签:
2条回答
  • 2020-12-29 23:04

    I ran into a similar issue and got around it by making a copy of the parameter map.

    Map<String, String[]> params = new HashMap<String, String[]>(req.getParameterMap());
    
    0 讨论(0)
  • 2020-12-29 23:16

    The parameters of a request are the values sent as parameters by the browser. There is no reason to change them. If you want to associate some value to the request, use an attribute rather than a parameter. This has the additional advantage that an attribute may be any object and not just a String:

    request.setAttribute("user", new User(userName, password));
    

    You may add parameters if you forward the request to another resource (although I wouldn't say it's a good practice):

    request.getRequestDispatcher("/some/path?j_username=" + user + "&j_password=" + pwd).forward(request, response);
    

    The parameters should be encoded correctly, though.

    0 讨论(0)
提交回复
热议问题