Solved

request.getAttribute problem in servlet

Posted on 2004-04-22
5
6,020 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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