Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet , bean , jsp Communication

 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Cld any1 tell me the way to set the property of a bean from a servlet, like setproperty inside JSP..
i want to set the value of a bean from a servlet and need to access this bean value from jsp..
is it a right practise to create a staitic instance of bean and using this instance in servlet for setting the bean property and for getting the bean value from jsp ??
thanks
Raj
 
Rajni Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create one javabean which should have set and get methods.
ex: TestBean.java
class TestBean{
int id ;
public void setId(int id){
this.id =id;
}
public int getId(){
return this.id;
}
}
in servlet, create instance of that bean.
TestBean tb = new TestBean();
td.setId(3); // i am setting id as 3 in TestBean.
request.setAttribute("TestBean",tb);
//then forward this servlet to JSP.
in JSP,
after defining page attributes...
don't forget to import ur bean package.
TestBean tb = (TestBean) request.getAttribute("TestBean");
int id = tb.getId();
out.println("id is"+id); // it will print 3 in ur jsp page.
all the best.
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx cuttie,
this is wat i was looking for..thanx one again for ur instant reply..
Raj
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java cute,
Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic