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

  • Thread starter Lee Francis Wilhelmsen
  • Start date
L

Lee Francis Wilhelmsen

Hi

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>
<c:eek:ut value="${myvar}" />
<br />

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 in useBean using JSTL/EL? Is
there a better way (but not using scriptlets)?

Regards
Lee Francis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top