• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bean Instantiation in JSP  RSS feed

 
SachinJ Joshi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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

<cut value='${requestScope.productDefBean.flag}' />
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!