Meaningless Drivel is fun!
The moose likes JSP and the fly likes Beans and Jsps and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Beans and Jsps and Servlets" Watch "Beans and Jsps and Servlets" New topic

Beans and Jsps and Servlets

Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
Hello all....
If someone could help me with this it would be GREATLY appreciated.
I have a web application that makes use of beans and jsps and servlets.
My problem is this...
I <usebean> on my initial JSP and have one user input field. My action on the Form is to go to a Process Jsp page which basically does a <set property> and then forwards to a servlet that uses the input field to look up a database record. I then fill up the bean with the setter methods and then forward to a display JSP page.
I am using the getAttribute in the servlet to get the bean and I am using getRequestDispatcher.forward for the JSPs. And when I <usebean> I am using request scope.
Problem is that as I am moving from page to page to servlet etc. I am losing my Bean data.
It only seems to be happening if I need to use two forms. I have page one is a form which processes the info and forwards to page 2 which is another form. When I process, I have already lost my bean and the information from page 1.
I just don't get it!!! I have been researching this all day and it seems like a SIMPLE THING but it is just not working for me. Is my request being lost somewhere???
All I can think of is that when I POST a form I am getting a NEW request at that time???
ANY help on this would be appreciated!!!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Can we see your code? What scope did you give the bean?
Also, check out the naming policy here at the ranch. You need to change your display name to be a first name a space and last name.

Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
thanks for the quick response
I did give the bean request scope.
containerInquiry.jsp -
<jsp:useBean id='ContainerBean' scope='request' class='csp.beans.ContainerBean'/>
<form action='equipment-process-page'>
<input type='text'
value='<%= ContainerBean.getEquipment()%>'>
<input type='submit'>
Which then submits to
equipment-process-page.jsp -
<jsp:useBean id='ContainerBean' scope='request' class='csp.beans.ContainerBean'/>
<jsp:setProperty name='ContainerBean' property='*'/>
<jsp:forward page=''/>
which forwards to a servlet -
ContainerBean containerBean = (ContainerBean)req.getAttribute("ContainerBean");
if(containerBean == null){
containerBean = new ContainerBean();
req.setAttribute("ContainerBean", containerBean);
Servlet also fills up a bunch of fields based on the input from the containerInquiry.jsp
**then forwards to another form page**
servlet.getServletContext().getRequestDispatcher(res.encodeURL(url)).forward(req, res);
entryEquipment.jsp -
At this point its almost like starting the cycle again. This page contains a form that the user enters in data and posts.....gets forwarded to the SAME process page above and the same servlet (which checks some parameters)..
The problem is this time around I need the data that was stored in the bean from the FIRST time around...
I hope I am explaining myself clearly...
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Hey Jennifer
Thanks for fixing your name.
The first thing that jumps out at me is that you'll lose the bean you create in your containerInquiry.jsp page. That bean has request scope so once the response is sent to the browser and the user clicks submit then they are starting a new request. The next two pages forward the request sot he bean would persist but then the entryEquipment.jsp page has a form that the client filss out. This means that a response is sent back to the client and a new request is going to start when they submit the new form.
If you want to make data persist over multiple requests then change the bean's scope to session or use the session state to save info between requests.
Hope that helps
Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
Thanks Dave....
Your help is much appreciated. I am going to change the bean scope to session.
I'll post back if I have any further problems...
I agree. Here's the link:
subject: Beans and Jsps and Servlets
It's not a secret anymore!