Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Bean Instantiation in JSP

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in ProductDefinitionHome.jsp I have a bean defined as --> <jsp:useBean id="productDefBean" scope="request" class="com.ubs.pdb.data.bean.ProductDefBean" />
I have one variable flag in ProductDefBean; default its value is false.
So initially when my ProductDefinitionHome.jsp getting loaded i can see value of my flag as 'false'. thats fine.
When I am clicking 'Ok' button on ProductDefinitionHome.jsp; I am calling ProductDefinitionHome.java where I am setting flag=true.

After that I am redirecting to ProductDefinitionHome.jsp however the value of flag is again = false

the <jsp:useBean id="productDefBean" scope="request" class="com.ubs.pdb.data.bean.ProductDefBean" /> is instantiating bean?
any solution to this?

JAVA code
request.setAttribute("ProductDefBean", objProductDefBean);
request.getRequestDispatcher("/ProductDefinitionHome.jsp").forward(request, response);

JSP code

<c:out value='${requestScope.productDefBean.flag}' />
 
Sheriff
Posts: 67636
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't be instantiating beans in the JSP -- that's the job of the controller.
 
SachinJ Joshi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You shouldn't be instantiating beans in the JSP -- that's the job of the controller.



Doese
<jsp:useBean id="productDefBean"
scope="session"
class="com.ubs.pdb.data.bean.ProductDefBean" />
instantiates ProductDefBean

How can i use ProductDefBean in JSP without useBean Tag?
is it correct?
<%

ProductDefBean objProductDefBean = (ProductDefBean) request.getAttribute("ProductDefBean");

%>
 
Bear Bibeault
Sheriff
Posts: 67636
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not correct. Both scriptlets and useBean are obsolete. The correct way to get beans into a JSP page is to instantiate them within the controller, and add them as scoped variables to request scope (or session scope if appropriate) with setAttribute(). A bean should never be created within the JSP itself.

Once in a scope, the scoped variable can be referenced via the EL without the need for useBean or getAttribute() calls.

The purpose of a JSP is to create the view -- not to create beans or perform processing. The controller(s) should be doing all the heavy lifting.

Please read this article for an overview on proper web application structure.
 
SachinJ Joshi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No, it is not correct. Both scriptlets and useBean are obsolete. The correct way to get beans into a JSP page is to instantiate them within the controller, and add them as scoped variables to request scope (or session scope if appropriate) with setAttribute(). A bean should never be created within the JSP itself.

Once in a scope, the scoped variable can be referenced via the EL without the need for useBean or getAttribute() calls.

The purpose of a JSP is to create the view -- not to create beans or perform processing. The controller(s) should be doing all the heavy lifting.

Please read this article for an overview on proper web application structure.



Absolutely correct.
So in my java class I am doing

ProductDefBean objProductDefBean = new ProductDefBean();
objProductDefBean.setFlag(true);

request.setAttribute("ProductDefBean", objProductDefBean);
request.getRequestDispatcher("/ProductDefinitionHome.jsp").forward(request, response);

Now i want to print 'flag' variable in my ProductDefinitionHome.jsp like --> <c:out value='${objProductDefBean.flag}' />
How to get the bean in JSP without useBean and getAttribute tags?





 
Bear Bibeault
Sheriff
Posts: 67636
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SachinJ Joshi wrote:How to get the bean in JSP without useBean and getAttribute tags?


It's already there! You don't need to do anything else. Creating the scoped variable with getAttribute() made it available in request scope.

The problem is evident by comparing these two lines of code:

If you name your dog Rover, do think he'll come running to you when you call him Fido?

You gave the scoped variable the name ProductDefBean and then didn't use it. (By the way, like all variables, it should start with a lowercase letter.)

So calling "Here Fido! Here Fido!" will not cause Rover to answer.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic