Solved

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

Posted on 2004-09-06
6
1,475 Views
Last Modified: 2013-11-24
Hi

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="http://java.sun.com/jstl/core" prefix="c" %>

<c:set var="myvar">
  <%=test.MainServlet.NAME%>
</c:set>

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

<html>

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


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)?
0
Comment
Question by:boomerang061797
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 50 total points
Comment Utility
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 />
</jsp:useBean>

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.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
I think it's not important whatever the name is because it's used locally in jsp file.
0
 

Author Comment

by:boomerang061797
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
It's left value. It cannot change after your jsp translate to servlet code.
0
 

Author Comment

by:boomerang061797
Comment Utility
Ok - so what you're saying is that the id can never be a EL expression or a scriptlet?
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
Yes. That's right.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help refactoring a java code 8 53
scores100 challenge 3 82
array6 challenfge 6 62
stringclean challenge 26 54
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now