File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Unable to retreive bean object in servlet with request scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Unable to retreive bean object in servlet with request scope" Watch "Unable to retreive bean object in servlet with request scope" New topic
Author

Unable to retreive bean object in servlet with request scope

Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Greeting's Ranchers,

I'm trying to implement a small MVC application.
I created a RegistrationActionForm in my jsp with the scope as request:
<jsp:useBean id="registrationInfo" class="com.RegistrationActionForm" scope="request" />

Now in the RegistrationServlet when I'm trying to retreive the bean object created in this jsp by calling
RegistrationActionForm frm = (RegistrationActionForm) req.getAttribute("registrationInfo");

I'm getting frm value as null.

On the other hand, If I set the bean with scope="application" or "session" instead of "request" and reterive the registrationInfo from the servlet as

RegistrationActionForm frm = (RegistrationActionForm) getServletContext().getAttribute("registrationInfo");

I'm able to retain the bean object.

Can anyone tell me why this is happening?

Thanks,

Naveen
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
it will be better to post your code.
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Hi,

Sorry for the delayed reply, well the code snippet is given below. In this code, I'm not able to access the userName in the confirmation.jsp if i use request scope. If i use context level scope, I'm able to retreive the username:


========================== Servlet code ==========================

public class RegistrationServlet extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

RegistrationActionForm actionForm = new RegistrationActionForm();
RegistrationAction registrationAction = new RegistrationAction();

String userName=req.getParameter("userName");
actionForm.setUserName(userName);
req.setAttribute("registrationInfo", actionForm);
String status = //Execute the business action
RequestDispatcher rd = null;
if (status.equalsIgnoreCase("success")){
rd = req.getRequestDispatcher("confirmation.jsp");
rd.forward(req, resp);
}

}

}


========================== confirmation.jsp code ==========================

<body>
<jsp:useBean id="registrationInfo" class="com.RegistrationActionForm" scope="application" />

<p align="center">Congrats
<jsp:getProperty name="registrationInfo" property= "userName" />
</body>
[ January 16, 2007: Message edited by: Naveen Sharma ]
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Any updates???
Tha�s Manfrim Firmino
Ranch Hand

Joined: Apr 12, 2004
Posts: 48
how did you go from confirmation.jsp code to the servlet?
you don't say how you linked one for each other. In order to access it from inside the jsp, the atribute has to be in the same request, what I believe is not happening.


Tha�s M. K. M. Firmino
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Hi Thais,

I'm not going from the confrimation page to the servlet, I'm in the servlet and from there i'm going to the confimation.jsp

The servlet name is RegistrationServlet, In this servlet I have created a bean object named RegistrationActionForm and set its userName property. Now I'm setting the bean object in HttpServletRequest object as a registrationInfo attribute. After this i'm dispatching the request to the confirmation.jsp. confirmation.jsp needs to display the value from the RegistrationActionForm bean.

========================== Servlet code ==========================

public class RegistrationServlet extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

RegistrationActionForm actionForm = new RegistrationActionForm();
RegistrationAction registrationAction = new RegistrationAction();

String userName=req.getParameter("userName");
actionForm.setUserName(userName);
req.setAttribute("registrationInfo", actionForm);
String status = //Execute the business action
RequestDispatcher rd = null;
if (status.equalsIgnoreCase("success")){
rd = req.getRequestDispatcher("confirmation.jsp");
rd.forward(req, resp);
}

}

}


========================== confirmation.jsp code ==========================

<body>
<jsp:useBean id="registrationInfo" class="com.RegistrationActionForm" scope="application" />

<p align="center">Congrats
<jsp:getProperty name="registrationInfo" property= "userName" />
</body>
Suchen Chodankar
Ranch Hand

Joined: Sep 18, 2006
Posts: 47
Hi naveen,

Can you post code which you have written for com.RegistrationActionForm Class.?

Because to access properties of your RegistrationActionForm Class using <jsp:getProperty ...... /> your Class should follow JavaBean specification.

so it would be easier for us to figure what exactly is happening in your application if you could post your RegistrationActionForm Class code.
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
package com;

public class RegistrationActionForm{

String userName="";

public void setUserName(String userName){
this.userName = userName;
}
public String getUserName(){
return userName;
}

}
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Hi Suchen,

I have added the form bean code above. Btw, the same code is working fie when I use the application scope in the jsp.

Thanks,
Naveen
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Hi Ranchers, Any updates???
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Naveen,

In your servlet change this rd = req.getRequestDispatcher("/confirmation.jsp"); and then try and let me know.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
But my confirmation.jsp is not lying at the context root. Should I change the location of confirmation.jsp to the root? Will it help?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Ok, I thought it is lying in the context root. I don't think that putting this in the context root will help. Even I'm baffled with this!
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
I hope you tried this on your machine as well, if context level attributes are available to all the resources in the web application, so we can set and then get the form bean anywhere we like, if you see the confirmation_jsp.java the servlet code is also try to getAttribute the registrationInfoId.

Does that mean I'm not able to undustand the concept right?
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
Hey,

Any updates? Can moderators please intervene???

Thanks,

Naveen
Manish Agarwal
Greenhorn

Joined: Jan 30, 2006
Posts: 20
Hi Naveen,

In your first post you have mentioned that you are setting the attribute in jsp and then trying to retrieve it in Servlet, while in the code that you have provided later you are trying to set attribute in Servlet and retrieve it in jsp.

One issue that I found in your later code is as follows:

In the servlet you are adding the attribute in request.
*************************
actionForm.setUserName(userName);
req.setAttribute("registrationInfo", actionForm);
String status = //Execute the business action
*************************

while in jsp you are using scope as "application"
*************************
<jsp:useBean id="registrationInfo" class="com.RegistrationActionForm" scope="application" />
*************************
That might be the reason you are not getting results.

However,if you are doing as you have mentioned in your first post then you might be setting attribute in one request and then retrieving it in next request, that is why you are not able to retrieve it in request scope and its working fine in other scopes.

Thanks,
Manish


SCWCD1.4, SCJP1.4, OCA 9i
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hey,

I have created a Servlet by the name of RegistrationServlet and created RegistrationForm bean in it and set the userName property and added it in request scope, then i forwarded the request to confirmation page. On confirmation page i located the bean in request scope and displayed the userName property and its working fine. Here is the code

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
so what was the bug ?


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
I think the problem was that he was setting the bean in request scope in Servlet whereas was trying to get it from Application scope.



As scope is specified as application, it will search the bean in application scope and will create a new empty one if it doesn't find any.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to retreive bean object in servlet with request scope