Can <jsp:useBean> use an EL variable in id attribute?

Posted on 2004-09-06
Last Modified: 2013-11-24

I'm no expert at either JSP or JSTL, but I'm trying to make my JSP scriptlet free.

Anyway, I'm trying to send a javabean from a servlet to a jsp page using the MVC pattern. The problem I'm having is on the JSP page. I'm running servlet version 2.3, JSP 1.2 and JSTL 1.0 on Tomcat 4.1.30.

I have a servlet that looks something like this:

package test;


public class MainServlet extends HttpServlet {
    public static final String NAME = "beanname";

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    StringBean bean = new StringBean();
    bean.setText("Servlet set text");
    req.setAttribute(NAME, bean);
    RequestDispatcher d = req.getRequestDispatcher("/Main.jsp");
    d.forward(req, resp);

Notice the public static javabean name constant. I want to use this constant in my JSP, and that's my problem.

In my JSP I do something like this:

<%@ taglib uri="" prefix="c" %>

<c:set var="myvar">

<jsp:useBean id="${myvar}" class="test.StringBean" scope="request">
  For debugging: Bean created in jsp<br />


Text: <jsp:getProperty name="${myvar}" property="text" />

The trouble is that the compiler useBean's id attribute won't accept the EL variable. I get

    JavaCompile: Syntax error on token "{", ";" expected.

How can I refer to the Servlet constant name in useBean using JSTL/EL? Is there a better way (but not using scriptlets)?
Question by:boomerang061797
  • 4
  • 2
LVL 11

Accepted Solution

sompol_kiatkamolchai earned 50 total points
ID: 11988364
It does not accept run-time expression. since it has to pre-compile to servlet.

If you define
<jsp:useBean id="${myvar}" class="test.StringBean" scope="request">
  For debugging: Bean created in jsp<br />

when servlet container compiler, how servlet container declare variable????

test.StringBean ${myvar} = new test.StringBean();

It is left-value, So it cannot run-time value.
LVL 11

Expert Comment

ID: 11988374
I think it's not important whatever the name is because it's used locally in jsp file.

Author Comment

ID: 11991038
Yes, the name isn't really important however I was trying to have the servlet be in charge of naming the bean. I the servlet should want to change the variable name then I wanted the JSP code to automatically be updated to use the new name.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 11991101
It's left value. It cannot change after your jsp translate to servlet code.

Author Comment

ID: 11994795
Ok - so what you're saying is that the id can never be a EL expression or a scriptlet?
LVL 11

Expert Comment

ID: 11994823
Yes. That's right.

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 172
looking for unix program to search some keywords in side EAR WAR and JAR files 10 111
hibernate example using maven 12 82
program arguments vs VM arguments 4 31
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question