Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP - UseBean

 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following
<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean"/>
<jsp:setProperty name="myBean" property="myProperty" value="<%=request.getParameter("sentProperty")%>"/>


MyBean.java

Package com.mypackage;

Public class MyBean
{
private String myProperty;
public MyBean(String myProperty)
{
this.myProperty= myProperty;
}
public void setMyProperty(String myProperty)
{
this. MyProperty= myProperty;
}
public String getMyProperty()
{
return myProperty;
}
}


Will this give a compilation/translation error? (Beans cannot have constructors with arguments)
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you try it out?

it should give an error, not because beans cannot have constructors with arguments, but because beans SHOULD have a no-arg constuctor or what will the container call when it needs to instantiate it?
 
Vivek Kinra
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Akshay...

A bean class must have a zero-argument (empty) constructor.You can satisfy this requirement either by explicitly
defining such a constructor or by omitting all constructors, which
results in an empty constructor being created automatically.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic