如何在jsp:setProperty中设置request.getParameter [英] How to set request.getParameter in jsp:setProperty

查看:221
本文介绍了如何在jsp:setProperty中设置request.getParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用bean/表单处理在登录屏幕上获取输入参数,然后使用这些参数尝试将用户登录到应用程序中.

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.

但是我遇到错误-

org.apache.jasper.JasperException:/loginbean.jsp(6,59)属性值request.getParameter("userName")引用为,在val中使用时必须转义

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

出现此错误的代码行是下面给出的代码块中的第二行- (即名称为"userName"的属性的代码行)

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

推荐答案

在这里,

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

您正在尝试混合使用 scriptlets 和taglib.这是无效的.使用一个或另一个.当userName将包含一个双引号,例如foo"bar时,那么JSP标记的值基本上将以value="foo"bar"结束.这在语法上是无效的.

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.

由于 scriptlets dead 技术,我建议您完全摆脱它.正确的方法是使用 EL .在EL中,所有请求参数都可以通过隐式变量${param}作为Map<String, String>使用.利用它.

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}"/>

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

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="*"/>

这篇关于如何在jsp:setProperty中设置request.getParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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