如何在jsp:setProperty中设置request.getParameter
我使用 beans/form 处理在登录屏幕上获取输入参数,然后使用这些参数尝试将用户登录到应用程序中。
但是我收到一个错误-
org.apache.jasper.JasperException: /loginbean.jsp(6,59) 属性值 request.getParameter("userName") 用 " 引起来,在 val 中使用时必须转义
出现此错误的代码行是下面给出的代码块中的第二行- (即 name='userName' 属性的代码行)
loginbean.jsp
<jsp:useBean id="db" scope="request" class="logbean.LoginBean" >
<jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
<jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>
</jsp:useBean>
LoginBean.java
package logbean;
public class LoginBean {
String userName="";
String password="";
public String getUserName() {
return userName;
}
public void setUsername(String username) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I used beans/form processing to take input parameters on login screen and then with those parameters try and log the user into the application.
However I am getting an error-
org.apache.jasper.JasperException: /loginbean.jsp(6,59) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the val
The line of code which has this error is the second line in the block of code given below-
(ie line of code for the property with name='userName')
loginbean.jsp
<jsp:useBean id="db" scope="request" class="logbean.LoginBean" >
<jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
<jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>
</jsp:useBean>
LoginBean.java
package logbean;
public class LoginBean {
String userName="";
String password="";
public String getUserName() {
return userName;
}
public void setUsername(String username) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,
您尝试混合scriptlet和标签库。这是无效的。使用其中之一。当
userName
包含像foo"bar
这样的双引号时,JSP 标记的值基本上会像value="foo"bar"
>。这在语法上是无效的。由于scriptlet已经死了技术,我建议完全摆脱它。正确的方法是使用 EL。在 EL 中,所有请求参数都可以通过隐式变量
${param}
以Map
形式提供。利用它。或者,当所有参数名称与属性名称相同时,您也可以让 JSP 自动设置所有属性,如下所示:
Here,
you're attempting to mix scriptlets and taglibs. This is invalid. Use the one or the other. When the
userName
would contain a doublequote likefoo"bar
then the value of the JSP tag will basically end up likevalue="foo"bar"
. This is syntactically invalid.Since scriptlets is a dead technology, I'd suggest to just get rid of it altogether. The proper way would be to use EL. In EL, all request parameters are available as a
Map<String, String>
through the implicit variable${param}
. Make use of it.Alternatively, you can also let JSP automagically set all properties as below when all parameter names are the same as property names anyway: