Solved

request.getAttribute problem in servlet

Posted on 2004-04-22
5
6,015 Views
Last Modified: 2013-12-10
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
Comment
Question by:nkan
5 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 40 total points
ID: 10912523
<%
//------------------------------------------------------------------
// 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
 

Assisted Solution

by:sanjayjay
sanjayjay earned 40 total points
ID: 10995638
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
 
LVL 7

Expert Comment

by:damonf
ID: 11097816
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
This article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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