Solved

Update the value of a bean from a servlet

Posted on 2007-12-02
28
417 Views
Last Modified: 2013-11-24
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
Comment
Question by:VoodooFrog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 9
28 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392677
You need to update the attribute after each setting. btw, the property should be 'welcome' not 'greeting'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392685
'testBean' and 'tester' are also separate objects
0
 
LVL 92

Expert Comment

by:objects
ID: 20392704
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:VoodooFrog
ID: 20392705
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392730
My two comments are connected - i mean that you are creating two separate and unconnected objects
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392736
(whereas you need to be creating just one and working on that instance)
0
 

Author Comment

by:VoodooFrog
ID: 20392755
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
 
LVL 92

Expert Comment

by:objects
ID: 20392766
just add the following to get your session

HttpSession session = request.getSession();
0
 

Author Comment

by:VoodooFrog
ID: 20392768
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
 
LVL 92

Expert Comment

by:objects
ID: 20392779
your bean is fine, you don't need to change it.
What I suggested should resolve your issue for you.
0
 

Author Comment

by:VoodooFrog
ID: 20392792
objects -- adding the HttpSession line gets me a NullPointerException at TestBean tester = (TestBean) session.getAttribute("testBean");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392797
>>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
 

Author Comment

by:VoodooFrog
ID: 20392799
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
 

Author Comment

by:VoodooFrog
ID: 20392806
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
 
LVL 92

Expert Comment

by:objects
ID: 20392809
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
 

Author Comment

by:VoodooFrog
ID: 20392821
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
 
LVL 92

Expert Comment

by:objects
ID: 20392837
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392847
>>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
 

Author Comment

by:VoodooFrog
ID: 20392851
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 20392859
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392863
(Or whatever the original session attribute was called)
0
 
LVL 92

Expert Comment

by:objects
ID: 20392898
> 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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 20392903
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
 

Author Comment

by:VoodooFrog
ID: 20392915
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 20392924
typo in my comment above.
for using request scope the jsp should be:

<jsp:useBean id="testBean" class="bookcellar.TestBean" scope="request" />
0
 

Author Closing Comment

by:VoodooFrog
ID: 31412229
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20392963
>>
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
 
LVL 92

Expert Comment

by:objects
ID: 20393277
> 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question