[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

request.getAttribute problem in servlet

Posted on 2004-04-22
5
Medium Priority
?
6,022 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 160 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 160 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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