Scope of jsp:useBean

TheLameProgrammer picture TheLameProgrammer · Dec 6, 2009 · Viewed 15.3k times · Source

home.jsp

<jsp:useBean id="username" class="java.lang.String" scope="application"/>

<%
      username="Jitendra";
%>

<jsp:include page="include.jsp"/>

include.jsp

<%=username%>

This gives an error saying “username” is undefined in include.jsp, even though the scope of Bean is application…

Answer

BalusC picture BalusC · Dec 6, 2009

As to your problem, anything which you declare locally using the old fashioned scriptlets is not linked with a jsp:useBean. Also, declaring a local scriptlet variable is not visible in the included pages, you need to explicitly put them in at least the request scope. As using scriptlets is a bad practice. I recommend to forget about it at all.

In your specific case, just create a real java bean to hold the data. That is, a class with an (implicit) default constructor and private properties which are exposed by public getters/setters. Here's a basic example:

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Then you can use a servlet class to preprocess requests. You can use servlet's doGet() method for this.

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    User user = new User();
    user.setName("Jitendra");
    request.setAttribute("user", user); // Store in request scope.
    request.getRequestDispatcher("/WEB-INF/show.jsp").forward(request, response);
}

Map this servlet in web.xml on an url-pattern of for example /show. This servlet should then be accessible by http://example.com/context/show and its doGet() would be executed immediately.

Then change/create the JSP file show.jsp which you place in /WEB-INF to prevent from direct access (so that clients cannot access it by http://example.com/context/show.jsp but are "forced" to call the servlet) with the following line:

<p>User name: ${user.name}</p>

The ${user} refers to the object which is associated with any request/session/application attribute key user. This does behind the scenes jspContext.findAttribute("user"). As the returned User instance conforms the javabean spec, the ${user.name} will invoke the getName() method on the User instance and the EL will display its outcome.

Oh, I should add, you do not need jsp:useBean for this as the servlet has already created and put the desired bean in the scope.

That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:

Hope this helps.