Java servlet:request.getParameter 和 request.setAttribute 以我不明白的方式连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25626755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:49:34  来源:igfitidea点击:

Java servlet: request.getParameter and request.setAttribute connected in a way I don't understand

javajspservlets

提问by jrhooker

At the top of my doPost method I grab a few parameters that I previously set in the JSP using a basic form:

在 doPost 方法的顶部,我获取了一些我之前使用基本形式在 JSP 中设置的参数:

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String serverName = request.getParameter("serverName");
        String destFileName = request.getParameter("destFileName");         
        String userName = request.getParameter("userName");
        String Message= request.getParameter("Message");

and at the end of the doPost method I add them back to the request object using setAttribute:

在 doPost 方法的末尾,我使用 setAttribute 将它们添加回请求对象:

        request.setAttribute("userName ", userName );           
        request.setAttribute("destFileName", destFileName);
        request.setAttribute("serverName", serverName); 
        request.setAttribute("Message", Message);   

        request.getRequestDispatcher(page).forward(request, response);

And then I get them in my JSP via request.getParameter again:

然后我再次通过 request.getParameter 在我的 JSP 中获取它们:

<%  /** if the parameters are already in place, grab them **/
    String destFileName = request.getParameter("destFileName");
    String user = request.getParameter("user");
    String serverName = request.getParameter("serverName");
    String Message = request.getParameter("Message");           
%>

And this works, unless I've rewrittenthe value on its way through the doPost method. If that is the case, then I have to use request.getAttribute in the JSP to retrieve it since request.getParameter will retrieve the value as it was defined at the top of the doPost method, ignoring any changes that were made between the top and the bottom.

这有效,除非我通过 doPost 方法重写了该值。如果是这种情况,那么我必须在 JSP 中使用 request.getAttribute 来检索它,因为 request.getParameter 将检索在 doPost 方法顶部定义的值,忽略在顶部和底部。

Does anyone have an explanation of this? I've got everything working, but I'd like to understand why I spent a couple of hours of frustration figuring out what was wrong.

有没有人对此有解释?我已经让一切正常工作,但我想了解为什么我花了几个小时的挫败感来弄清楚出了什么问题。

采纳答案by Sotirios Delimanolis

getParameterand getAttributeare completelyunrelated.

getParameter并且getAttribute完全无关的。

getParameter

getParameter

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query stringor posted form data.

以字符串形式返回请求参数的值,如果该参数不存在,则返回 null。请求参数是与请求一起发送的额外信息。对于 HTTP servlet,参数包含在查询字符串或发布的表单数据中。

getAttribute

getAttribute

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

将命名属性的值作为对象返回,如果不存在给定名称的属性,则返回 null。

In other words, returns a value that was set using setAttribute().

换句话说,返回一个使用 设置的值setAttribute()