• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Bean from JSP  RSS feed

 
Kumar Shroff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to JSP/Servlets or I can say new to Java.
I wrote a AForm.html, A.jsp, AHandler.java, ABean.java
user fills in data on AForm.html (it has 2 fields say val1, val2)
these values are passed to AHandler.java as method="GET"
now in AHandler.java i am doing sth like below:

// please ignore syntax...
doGet(req, res){
val1 = req.getParameter("val1");
val2 = req.getParameter("val2");
HttpSession session = request.getSession(true);
//Now instantiate ABean
ABean myBean = new ABean();
//Now populate Bean values
myBean.setVal1(val1);
myBean.setVal2(val2);
//Add the ABean object to the session
session.setAttribute("myBean", myBean);
dispatch(req, res);
}
dispatch(req, res) {
req.getRequestDispatcher("A.jsp").forward(req,res);
}
--------
Now in A.jsp

<jsp:usebean id="myBean" class="packegename.ABean" scope="session" />
<H1>
Value of first variable = <%= myBean.getVal1() %>
Value of second variable = <%= myBean.getVal2() %>
---------

in my A.jsp, i am getting null values for myBean.getVal1 and myBean.getVal2.
May be in A.jsp, it is creating new instance for ABean instead of looking for already created instance.

Can anyone help me out in retrieving the values of already instantiated bean from A.jsp?
Thanks in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't see anything blatant after just a quick look, but your code is hard to read since you did not use the UBB CODE tags (see the CODE button under the text entry area) to preserves the formatting of the code. (Be sure to use these tags in the future).

But, why are you using session scope rather than request scope?
[ December 05, 2005: Message edited by: Bear Bibeault ]
 
Kumar Shroff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request or session does'nt anything would do for me.
I just wanted to know how to retrieve already instantiated bean (instantiated in a servlet) from JSP file
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good to me to.
What does AForm.html look like?

Try putting this on A.jsp:


If those are null, then you need to find out why.

Check the names of the fields on the input form.
Are you using method="get" or method="post" on your html form? You have only implemented get in your servlet.

Also check your implementation of setVal1 and setVal2 to see what they do.
 
Kumar Shroff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did check using val1 = <%= request.getParameter("val1") %>
and it works (val1 is not null). But my requirement is to use <jsp:usebean /> and retrieve the value.

setVal1 and setVal2 are just setter methods in ABean.java.
and Iam using method = "GET" not post.

I would appreciate if anyone can help me out in retrieving the values from <jsp:usebean />

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kumar Shroff:
But my requirement is to use <jsp:usebean /> and retrieve the value.


Stefan recommended looking at the parameters as the first obvious debugging step to make sure that you are not putting null into the bean in the 1st place, not as a suggestion to change your requirements

Originally posted by Kumar Shroff:
I would appreciate if anyone can help me out in retrieving the values from <jsp:usebean />


That's what we are trying to do. I brought up the request vs. session question is not only because it's bad form to use the session when the request is more approriate, but switching to using request scope would help eliminate session-maintenance problems as a possible cause.

If you are not willing to help debug your own code there's not much we can do for you here.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the parameters are going through, but they are not being set in the bean successfully.

My next wild stab in the dark is that it is caused by your set method using the same name for the parameter and the attribute, and not using the "this" keyword.

ie



If that is not it, then sprinkle a few System.out.println() statements through your code. Its still one the most effective debugging tools I know.
Check the values as they are retrieved/set. Put one in the constructor of ABean so you can see if/when it is instantiated - once or twice?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where are you declaring val1 and val2?

Can you post the actual Java code for the servlet?
As Bear instructed, use the UBB Code tags.
Put as much effort in presenting your code as you would like to see us put into helping you with it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!