• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet , bean , jsp Communication

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx cuttie,
this is wat i was looking for..thanx one again for ur instant reply..
Raj
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic