This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a simple JSP/Servlet combo that allows a user to enter details into text fields in a JSP, on submitting a POST the Servlet creates an instance of a JavBean to store the details then redirects to another JSP which displays the details (with me so far?) Problem is when you get to the second JSP the values are set to 'null', now if you insert a System.out in the servlet to display the values stored in the JavaBean, you get the correct data (taken from the 1st JSP) but the 2nd JSP gets a null value. Basically somewhere between the servlet populating the fields in the JavaBean and redirecting to the 2nd JSP the values seem to be lost. I have the <jsp:useBean id="details" class="javasrc.beans.detailsBean" scope="page" /> in my 2nd JSP and the Servlet is definately populating the values in the JavaBean, so I can't for the life of me see whay the 2nd JSP is displying 'null' (<%= details.getFname() %> Please can anyone tell me what I'm leaving out? (I got a feeling it's gonna turn out to be something stupidly obvious).
Expounding: Firstly, after you create your bean in the servlet, how are you attaching it to the page context? There's no pageContext in the servlet. What is most common, is to create the bean in the servlet, attach it to the request (using request.setAttribute()), forward to the page, and retrieve the bean from request context on the page. Note 1: if no bean is found in the specified context, the useBean tag creates a fresh instance (this is why you are seeing all those nulls). Note 2: you must forward rather than redirect, since a redirect causes the current request to end and a new one to be created. Note 3: if you must redirect (rather than forward), you need to store the bean in either session or application scope for it to "survive" the transition. hth, bear
Joined: Jun 05, 2003
Wow cool. Three replies in less than a day, and all look useful. Many thanks guys. KPS