• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to retreive bean object in servlet with request scope

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will be better to post your code.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any updates???
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com;

public class RegistrationActionForm{

String userName="";

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

}
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers, Any updates???
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen,

In your servlet change this rd = req.getRequestDispatcher("/confirmation.jsp"); and then try and let me know.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Any updates? Can moderators please intervene???

Thanks,

Naveen
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what was the bug ?
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic