Paul
Paul

Reputation: 608

passing object from servlet to jsp and getting output with jstl

I am expirementing with jstl. I place an object (bean) onto req in my servlet as follows:

req.setAttribute("myBean", myBean);

In my jsp I had:

MyBean mb = (MyBean)request.getAttribute("myBean");
<%= mb.getStuff() %>

Then I tried to use Jstl as follows:

<c:out value="${mb.getStuff}"/>

And I get the literal ${mb.getstuff} in the output. It is wrong. How should it be.

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>

Tomcat 6. Eclipse.

This is from sample on web:

request.setAttribute("name", "ss ss");
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);

<h3>An Example of c:out JSTL...</h3><br/>
The value comes from servlet is : <b> <c:out value="${name}"/></b>

Evaluates to blank.

Upvotes: 1

Views: 2790

Answers (1)

Dave Newton
Dave Newton

Reputation: 160261

There are two (potentially three) issues.

1. The servlet level declared in your web.xml should be at 2.5+

<web-app version="2.5" xmlns="java.sun.com/xml/ns/javaee"
  xmlns:xsi="w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

2. Request attributes aren't the same as scriptlet variables; the scriptlet variable you've created is invisible to JSP EL, which access scoped attributes, not scriptlet vars. The correct EL would be:

${myBean.getStuff}

3. If getStuff is actually a method getStuff() then the EL would actually be:

${myBean.stuff}

Also, make sure the app was redeployed, and the changes have been picked up and compiled.

Upvotes: 2

Related Questions