Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why use <jsp:useBean>?  RSS feed

 
Billy Vandory
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is <jsp:useBean better than simply instantiating it in the page as you would in java? seems to me <jsp:useBean, <jsp:getProperty, etc. is way more typing than

<%
bean = new Bean();
bean.getWhatever();
bean.setWhatever("whatever");
%>

Can someone tell me where my gap is? (other than between the ears..)

thanks,

Billy
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java scriptlets in JSPs are an artifact of JSP 1.x that have been discredited since the inception of JSP 2.0 over 8 years ago. Modern best practices prohibit the use of Java code in JSPs.

That said, even <jsp:useBean> is less used than you might think. While it is sometimes appropriate to instantiate a display-support bean in a JSP view under certain circumstances, beans are usually created in the page controller for the JSP and "passed" to the JSP as request-scoped variables.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a JSP 2.0 app useBean isn't used much anymore. EL will find your scoped variables for you.


But, to answer your question, useBean does more than the scriptlet you posted.

UseBean, if a class attribute is present, will check to see if there is already an instance in the specified scope (or request by default).
If it is there then it will return a reference to that object with the variable that you enter in the id attribute.
If it isn't present, it will instantiate it, set any properties that you specify in the body, bind it to the scope specified (or request by default) and then return the reference to that object via the variable named in the id attribute.

If you don't specify a concrete class but instead specify a type, then it will only look for the object in the specified scope and return the reference, it won't instanciate it.

There is a link to the JSP Spec in my signature.
The section on useBean is short and easy to read. You might want to go through it.
 
Billy Vandory
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Java scriptlets in JSPs are an artifact of JSP 1.x that have been discredited since the inception of JSP 2.0 over 8 years ago. Modern best practices prohibit the use of Java code in JSPs.

That said, even <jsp:useBean> is less used than you might think. While it is sometimes appropriate to instantiate a display-support bean in a JSP view under certain circumstances, beans are usually created in the page controller for the JSP and "passed" to the JSP as request-scoped variables.


Fascinating. I'm reading head first servlets and haven't gotten to the EL part yet.. Thanks, you helped me to avoid a potentially embarassing situation.

Billy.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. It's important to understand scriptlets and their ilk as you are likely to run across legacy code that uses them, but for new code, they should be eschewed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!