我的jsp没有显示从servlet发送的参数

时间:2022-03-15 11:53:29

I would like to ask you a question please. Can Servlet parameters caontain a -?

我想问你一个问题。 Servlet参数可以caontain a - ?

Because, I tried to retrieve the parameters from the request of the doPost in my servlet but the result is not what I'm looking for.

因为,我试图从我的servlet中的doPost请求中检索参数,但结果不是我正在寻找的。

In my servlet I have this :

在我的servlet中我有这个:

 HttpSession session = request.getSession(true);
    if (session != null) {

        String X_USER = request.getParameter("X-User");
        request.setAttribute("X-User", X_USER);

        String YYYY_YYYY = request.getParameter("Y_CODE");
        request.setAttribute("Y-Code", YYYY_YYYY);
    }

The JSP in wich I give the values I did this:

我给出了这个值的JSP:

 <li><label for="X_USER">Login</label> <input type="text"
        name="X-User"></li>
    <li><label for="Y_CODE">Code</label> <input
        type="text" name="Y-Code"></li>

and in my second JSP where I want to show these parameters so I do this:

在我的第二个JSP中,我想显示这些参数,所以我这样做:

  <li>${X-User}</li>

    <li>${Y-Code}</li>

The problem is that I'm getting 0 instead of the parameter value.

问题是我得到0而不是参数值。

2 个解决方案

#1


3  

Your concrete problem is not the HTTP request parameter, but the EL variable name. All Java keywords and identifiers are illegal in EL variable names.

您的具体问题不是HTTP请求参数,而是EL变量名称。所有Java关键字和标识符在EL变量名中都是非法的。

The - is a subtraction operator in Java (and also in EL). How should the EL processor know if you meant to use an attribute with the literal name X-User or the result of an integer subtraction of ${User} from ${X}? It's by specification interpreted as latter which also explains the numeric result of 0.

- 是Java中的减法运算符(也是EL中的减法运算符)。 EL处理器应该如何知道您是否打算使用文字名称为X-User的属性或$ {User}中$ {User}的整数减法结果?它由规范解释为后者,也解释了0的数值结果。

Just use underscore or camelcase instead, like as in normal Java.

只需使用下划线或camelcase,就像在普通Java中一样。

request.setAttribute("Y_User", X_USER);
request.setAttribute("Y_Code", Y_CODE);
${X_User}
${Y_Code}

If you absolutely need to access a request attribute containing a hyphen in EL, then use the brace notationon the request scope map:

如果您绝对需要在EL中访问包含连字符的请求属性,请在请求范围映射中使用括号表示法:

${requestScope['X-User']}
${requestScope['Y-User']}

The same applies to request parameters, by the way, which you don't necessarily need to copy over into the request scope in the servlet:

顺便说一句,这同样适用于请求参数,您不一定需要将其复制到servlet中的请求范围中:

${param['X-User']}
${param['Y-Code']}

See also:

#2


0  

You can use implicit object of jsp as

您可以使用jsp的隐式对象作为

response.getAttribute("X-User");
response.getAttribute("Y-Code");

As you're setting attribute from servlet there is no need to get it via jstl.

当您从servlet设置属性时,无需通过jstl获取它。

#1


3  

Your concrete problem is not the HTTP request parameter, but the EL variable name. All Java keywords and identifiers are illegal in EL variable names.

您的具体问题不是HTTP请求参数,而是EL变量名称。所有Java关键字和标识符在EL变量名中都是非法的。

The - is a subtraction operator in Java (and also in EL). How should the EL processor know if you meant to use an attribute with the literal name X-User or the result of an integer subtraction of ${User} from ${X}? It's by specification interpreted as latter which also explains the numeric result of 0.

- 是Java中的减法运算符(也是EL中的减法运算符)。 EL处理器应该如何知道您是否打算使用文字名称为X-User的属性或$ {User}中$ {User}的整数减法结果?它由规范解释为后者,也解释了0的数值结果。

Just use underscore or camelcase instead, like as in normal Java.

只需使用下划线或camelcase,就像在普通Java中一样。

request.setAttribute("Y_User", X_USER);
request.setAttribute("Y_Code", Y_CODE);
${X_User}
${Y_Code}

If you absolutely need to access a request attribute containing a hyphen in EL, then use the brace notationon the request scope map:

如果您绝对需要在EL中访问包含连字符的请求属性,请在请求范围映射中使用括号表示法:

${requestScope['X-User']}
${requestScope['Y-User']}

The same applies to request parameters, by the way, which you don't necessarily need to copy over into the request scope in the servlet:

顺便说一句,这同样适用于请求参数,您不一定需要将其复制到servlet中的请求范围中:

${param['X-User']}
${param['Y-Code']}

See also:

#2


0  

You can use implicit object of jsp as

您可以使用jsp的隐式对象作为

response.getAttribute("X-User");
response.getAttribute("Y-Code");

As you're setting attribute from servlet there is no need to get it via jstl.

当您从servlet设置属性时,无需通过jstl获取它。