Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP EL ${stuff} syntax not working

Tags:

java

jsp

el

I have two problems. The first one is that I'm using JSP and that I can't solve. The second one is that I'm getting an odd behavior.

When I put this in the doGet() method of my servlet

req.setAttribute("test", "SARASA");
req.getRequestDispatcher("WEB-INF/main.jsp").forward(req, resp);

And this in "WEB-INF/main.jsp":

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%= request.getAttribute("test") %>
<c:out value="${test}"/>

The output is:

SARASA ${test}

I don't know what I'm doing wrong... what can be the reason for this?


Update: I solved it adding

<%@ page isELIgnored="false" %>

In each JSP where I needed it. Oddly, I didn't needed that in another project using some very similar web.xml and pom.xml files (I'm using maven).

like image 287
eordano Avatar asked Sep 10 '11 21:09

eordano


People also ask

What is ${ in JSP?

jsp:useBean is a standard element that creates an object containing a collection of locales and initializes an identifier that points to that object. JSP expression language expressions ( ${ } ) retrieve the value of object properties. The values are used to set custom tag attribute values and create dynamic content.

Which of the following is correct syntax for expression language EL in JSP?

Simple Syntax The most common operators in JSP EL are . and []. These two operators allow you to access various attributes of Java Beans and built-in JSP objects. When the JSP compiler sees the ${} form in an attribute, it generates code to evaluate the expression and substitues the value of expresson.

What is the syntax of JSP expression?

The JSP Expression tag transforms the code into an expression statement that converts into a value in the form of a string object and inserts into the implicit output object.

What is isELIgnored attribute and its default value in JSP?

The isELIgnored attribute gives you the ability to disable the evaluation of Expression Language (EL) expressions which has been introduced in JSP 2.0. The default value of the attribute is true, meaning that expressions, ${...}, are evaluated as dictated by the JSP specification.


1 Answers

Your web.xml is probably referencing the Servlet 2.3 spec, in which isELIgnored is set to true by default. If you reference the Servlet 2.4 spec instead, isELIgnored will be set to false by default.

If you want to reference the Servlet 2.4 spec, your web.xml header should look something like this:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
like image 151
Mansoor Siddiqui Avatar answered Oct 21 '22 04:10

Mansoor Siddiqui