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>
<cut 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
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>
<cut 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