java修改请求参数的值
异常
java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap。网上查了一些资料,解决方案是使用Wrapper,重写HttpServletRequestWrapper,如下:
//然后在另一个地方使用时,按下面这样用,假如我要修改username的值HashMap m=new HashMap(request.getParameterMap()); /*关键部分,如果不转换成String数组,如果在下面这样是有时:eg:HttpSession session = request.getSession();Map map = request.getParameterMap();Set keySet = map.keySet();for (Iterator iterator = keySet.iterator(); iterator.hasNext();){String key = (String)iterator.next();String values[] = (String[])map.get(key); //这个地方就会报错,因为username一个String不能转换成String数组if (values.length == 1)session.setAttribute(key, values[0]);elsesession.setAttribute(key, values);}异常就会出现java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;*///m.put("username", newusername); //这样会报错m.put("username", new String[]{newusername}); //这就是对的HttpServletRequest req = (HttpServletRequest) request; com.gdcn.bpaf.security.yale.ParameterRequestWrapper wrapRequest=new com.gdcn.bpaf.security.yale.ParameterRequestWrapper(req,m); request=wrapRequest; //这是rquest就和本身的request一样了
?