Solved

request.getAttribute problem in servlet

Posted on 2004-04-22
5
6,013 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now