R – how to get Updated data using same Httpservice in flex

flex3hibernateservlets

I have a flex form which has two httpservice.one which accesses data from the servlet and one which stores data into another servlet.
Firstly when im accessing the data from the servlet that is working and the storing part is also working..so when i again call the access servlet im not getting the updated display..the access servlet is not getting called again..
This is my access servlet code

  public void doPost(HttpServletRequest request,HttpServletResponse response) 
              throws ServletException,IOException 
{ 
PrintWriter out=response.getWriter();

        try
                {
                    response.setContentType("text/html"); 
                    String gradeName=request.getParameter("tx1");
                    System.out.println(gradeName);    
                    gradeName=gradeName.toUpperCase();
                    Session session = HibernateUtil.getSessionFactory().openSession();

                    Transaction tx = session.beginTransaction();
                    Grade g=new Grade(gradeName);
                        session.save(g);
                        tx.commit();

                        session.close();
                        //HibernateUtil.shutdown();
                        out.println("Added Successfully");

                }
                catch(ConstraintViolationException e)
                {
                    out.println("Grade is already Present");
                }   
                catch(Exception e)
                {
                    e.printStackTrace();
                }

}

}

this is my display servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx=session.beginTransaction();
    Query q=session.createQuery("from Grade");
    List l=q.list();

    Grade t;
    PrintWriter out=response.getWriter();
    response.setContentType("text/xml");
    String str="<?xml version=\"1.0\" encoding=\"utf-8\"?><top>";

    for(int i=0;i<l.size();i++)
    {
        t=(Grade)l.get(i);
        str+="<inside><id>"+t.getGradeId()+"</id>";
        str+="<name>"+t.getGradeName()+"</name></inside>";
    }
    str+="</top>";
    out.println(str);
    System.out.println("yattaa->"+str);
    tx.commit();
    session.close();
    HibernateUtil.shutdown();

Best Answer

I am not sure how I have to interpret "which stores data into another servlet". It sounds like you're talking about assigning data as instance variable of a servlet instance. You should never store any request- or session scoped data in a servlet that way. You should use HttpServletRequest#setAttribute() and HttpSession#setAttribute() for this respectively. This beause of the fact that one and same servlet instance is been used during the entire application lifetime, it's been shared among all requests and sessions. That way Visitor X would see data of Visitor Y and you really don't want to have that.

In this particular case, you'd like to share the data among multiple requests inside the same session (I assume that Flex is smart enough to use the same session as the parent JSP/HTML page is using). Then just store it in the session the following way:

request.getSession().setAttribute("data", data);

you can access it in any subsequent requests in the same session the following way:

Data data = (Data) request.getSession().getAttribute();
Related Topic