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 Passing Javabean from JSP to Servlet - null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing Javabean from JSP to Servlet - null" Watch "Passing Javabean from JSP to Servlet - null" New topic
Author

Passing Javabean from JSP to Servlet - null

Peter Bennett
Greenhorn

Joined: Mar 02, 2005
Posts: 4
I am trying to pass values from a JSP form to an action servlet using a Java Bean. I have been reading through somw Wrox and IBM books on how to do this, but they seem to suggest what I have done, which is within the JSP to do this: -

( I have also tried setting the scope to 'request' )
Then in the servlet, I try to pick it up using: -

But the conBean always comes up as a null, I never get the Java bean making it through the request or session.
Everything I've read seems to concentrate on passing a Java bean from a servlet to a JSP, but not this way around. (I think that the idea should be the same though)
Please, point out what I've done wrong?

Thanks.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
No it doesn't work the opposite way.

usebean tag means use the bean which is there in the request or session object, not place the bean in request or session. We can bind a bean object with a session object using session.setAttribute(). But its not nice, unless its really rational to do that.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Actually, the useBean tag will instantiate a bean for you if it doesn't exist and place it into the specified scope. Check out the documentation. Notice the description of "session" scope where it says
The page in which you create the Bean must have a <%@ page %> directive with session=true.
Did you do that?


James Carman, President<br />Carman Consulting, Inc.
Peter Bennett
Greenhorn

Joined: Mar 02, 2005
Posts: 4
I have done now, although the O'Reilly book on Java Server Pages (page 401) says that this is the default.
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
session="true"
%>
The same thing is happening, even after adding this explicit line to the JSP, the bean is still coming back as a null in the servlet.
Any ideas?!?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Can you give us a little background as to what you're doing here? How are you calling the servlet? Are you showing the JSP page (with the useBean tag in it) and then clicking something that executes the servlet? That should work, if that's what you're doing.
Peter Bennett
Greenhorn

Joined: Mar 02, 2005
Posts: 4
Certainly,
it's a rather straight forward JSP presenting an HTML form, which has all the form input fields set bye the repsective Java bean attributes. Such as: -

Which I think is fairly normal, then at the end of the form, I have: -


I know that _ONTACT isn't spelled correctly, but I'm using the servlet name (mapped) to direct the one and only action servlet to the right method, and I already had one starting with a 'C' - since writing that switch statement, I have read about much more elegant ways of writing an action class, but that's what I have at the moment.
The servlet currently does lots of

in a method call to a local stateless session bean (which is a facade to some CMP EJBs.)
Anything else I should be mentioning? (I haven't had to ask this kind of question before, though I suspect you could probably tell...)

Pete.
H Wilson
Ranch Hand

Joined: Feb 23, 2005
Posts: 33


Wow, if the above code is in the doPost() method (or any method called from doPost() with the request parameter passed to it), that should certainly work.

The only thing I can think of is that your deployment descriptor (web.xml) is not set up properly and the server doesn't know that your JSP and Servlet are part of the same app, in which case the session being created in one is not accessible to the other. This idea, however, is a total shot in the dark.

Good luck.
H Wilson
Ranch Hand

Joined: Feb 23, 2005
Posts: 33
Originally posted by Peter Bennett:



I meant to copy the above snipet for my last post. This is what I assume is in the doPost() method of your servlet(?).
Peter Bennett
Greenhorn

Joined: Mar 02, 2005
Posts: 4
Yes, through a switch statement and a method call, from the doPost() method.
Strange, I would have thought that this sort of thing happened quite a lot, given that I can do validation within the bean and it 'feels' like a more elegant way of passing information between view and controller. None of the books (and I have loads, a kind of comfort blanket I think) seem to say anything about doing this from JSP => Servlet, it's all the other way about.
Odd, but that's almost my middle name.
Thanks for the thoughts, replies and lines to investigate though, very helpful, and I'm learning a lot from the investigation. Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Javabean from JSP to Servlet - null
 
Similar Threads
Passing JavaBean from jsp to servlet
Value for the useBean class attribute is invalid -- why?
How can we set a Collection object as attribute to be used by a custom tag?
simple use bean questions
jsp:useBean returns nulls