0

In my jsp, I have the following:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set scope="request" var="test" value="${com.xxx.foo.Bar.getBar()}" />

But this seems to store test as a string with the literal value of ${com.xxx.foo.Bar.getBar()} rather than the return value of that method (which is an enum).

Here getBar() is an instance method, not a static method.

What am I doing wrong?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Ali
  • 261,656
  • 265
  • 575
  • 769
  • If it's an instance method, what instance are you invoking it on? Basically, you don't do this. You can access a property of a bean with `${someBean.bar}`, which EL will use to retrieve an attribute named `someBean` and invoke its `getBar()` method. – Sotirios Delimanolis Mar 08 '14 at 21:16
  • @SotiriosDelimanolis So I'd create an instance using `` and then invoke it on `bar.getBar()`? – Ali Mar 08 '14 at 21:17
  • 2
    The JSP EL is supposed to access properties of Java beans stored in page, request, session or application attributes. Not to invoke static methods of arbitrary classes. The controller should store the test in a request attribute, not the JSP. – JB Nizet Mar 08 '14 at 21:17
  • 2
    No, you can't do that. Write code in Servlets, access attributes in JSPs. And never use Scriptlets. – Sotirios Delimanolis Mar 08 '14 at 21:18
  • @JBNizet I don't have a controller, just the jsp. And its an instance method, not static., – Ali Mar 08 '14 at 21:18
  • And start by going through some of the FAQ in the `jsp` and `el` tag info pages. Most of this is explained in there. – Sotirios Delimanolis Mar 08 '14 at 21:18
  • 1
    If it's an instance method, this code makes even less sense. Even in Java, it wouldn't make sense. The problem is precisely that you don't have a controller. You should. JSPs should be used as view components, which generate HTML, and nothing more. – JB Nizet Mar 08 '14 at 21:19
  • @JBNizet Is it possible to get a return value from another tag and store that as a variable? – Ali Mar 08 '14 at 21:23
  • I don't understand what you mean. – JB Nizet Mar 08 '14 at 21:48
  • @JBNizet Like if you do `` then its replaced by the response from that tag. Can you store that response as a variable? – Ali Mar 08 '14 at 22:06
  • As a page-scope attribute, yes. That's what all the JSTL tags having a var attribute do: `c:set`, `fmt:formatDate`, etc. – JB Nizet Mar 08 '14 at 22:08
  • @JBNizet If I went the servlet way, will I just do what's done in this question: http://stackoverflow.com/questions/6513543/httpservlet-and-jsp-integration ? Will this work on tomcat? – Ali Mar 08 '14 at 22:11
  • 1
    Yes, that's what you should do. And of course it will work on Tomcat. Tomcat implements the servlet and JSP specifications. Servlets can be declared and mapped using an `@WebServlet` annotation starting with servlet 3.0/Tomcat 7. You don't even need a web.xml file anymore. I would start with that, and once you get the idea, invest in learning a real MVC framework like Spring MVC, which provides many more features (bean population from forms, validation, internationalization, custom tags, etc.) – JB Nizet Mar 08 '14 at 22:14
  • @JBNizet Thanks. So If I set an `Enum` value in the servlet via `request.setAttribute("foo", fooEnum)` and forwarded to a jsp, and in the jsp if I included a tag, then will the tag's java class be able to access the value of `fooEnum` by doing `getAttribute` on the `HttpServletRequest`? – Ali Mar 08 '14 at 22:17
  • 1
    Yes. A request attribute is accessible from all the components involved in handling the request. Attributes are used to propagate request-scoped state from component to component. – JB Nizet Mar 08 '14 at 22:19
  • @JBNizet Thanks. If you wanna post a summary of this as an answer, I can accept it. – Ali Mar 08 '14 at 22:20
  • @JBNizet Also, if I included a url parameter when calling my jsp, e.g `myJsp.jsp?foo=bar`, will `request.getAttribute("foo")` have the value `bar` for the jsp? – Ali Mar 08 '14 at 22:22
  • 1
    No. Parameters and attributes are different things. To get parameter values, you use getParameter(). To get attribute values, you use getAttribute(). In the JSP, you would use ${param.foo} to get the value of the foo parameters. But parameters should be handled by the controller, not by the view. And you shouldn't send a request to the JSP, but to the controller. – JB Nizet Mar 08 '14 at 22:26
  • @JBNizet Yeah mate but I'm short on time so just trying to get this over with with least effort. So if I called `foo.jsp?foo=bar`, and in `foo.jsp` I included ``, then in the `doTag()` of `bar` tag, I could access the value of `foo` parameter from url by doing `request.getParameter("foo")`? – Ali Mar 08 '14 at 22:29
  • 1
    Yes, that will work fine. – JB Nizet Mar 08 '14 at 22:34

1 Answers1

0

As suggested by others in the comments, I solved this by creating a servlet and passing in the info to the jsp, like this:

public class FooServlet extends HttpServlet
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
        Bar bar = new Bar();
        request.setAttribute("bar", bar.getFooBar() );
        request.getRequestDispatcher("/myPage.jsp").forward(request, response);
    }
}

In the jsp:

<%=request.getAttribute("bar") %>
Ali
  • 261,656
  • 265
  • 575
  • 769