如何在jsp:setProperty中设置request.getParameter

发布于 2024-11-17 17:31:11 字数 1080 浏览 11 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-11-24 17:31:11

在这里,

<jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
<jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>

您尝试混合scriptlet和标签库。这是无效的。使用其中之一。当 userName 包含像 foo"bar 这样的双引号时,JSP 标记的值基本上会像 value="foo"bar" >。这在语法上是无效的。

由于scriptlet已经死了技术,我建议完全摆脱它。正确的方法是使用 EL。在 EL 中,所有请求参数都可以通过隐式变量 ${param}Map 形式提供。利用它。

<jsp:setProperty name="db" property="userName" value="${param.userName}"/>
<jsp:setProperty name="db" property="password" value="${param.password}"/>

或者,当所有参数名称与属性名称相同时,您也可以让 JSP 自动设置所有属性,如下所示:

<jsp:setProperty name="db" property="*"/>

Here,

<jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
<jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>

you're attempting to mix scriptlets and taglibs. This is invalid. Use the one or the other. When the userName would contain a doublequote like foo"bar then the value of the JSP tag will basically end up like value="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.

<jsp:setProperty name="db" property="userName" value="${param.userName}"/>
<jsp:setProperty name="db" property="password" value="${param.password}"/>

Alternatively, you can also let JSP automagically set all properties as below when all parameter names are the same as property names anyway:

<jsp:setProperty name="db" property="*"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文