It's not a secret anymore!
The moose likes Servlets and the fly likes Accessing javabeans values in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing javabeans values in servlet" Watch "Accessing javabeans values in servlet" New topic

Accessing javabeans values in servlet

Amtul Schwab,

Joined: Dec 07, 2004
Posts: 10
can i access the jaav bean fields in a servlet?
I am making an application that have one bean,2 servlet n 2 jsps..
the architecture goes like this:
JSP calls sevlet which makes beans and forward them to another JSP.
On this second jsp the user selects the data to be store in db and click save when this save button is clicked JSP transfer control to another servlet which have to save data in db.
Now the problem is that i do not know how to get the data that was shown in jsp via bean's getProperty method.
plz help.
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
You could do any of the following to get access to the bean's info in your servlet:

1) Store the bean in session scope and get it in the servlet with

2) Store the bean state in form fields in the jsp and then submit the form to the servlet and get the data out of the request like so

The problem with this method is that it makes you "Stringify" all of your bean state

3) Make a link in your jsp page that has the beans data as request parameters like so

This method also requires you to "Stringify" your bean state
[ December 08, 2004: Message edited by: Anthony Watson ]

Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
M. Gagnon
Ranch Hand

Joined: Feb 18, 2004
Posts: 68
Originally posted by Anthony Watson:
Store the bean in session scope and get it in the servlet with

Don't forget that you can store the bean in scopes other than session. If your bean has application scope, you'll need to get it from the ServletContext. For example:

YourClass yourClassInstance = (YourClass)getServletContext().getAttribute("yourBeanInstance");
I agree. Here's the link:
subject: Accessing javabeans values in servlet
It's not a secret anymore!