File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet , bean , jsp  Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet , bean , jsp  Communication" Watch "servlet , bean , jsp  Communication" New topic
Author

servlet , bean , jsp Communication

Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
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


SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Rajni Kumar
Greenhorn

Joined: Mar 05, 2003
Posts: 8
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

Joined: Aug 27, 2002
Posts: 455
thanx cuttie,
this is wat i was looking for..thanx one again for ur instant reply..
Raj
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet , bean , jsp Communication