• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1526
  • Last Modified:

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

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
boomerang061797
Asked:
boomerang061797
  • 4
  • 2
1 Solution
 
sompol_kiatkamolchaiCommented:
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
 
sompol_kiatkamolchaiCommented:
I think it's not important whatever the name is because it's used locally in jsp file.
0
 
boomerang061797Author Commented:
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
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!

 
sompol_kiatkamolchaiCommented:
It's left value. It cannot change after your jsp translate to servlet code.
0
 
boomerang061797Author Commented:
Ok - so what you're saying is that the id can never be a EL expression or a scriptlet?
0
 
sompol_kiatkamolchaiCommented:
Yes. That's right.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now