views:

349

answers:

3

I have a project which works fine. bookstore example I didn't understand where it initialize JSP's variable? There is no beans. All we have is Java class. JSP calls java class's variable but how? I searched into all the configuration files but nothing found.

public class Bookstore {    
    private final Map<String, Item> items = new TreeMap<String, Item>();

    private String name;

    public Bookstore() {
        setName("Otel Sheriton");
    }

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

here is my jsp file:

.....
<body>

    <h1>${it.name}</h1>
 ......

</body>

${it.name} means: get variable 'name' from one of the Objects of BookStore
So, how and where can i identify ${it}

+1  A: 

If you put code in a scriptlet (the older <% %> tags), they are translated by the JSP compiler into the _jspService() method. This method is invoked whenever the JSP page is accessed, either directly by the client, or internally within the container using a server-side forward or include.

The _jspService() method cannot be overridden. The JSP compiler creates the body of the service method in the same order as the code appears in the JSP page. Therefore, the variables are created as local variables and are initialized every time the service method is invoked.

In cases where you use the older <%! %> tags or the newer jsp:declaration tags to define methods, the variables are created and initialized when the declared method is executed.

By the way, all implicit variables will be initialized by the container.

Additional details of this behavior can be found in the JSP Specification, and in the JspPage javadoc.

If you want to know how the JspPage and the HttpJspPage class is initialized and used by the container, you will have to go through the container source code. However, the JSP specification (chapter 11 on the JSP container contract) defines how the JSP container initializes and creates the JSP page. Usually initialization of the page class itself is done once; at this point jspInit() is invoked. When the page is destroyed, usually on application or container shutdown, the jspDestroy() method is invoked before the page is destroyed.

Update

EL expressions eventually find the bean, by attempting to resolve the variable in the page, request, session and application scopes. The variable to be used should have been declared and initialized in the appropriate scope. JavaBean components can be initialized using the jsp:useBean tag. Other objects should have been declared and initialized in a servlet or in a scriptlet.

Vineet Reynolds
Declaring variables in JSP using the <%! %> tags can cause you problems since they are not thread safe. Your JSP gets transformed into a Servlet which is shared among clients and this declaration does not create a local variable in _jspService() but a field in the Servlet object.
dpb
Yes, declaring variables in <%! %> tags is a bad practice.
Vineet Reynolds
Can you give an example. If i have class book and String author is the variable of that class then how can i initialize this author to auth to use into jsp
Iguramu
I think you should provide more details. It is not clear from your comment, and from your question, what exactly you are trying to do. Edit your question to be more specific, maybe post some code. You just want to initialize some object’s data to later display it in a JSP, or are you interested in specific details of how your application does what it does?
dpb
+1  A: 

${it.name} means get property name from the object that can be found as a named attribute it.

This can be found in your request for example, or in your session. This basically translates as

((Bookstore) pageContext.findAttribute("it")).getName()

This searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

So you have to place the object in the desired scope before you go to your JSP, something like

request.setAttribute("it", bookStoreObj)

I am assuming you use MVC, so it is the controller’s job to place this in scope before selecting the JSP.

dpb
A: 

JSP as you know gets compiled into a servlet. Your variables defined in JSP will be coming into the compiled servlet class. You should check the documentation of the server you are using and see the compiled servlet class. If its a .class file you can decompile and check.

Rajat