• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet to Bean to JSP problem

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guy

Having a little trouble with Servlets & Benas, wondering if you could help.

I have a simple servlet that lets a users enter some data, the servlet then access' a database and retrieves appropriate data based on what the user entered (basically you put in a code servlet retrieves product info), then the servlet redirects to a JSP which calls the bean and displays the data (still with me?)...

Now I want to put this info into a bean so that it can be retrieved at any time, but I can't for the life of me remember how you create an instance of the bean and populate it from a servlet. I have looked all over and can't find any examples (decent ones anyway).

Any help would be greatly appreciated.

K
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Looking for something like this?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have looked all over and can't find any examples (decent ones anyway).



http://simple.souther.us

SimpleMVC does this.
 
Bob Backlund
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers everyone, that appears to solve my problem.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kenneth",
you used to have a valid display name, now you do not. Please change it back immediately since accounts with invalid display names get deleted.

We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

thanks,
Dave.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having a mare with this JavaBean stuff. The servlet is now storing data in the bean and using System.out I can see that the setField(String s) has a value, but when I redirect to the JSP the getField returns 'null', it's like the bean is loosing the values somewhere.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith,

Just my thoughts, if you are doing a redirect then the control is passed back to the browser which reissues a new request.

Are you storing the bean in the request? If so redirect will mean you get a new request at the jsp and the bean will be created empty.

See paragraph 2 of this link

Javapractices

You could possible use forward or store the bean in the session and declare it as session scope in the jsp.

If youre not using redirect then its some other problem.

HTH Graham

[ February 18, 2005: Message edited by: Graham VMead ]
[ February 18, 2005: Message edited by: Graham VMead ]
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ar yes, i am using redirect. I knew I'd seen the solution somewhere before, but couldn't for the life of me remember it.

I think that was it though

Cheers
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by K Secker:
ar yes, i am using redirect. I knew I'd seen the solution somewhere before, but couldn't for the life of me remember it.

I think that was it though

Cheers



You can either forward with a requestDispatcher instead of redirecting, or store the values in session instead of request scope.

Again, SimpleMVC at http://simple.souther.us does exactly what you're trying to do.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope still didn't do it

code:
RequestDispatcher rd = getServletContext().getRequestDispatcher("success.jsp");
rd.forward(request, response);

The setField value is there but getFeild is 'null', this has got to be so simple I can;t believe I can't do it!

I give up, ain;t worth it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getRequestDispatcher("/success.jsp");
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you look at the example I gave you?
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dunno if you'll read this, but cheers.

Sorry for the delay, I took a couple of days off.

changing the bean scope from session to request done it!!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That'll do it.

Thanks for posting back.
 
reply
    Bookmark Topic Watch Topic
  • New Topic