request.getParamter()获得的参数是在页面上传过来的,但有时候需要动态的设置这些参数,例如转发到另一个需要使用username的页面,但是页面无法直接传递,而是要在后台才能计算获得,这时候就可以动态改变请求参数
@SuppressWarnings("unchecked")
Map<String, String> map=request.getParameterMap();//获得请求参数集合
Method method=null;
method=map.getClass().getDeclaredMethod("setLocked",boolean.class); //获得修改 参数锁定状态的方法,如果不修改锁定状态是不能修改参数集合的
if(method!=null)
{
method.invoke(map,false); //设置为未锁定
map.put("user.password", trypwd); //向集合里面放入请求参数
map.put("user.username", tryusername);
map.put("atulogin", "true");
request.getRequestDispatcher("user/Login_action.htm").forward(request, response); //转发到另一个页面,在另一个页面可以使用request.getParamter()获得这些参数
本文介绍了一种在Java Servlet中动态设置请求参数的方法,通过反射解锁getParameterMap()返回的Map,以便在转发前向目标页面注入自定义参数,如用户名和密码。
2350

被折叠的 条评论
为什么被折叠?



