This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Request.getParameter(String) will return only request parameter values that were either posted to the request via a form submission, or were specified in the query string. There is no way it could return the property of a bean. If you are getting a value from calling getParameter, it's the result of a submitted value To answer the original question, if the bean is set as an attribute on the request, you can get a reference to the bean using getAttribute (casting the returned Object as the bean type), and then calling accessors on the bean. hth, bear
Joined: Sep 14, 2002
When I try getAttribute(), they come back as null. Here is my indexBean:
Here is my index.jsp:
And here is my loginAction servlet:
Joined: Jul 28, 2001
your bean is in session scope. so, to get it run getAttribute on your session object.
Your servlet is not actually accessing the fields of your bean by using getAttribute().
To access the form fields, use getParameter() as you described before, but this does NOT access the bean either.
To access variables you have placed into the attribute space of request, session or application object, use the getAttribute() method of the object you used setAttribute() on. In your example, you never use setAttribute() on any object *explicitly*, so that's why you are getting nulls. There is no attribute called firstName. There is though, a parameter of that name, based on your form submission, which is why your example used to work. But you were never getting it from the bean.
Javabeans *implicitly* set the instance of the javabean object into whatever scope you specify in the scope parameter of jsp:useBean
But to use it in a servlet would be a two-step process. First retrieve the bean from the proper scope...