user1741722
user1741722

Reputation: 49

use of <jsp:useBean> tag

consider i have declared a class as:

public class Calcultor{

public int cube(int n){return n*n;}

}

now are both the below declarations same?

  1. index.jsp:

    <jsp:useBean id=obj class="Calculator"/>
    <%
    int m=obj.cube(5);
    out.println("cube of 5 is "+m)
    %>
    
  2. index.jsp:

    <%
    Calculator obj=new Calculator();
    int m=obj.cube(5);
    out.println("cube of 5 is "+m);
    %>
    

if both are same, what is the advantage?

Upvotes: 1

Views: 3448

Answers (2)

Lluis Martinez
Lluis Martinez

Reputation: 1973

It's not the same. jsp:useBean will locate an instance of Calculator in some scope (request, session ...) that someone has created before (servlet, framework, controller etc.) while the scriptlet creates a new instance.

Check this:

http://docs.oracle.com/javaee/1.3/tutorial/doc/JSPIntro11.html

btw the implementation of cube is wrong, it should be return n*n*n

Upvotes: 3

sam
sam

Reputation: 1

If we use usebean tag it will hide the "java coding" to get the feel of code like html structure..it will give uniform look for the page code...

in the second one is in inside "jsp scriplet" which is used when we want to code a java multiple statements in jsp page(mostly avoided in mvc architecture to write biz logic and view together).... :-)

Upvotes: 0

Related Questions