Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Update the value of a bean from a servlet

I am trying to figure out some servlet/bean usage for redesigning our company web site.  I had thought that it was working before, but apparently I missed something in the process.  I created a bean (code is below) that has a single getter and setter function.  Then I use a servlet to update the value and open a JSP file.  The problem is that the JSP is showing the original value -- not the input value from the servlets update. -- In this example, no matter what I do the JSP page outputs Hello World instead of the new welcome message I put in through the servlet.  
BEAN CODE:
public class TestBean {
	private String greeting = "Hello World";
	public String getWelcome(){		
		return greeting;
	}
	public void setWelcome(String test){
		greeting = test;		
	}
}
 
___________________________________________
 
Servlet Code:
TestBean tester = new TestBean();
			tester.setWelcome("NEW WELCOME");
			request.setAttribute("welcome", tester);
		    RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Sell.jsp");
		    dispatch.forward(request, response);
 
______________________________
JSP Page
 
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>MY TITLE IS: BOOKS</title>
</head>
 
<body>
<jsp:useBean id="testBean" class="bookcellar.TestBean" scope="session" />
<jsp:getProperty name="testBean" property="welcome" />
 
 
</body>
</html>

Open in new window

0
VoodooFrog
Asked:
VoodooFrog
  • 10
  • 9
  • 9
3 Solutions
 
CEHJCommented:
You need to update the attribute after each setting. btw, the property should be 'welcome' not 'greeting'
0
 
CEHJCommented:
'testBean' and 'tester' are also separate objects
0
 
objectsCommented:
you need to get the bean from the session instead of creating a new one.
You may also want to persist the bean depending on your requirements


TestBean tester = (TestBean) session.getAttribute("testBean");
                        tester.setWelcome("NEW WELCOME");
                    RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Sell.jsp");
                    dispatch.forward(request, response);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
VoodooFrogAuthor Commented:
CEHJ -- I am not sure what you mean in your first response -- could you elaborate please?

on the second -- tester is an instantiation of TestBean -- I had thought that beans worked the same as other classes that way?
0
 
CEHJCommented:
My two comments are connected - i mean that you are creating two separate and unconnected objects
0
 
CEHJCommented:
(whereas you need to be creating just one and working on that instance)
0
 
VoodooFrogAuthor Commented:
I still don't understand your point CEHJ -- which part of the code are you talking about?

objects -- the line TestBean tester = (TestBean)session.getAttribute("testBean");  throws an error "session cannot be resolved" -- it looks as though that is combining the request.setAttribute with the bean declaration -- is that allowed?  and if so is that the right syntax?
0
 
objectsCommented:
just add the following to get your session

HttpSession session = request.getSession();
0
 
VoodooFrogAuthor Commented:
CEHJ -- assuming your first comment was about the Bean I changed the code to:

public class TestBean {
      private String welcome = "Hello World";
      public String getWelcome(){            
            return welcome;
      }
      public void setWelcome(String test){
            welcome = test;            
      }
}

nothing changed in the results
0
 
objectsCommented:
your bean is fine, you don't need to change it.
What I suggested should resolve your issue for you.
0
 
VoodooFrogAuthor Commented:
objects -- adding the HttpSession line gets me a NullPointerException at TestBean tester = (TestBean) session.getAttribute("testBean");
0
 
CEHJCommented:
>>I changed the code to:

That's better. It's not the reason for your problem though. That's because you were using two objects when you meant to use one
0
 
VoodooFrogAuthor Commented:
this is also the first instantiation of the bean so far -- I think that may be the problem.  I am just trying to feed a value into the bean and pull it right back out.  -- so the session doesn't have a bean initialized until this code is in place.  The code is in the doGet function if that makes a difference.  
0
 
VoodooFrogAuthor Commented:
CEHJ -- could you show me what you mean by two objects instead of one?  my understanding of it is that I am instantiating TestBean and giving it the name tester after which I run the code from the tester object.  I will need a code illustration apparently because I do not understand what you are saying.  
0
 
objectsCommented:
then change what i posted to:


HttpSession session = request.getSession(true);
TestBean tester = (TestBean) session.getAttribute("testBean");
if (tester==null)
{
   tester = new TestBean();
}
tester.setWelcome("NEW WELCOME");
RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Sell.jsp");
dispatch.forward(request, response);
0
 
VoodooFrogAuthor Commented:
nope -- null pointer is gone, but I am back to the original problem -- the update to the greeting is not being pulled through to the JSP which still displays "Hello World"  
0
 
objectsCommented:
sorry, missed a bit

HttpSession session = request.getSession(true);
TestBean tester = (TestBean) session.getAttribute("testBean");
if (tester==null)
{
   tester = new TestBean();
   session.setAttibute("testBean", tester);
}
tester.setWelcome("NEW WELCOME");
RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Sell.jsp");
dispatch.forward(request, response);
0
 
CEHJCommented:
>>CEHJ -- could you show me what you mean by two objects instead of one?  

You create one object called 'tester' then a new one called 'testBean'
0
 
VoodooFrogAuthor Commented:
still says hello world -- feels like all we have done is change the scope of the bean from request to session scope, but the underlying problem is unchanged.  
0
 
CEHJCommented:
>>the update to the greeting is not being pulled through to the JSP which still displays "Hello World"  

That's because the JSP creates a new object. What you want is

<%=session.getAttribute("tester")%>

in the JSP
0
 
CEHJCommented:
(Or whatever the original session attribute was called)
0
 
objectsCommented:
> still says hello world -- feels like all we have done is change the scope of the bean from request to session scope, but the underlying problem is unchanged.  

your jsp (as posted) does use session scope
check your jsp is as u posted in question
0
 
objectsCommented:
alternatively to use request scope:

TestBean tester = new TestBean();
tester.setWelcome("NEW WELCOME");
request.setAttribute("testBean", tester);
RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Sell.jsp");
dispatch.forward(request, response);
 
______________________________
JSP Page
 
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>MY TITLE IS: BOOKS</title>
</head>
 
<body>
<jsp:useBean id="testBean" class="bookcellar.TestBean" scope="session" />
0
 
VoodooFrogAuthor Commented:
CEHJ -- using <%=session.getAttribute("tester")%> results in a null value for the property -- no error but the JSP just says 'null' at the top.  
0
 
objectsCommented:
typo in my comment above.
for using request scope the jsp should be:

<jsp:useBean id="testBean" class="bookcellar.TestBean" scope="request" />
0
 
VoodooFrogAuthor Commented:
thank you both for your help -- I split the points because in the end I think you were both saying the same thing.  I was able to understand objects more because of his use of code to help me.  
0
 
CEHJCommented:
>>
CEHJ -- using <%=session.getAttribute("tester")%> results in a null value for the property -- no error but the JSP just says 'null' at the top.  
>>

Is that the right attribute name?
0
 
objectsCommented:
> I split the points because in the end I think you were both saying the same thing.

thats ok, but the comment by CEHJ is just plain wrong (and irrelevant, you don't need (or want) to use scriptlets) and will only confuse others with the same problem looking at this question
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now