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 Extracting data from a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Extracting data from a bean" Watch "Extracting data from a bean" New topic
Author

Extracting data from a bean

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys,

I have a Servlet, where I want to extract the firstName from a bean, my code is as follow

The servlet displays 'null' for firstName - ??
My code in the bean is as follow

JSP is as follow


What am I doing wrong? It's all spot on in my eyes
Thanks in advance for your thoughts,

Regards
Zein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

I see no place in your code where you are setting firstName as a scoped variable in the session.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Bear Bibeault thanks for your reply,

Can you please elaborate on that, where do I need to set the varible, in the JSP?? Can you please give me an example

Thanks in advance, Regards

Zein
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys,

I missed a key part of the code that exists in my JSP



Still no idea what the prob is, bleow code still returns null


Regards
Zein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

You seem to have a fundamental misunderstanding about the relation between beans, scoped variables, sessions, and submission parameters.

The only place I see firstName used is as a form element. This means that when submitted to the servlet, it will become a request (submission) parameter which you would obtain via request.getParameter("firstName").

Why would you expect it to be in the session? Why mention the bean at all since it has nothing to do with the form submission?

I'd strongly suggest finding a tutorial or good book on servlet programming before venturing much further.
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Guys,

Bear - must have caught you on a bad day.

If you look here, I have a bean, session scope setting all varibles in my bean.



The bean also have a varible/methods for firstName,



Hence, as the bean has a session scope now, I cant see why I get null in the servlet with the following lines of code



btw request.getParameter("firstName") returns the enetered value in the form, but I want this value to availble to other servlets too without explicity passing, thats why im wanting to use session and not req.getParamtere....

Thanks for your thoughts,

Regards
Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're looking for an attribute named 'firstName' in session scope.
You've never added a 'firstName' attribute to session scope.

session.getAttribute doesn't search all of the beans to find the attribute you're looking for.

If you're using a bean you would want to:
  • Bind the bean to the proper scope
  • Set the bean's first name property
  • Dereference the bean's property from the JSP.



  • [ July 14, 2006: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    And, yes, as you have indicated, the useBean tag will create the bean and bind it to scope for you if you hit the JSP before the servlet.

    Then, from your servlet, you only need to call session.getAttribute("testBean") in order to get a reference to it for setting or getting it's properties.


    If you want a working example of this, the SimpleMVC app at http://simple.souther.us does all of this (with request scope instead of session scope but the concept is the same).
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61606
        
      67

    Originally posted by Zein Nunna:

    Bear - must have caught you on a bad day.


    Not at all -- just pointing out that you have some misconceptions. That's the start of learning to overcome them.
    Samuel Lugo
    Greenhorn

    Joined: Feb 28, 2005
    Posts: 24
    Alright,

    So you have a form, you enter the first and last name, you submit the data to the servlet, and you want the servlet to pick it up from the session...is that correct? I don't know if that's possible. The servlet has to pick up the names as parameters, set them in the bean, and then in the servlet you can put the bean in the session and then pick it up in the JSP that follows from the session, using JSTL if possible.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Samuel Lugo:
    Alright,

    So you have a form, you enter the first and last name, you submit the data to the servlet, and you want the servlet to pick it up from the session...is that correct? I don't know if that's possible. The servlet has to pick up the names as parameters, set them in the bean, and then in the servlet you can put the bean in the session and then pick it up in the JSP that follows from the session, using JSTL if possible.


    No, I think he's posting back to the JSP and using the older <jsp:setProperty /> with the wild card to populate the bean.
    Zein Nunna
    Ranch Hand

    Joined: Mar 31, 2005
    Posts: 245
    Ben / Bear / everybody else,

    Thanks for your input - I see what I was doing wrong.

    Yes I am trying to populate via the wildcard as stated by Ben above.

    The examples you have Ben, are great. I had a look at the sampleMVC, it works on my comp too I see you are using request, to get your values from the form - is that how its meant to be done?

    What I am trying to do is populate the values in the bean (session scope) directly from the JSP, then the servlet/s can access the values from the bean whenever required.

    However the bean doesnt get updated for some reason, the value printed when extracted in the servlet is blank, as set by default in the contsructor of the bean.

    My exact code for the JSP is


    Bean is as follow


    and servlet



    I get the felling its a petty mistake, with a missing colon or something?

    Thanks in advance for your thoughts/input
    Regards
    Zein
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    It's not a petty (or syntatical) mistake; it's a conceptual one.



    To gather form information from a user requires at least two requests; one to build the form and the second to recieve and process the values entered by the user.

    You've got the first part right.
    Your JSP is building the form.
    The problem is that, in order to use the jsp:setProperty tag with the wild card, the form has to post the data TO that page.
    In your case, you're posting the form to TestServlet.

    From your TestServlet, you need to call request.getParameter("firstName") and then use the results to populate your bean with the setFirstName method.

    The jsp:setProperty tag is used mostly in Model1 (non MVC) apps.
    If you're coding servlets to process form requests (a better way to go in my opinion), that tag won't work for you.
    Zein Nunna
    Ranch Hand

    Joined: Mar 31, 2005
    Posts: 245
    Thank you very much Ben, that is a great explanation - it all falls in to place now

    Can you point me into the direction of some coherent tutorials that I could browse through for this sort of application?

    Also in a previous post above your reply to Samuel Lugo was that I'm using the 'old' methodology, whats the new one? JSP 2.0?

    Thank you very much for your thoughts/time

    Regards
    Zein
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    With the newer features in JSP 2.0 (EL and JSTL support) there is no need for the <jsp:xxx /> tags anymore.

    I've heard that "Head First Servlets and JSP" has a section on MVC but havn't read it. I have J2EE patterns by Sun (not sure of the author or publishing company. the book is at the office and I'm not). It's very complete but not so friendly to the beginner.
    Zein Nunna
    Ranch Hand

    Joined: Mar 31, 2005
    Posts: 245
    Cheers Ben, I'll look into it

    Regards
    Zein
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Extracting data from a bean