aspose file tools*
The moose likes JSP and the fly likes PageBean empty when delivered to JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "PageBean empty when delivered to JSP?" Watch "PageBean empty when delivered to JSP?" New topic
Author

PageBean empty when delivered to JSP?

Matt Williard
Greenhorn

Joined: Apr 20, 2012
Posts: 7
Some background: I learned to code core Java in school but they didn't offer any client server classes. I've been dying to learn that aspect but picking up books just doesn't work for my style of learning. At work a developer has been nice enough to teach me the client server side. Being Friday and everyone is out, I'm stuck with a question that is just killing me.

At work we use WebSphere and I was able to create a very basic servlet that passes a POJO to a JSP page. I have an input.jsp page that takes a first name and surname variable. The JSP page sends this to my NameServlet. I also created a POJO (OutputPageBean.java) to hold the name information so that I can have my NameServlet send it to an output.jsp page. I was soo excited about my success at work that I wanted to practice more at home.

Since I don't have access to WebSphere at home, i installed Apache-Tomcat 7 and I use Eclipse. I created the exact same web project from scratch on my home machine, but for some reason the JSP page seems be be receiving an empty POJO from my Servlet. I've tried for several days to resolve the issue and I'm now pulling my hair out. From debugging it I can tell the POJO object has the values at the last step of the doPost() method of my servlet, but my JSP page seems to receive an empty object. I'm pretty sure the JSP page is receiving the object from the NameServlet, but when I try to print the variable to the page, nothing is there.

I can't believe this works in WebSphere but not with Apache :(

Here is my code






Any insight would be much appreciated before I go bald pulling my hair out!

Thanks . . . Matt
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Welcome to the Ranch!

Well, first of all, as you just created this JSP, so we know it's not a legacy page, why are you using the getProperty actions that have been replaced for the past 10 years by modern JSTL and EL?

Sure it should all still work, but why waste time debugging code that looks like it was written in 2001? and using mechanics that should no longer be used on JSP pages?

I think your time would be better spent learning how to write JSP pages using today's technology.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matt Williard
Greenhorn

Joined: Apr 20, 2012
Posts: 7
To be self deprecating and brutally honest, because I'm a noob at this :)

I didn't want to be shown how to do it as much as I wanted to be pushed in the right direction on how to make it work. Making mistakes is a correcting them seems to be a good learning and retention tool for me. So once I got the concept explained I went about finding a solution. When I googled searched for examples, the getProperty actions were at the top of the list. That's the long and short of it.

I'd be more than open to learning a more relevant way . . . albeit the mystery of why my code worked in WebSphere and not in Apache-Tomcat will still keep me awake at night until I can figure it out :(
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Matt Williard wrote:To be self deprecating and brutally honest, because I'm a noob at this :)

No problem. Everyone's a noob at one point!

I didn't want to be shown how to do it as much as I wanted to be pushed in the right direction on how to make it work.

Perfect. You'll like it here, cause that's just how we roll.

When I googled searched for examples, the getProperty actions were at the top of the list. That's the long and short of it.

Yes, that will happen a lot. The vast majority of the web community seems to have not gotten the memo that JSP 2 was introduced in 2002 and are still writing JSP pages as if it were 1998. At least you aren't using any Java scriptlets in your pages!

I'd be more than open to learning a more relevant way

The modern way would be to use the EL (Expression Language) to access bean properties, and the JSTL (JSP Standard Tag Library) to perform actions like conditional and iteration.

albeit the mystery of why my code worked in WebSphere and not in Apache-Tomcat will still keep me awake at night until I can figure it out :(

Understood. And you can pursue that if it'll help you sleep better. On a brief inspection, I don't see anything wrong (but again, that's stuff I haven't used in over 10 years so I may be missing something).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

I also recommend that newcomers to JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  • Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Found your problem! You are placing the scoped variable in session scope but looking for it in request scope.
    Matt Williard
    Greenhorn

    Joined: Apr 20, 2012
    Posts: 7
    Bear Bibeault wrote:Found your problem! You are placing the scoped variable in session scope but looking for it in request scope.


    Do you mean in this section of the output.jsp



    Where scope= should be "session"?

    If so, I tried that with the same results. Always the output.jsp page returns Your name is .

    Actually I used the scope="session" first and when that didn't work I started to experiment with other scope variables. I copied a piece of code where I forgot to change it back to session, but the results were the same in either case.

    When I debug and the debugger gets to the <jsp:useBean id="pageBean" class="matt.OutputPageBean" scope="request"> line of the output.jsp, it skips the break points for the two jsp:getProperty lines and moves right to the closing </jsp:useBean> tag. I'm not sure what that means.

    Thanks again for your help!

    . . . Matt
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Matt Williard wrote:Where scope= should be "session"?

    Using session scope is probably not the right approach. Unless there's a reason that the scoped variable needs to have a lifetime beyond the current request, request scope should be used. That means placing the scoped variable into request scope in the controller, and retrieving it from the same scope in which it was placed.

    In either case, you have to fetch the variable from the same scope in which it was placed. You can't get underwear out of the sock drawer!

    When I debug and the debugger gets to the <jsp:useBean id="pageBean" class="matt.OutputPageBean" scope="request"> line of the output.jsp, it skips the break points for the two jsp:getProperty lines and moves right to the closing </jsp:useBean> tag. I'm not sure what that means.

    The embedded tags will only be evaluated if the bean is newly created, which is not the case here.

    P.S. Once you start using the EL, this all becomes a lot easier.
    Matt Williard
    Greenhorn

    Joined: Apr 20, 2012
    Posts: 7
    The embedded tags will only be evaluated if the bean is newly created, which is not the case here.

    P.S. Once you start using the EL, this all becomes a lot easier.


    I finally figure this out! Yes, it did take me from the time I read this last post until now before I figured it out and yes I feel dumb. I think you told me the solution above and I just wasn't reading it correctly.



    The problem with the above code is that the <jsp:getProperty> tags are inside the <jsp:useBean> tags. Once I moved them outside of those tags to look like this (see below), it worked perfect !



    Thanks for all your help, I can now sleep peacefully :)

    . . . Matt
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: PageBean empty when delivered to JSP?