need an advice about getAttribute()

Discussions

Web tier: servlets, JSP, Web frameworks: need an advice about getAttribute()

  1. need an advice about getAttribute() (2 messages)

    hi,
    i've done a jsp file with a form. this form, ince submited calls a servlet that makes a sql query to a database, and there puts the results of the query in an object.
    then i forward the object to the jsp this way :


    ResponseObj mObject = new ResponseObj();
    Myconnclass.search(sYear,sCurrency, mObject); //here is the class that makes the query and writes the results in my object
    request.setAttribute("myObj", mObject); //to set the attribute
    RequestDispatcher dispatcher =
    getServletContext ().getRequestDispatcher("/myWebApp/myJSP.jsp"); //to send it to the jsp

    try { dispatcher.forward(request,response); }

    catch(ServletException se) { System.out.println("servlet exception : " + se.getMessage()); }

    catch(IOException io) { System.out.println("IO exception : " + io.getMessage()); }

    then i must get this parameter in the jsp.
    i think it should be something like :

    ResponseObj rObj =(ResponseObj) request.getAttribute("myObj");
    this should give me back the object with the results.

    here is the problem :
    i do use the same jsp file (for clicking the button that submits the query and for displaying the result in the web page.), in fact it is like a "refresh" of the web page with the new results.

    when i launch the page the first time, before to have cliked on the button, the attribute myObj doesn't exixt because it is created after the query. so if i put this code in my jsp :
    <%= rObj.aVariable %>
    of course this object will only exist when the query is done, and so i do have a display error.

    so what could i do ?
  2. need an advice about getAttribute()[ Go to top ]

    Just test whether the object is null before using it.
    <% RequestObj obj = (RequestObj)request.getParameter("reqObj"); %>
    <% if(obj != null) { %>
        <h1><%= obj.getValue() %></h1>
        <h1><%= obj.getName() %></h1>
    <% } <%>
  3. need an advice about getAttribute()[ Go to top ]

    thanks !