• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6023
  • Last Modified:

request.getAttribute problem in servlet

I am using a a.jsp, on form submit, it will post to a servet bServlet.java, while I found that I couldn't get the the request object in jsp by request.getAttribute() in the servlet

in a.jsp:
<jsp:useBean id="fBean" class="govi.FormBean" scope="request"/>
<jsp:setProperty name="fBean" property="*" />
...
<FORM .... action="bServlet" method="POST>
...
----------------------------------------------------------------------------------
then in bServlet:
public void doPost (HttpServletRequest request,
       HttpServletResponse response) {

try {
    FormBean f = (FormBean) request.getAttribute ("fBean");
    f.setName("Mogambo");
    // do whatever else necessary
   getServletConfig().getServletContext().
         getRequestDispatcher("/jsp/Bean2.jsp").
             forward(request, response);
      } catch (Exception ex) {
            . . .
      }
}
But i found that the request.getAttribute ("fBean") will return a NULL object.

Any Idea? Thanks a lot.
0
nkan
Asked:
nkan
2 Solutions
 
rama_krishna580Commented:
<%
//------------------------------------------------------------------
// NOTE: You must cast the Object returned from getAttribute to the
// appropriate real class you used in "setAttribute":
//------------------------------------------------------------------
String strParamAValue = (String)request.getAttribute("a");
%>



R.K

0
 
sanjayjayCommented:
This should answer your problem


<jsp:useBean id="formHandler" class="foo.FormBean" scope="request">
    <jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>


The <jsp:useBean> tag first looks for the bean instance with the specified name, and instantiates a new one only if it cannot find the bean instance within the specified scope. Here, the scope attribute specifies the lifetime of the bean. Newly instantiated beans have page scope by default, if nothing is specified. Observe that in this case, I specify that the bean have request scope before a response is sent back to the client, since more than one JSP is involved in processing the client request.

You may be wondering about the <jsp:setProperty> within the body of the <jsp:useBean> tag. Any scriptlet or <jsp:setProperty> tags present within the body of a <jsp:useBean> tag are executed only when the bean is instantiated, and are used to initialize the bean's properties. Of course, in this case I could have placed the <jsp:setProperty> tag on the outside of the <jsp:useBean>'s body. The difference between the two is that the contents of the body are not executed if the bean is retrieved from the specified scope --
0
 
damonfCommented:
If you set the bean to request scope, then it expires as soon as the JSP page loads.  When the user then submits to the server, the bean is null again so you won't be able to get the attribute from request scope.  If you want to keep it, set to session scope.  Or write out the value you want to a hidden field and access using request.getParameter
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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