关于JSP中使用EL表达式无法取出request.setAttribute中设置的值

本文介绍了在JSP中遇到EL表达式无法从request.setAttribute获取值的问题,问题出现在WEB-INF目录下web.xml的dtd版本过低。解决方法包括升级web.xml的版本到3.1,并在jsp页面指令中设置isELIgnored为false。通过这两个步骤,可以成功解决EL表达式取值异常的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于JSP中使用EL表达式无法取出request.setAttribute中设置的值

首先看代码

LoginServlet.java

String result="";    
if (loginCustomer!=null) {
    request.getRequestDispatcher("/index.jsp").forward(request, response);
}else {
    result = "用户名或密码错误";
    request.setAttribute("result",result);
    request.getRequestDispatcher("/login.jsp").forward(request, response);
}

验证用户名密码是否正确,如果正确内部跳转主页,如果不正确将result保存到request域中

在登录页面login.jsp中

<script type="text/javascript">	
    //使用EL表达式取到result值
    var result = '${result}';
    //如果result不为空
    if(result){
    	alert(result);
    }
</script>

如果发现EL表达式无法取到result的值,在弹窗中显示

${result}

则有可能为WEB-INF目录下的web.xml中dtd版本过低

解决方式有两种:

  1. 修改web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    </web-app>
    
  2. 在jsp页面指令中添加

    isELIgnored="false"
    

    添加到

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    

    问题解决

    over

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值