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

关于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

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值