• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Javabean from JSP to Servlet - null

 
Peter Bennett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter Bennett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic