如何将参数添加到我的Java Servlet的现有HttpServletRequest中? [英] How to add a parameter to the existing HttpServletRequest of my Java Servlet?

查看:411
本文介绍了如何将参数添加到我的Java Servlet的现有HttpServletRequest中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 HttpServletRequest 的参数图中添加一个新参数。

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

以下代码

 request().getParameterMap().put("j_username", user);
 request().getParameterMap().put("j_password", pwd);

创建此错误

no modifications are allowed to a locked parameter map

正确的方法是什么这样做?

What is the correct way to do this?

推荐答案

请求的参数是浏览器作为参数发送的值。没有理由改变它们。如果要将某个值与请求关联,请使用属性而不是参数。这具有额外的优点,即属性可以是任何对象而不仅仅是字符串:

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

但参数应该正确编码。

这篇关于如何将参数添加到我的Java Servlet的现有HttpServletRequest中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆