views:

327

answers:

1

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();
A: 

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();
BalusC
hey the code above is the add code. where im storing values in the database...ive got one more servlet code that takes values from the database and the flex GUI displays it.this is also working ones...but the next time when i add 1 more value into the database and then display using the same servlet..this servlet is not being invoked..i wanted the reason why is it not invoking again
Gaurav
Apparently the request URL was plain wrong.
BalusC