java - Difference between the two methods of creating class objects in JSP -
i'm learning jsp came across 2 methods of creating class object in jsp
method 1:
<jsp:usebean id = "obj" class = "classname" /> <% obj.method(); %>
method 2:
<% classname obj = new classname(); obj.method(); %>
- can 1 please explain difference?
- which practice?
- in context method 1/method 2 relevant , not
thank in advance
method 1:
the <jsp:usebean>
standard action element used locate or instantiate javabean component. firstly <jsp: usebean>
tries locate instance of bean class if found fine, if not instantiate class mentioned in class
attribute. default object in page scope if scope
attribute not specified.the name of bean same have given in id
attribute of <jsp:usebean>
. if object reference doesn't exist name have specify create instance , find scope of variable, class
attributes defines bean class , type
attribute defines parent class or interface of bean class.
you can access bean style properties of java bean using <jsp:usebean>
:
<jsp:usebean id = "obj" class = "classname" /> <jsp:getproperty name="obj" property="someproperty"/>
the above code looks java bean stored obj
in page
, if doesn't finds 1 tries create new bean using class definition specified class
attribute , sets newly created bean page
scope. try access someproperty
attribute of obj
bean . someproperty
attribute should have bean style getter. go through oracle tutorial more.
method 2 :
you creating local object within service()
method of generated servlet. not setting scope.
note:
scriptlets , <jsp:usebean>
not advisable . please use jstl , el expressions.
Comments
Post a Comment