如何在flex中使用相同的Httpservice获取更新的数据

发布于 2024-08-19 23:27:29 字数 2300 浏览 4 评论 0原文

我有一个 Flex 表单,其中有两个 httpservice.one,它从 servlet 访问数据,另一个将数据存储到另一个 servlet 中。 首先,当我从正在工作的 servlet 访问数据并且存储部分也在工作时。因此,当我再次调用访问 servlet 时,我没有得到更新的显示。访问 servlet 不会再次被调用。 这是我的访问 servlet 代码

  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();
                }

}

}

这是我的显示 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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2024-08-26 23:27:29

我不确定如何解释“将数据存储到另一个 servlet 中”。听起来您正在谈论将数据分配为 servlet 实例的实例变量。您绝对不应该以这种方式将任何请求或会话范围的数据存储在 servlet 中。您应该分别使用 HttpServletRequest#setAttribute() 和 HttpSession#setAttribute() 来实现此目的。这是因为在整个应用程序生命周期中都使用同一个 Servlet 实例,因此它在所有请求和会话之间共享。这样,访问者 X 就会看到访问者 Y 的数据,而您确实不想看到这些数据。

在这种特殊情况下,您希望在同一会话内的多个请求之间共享数据(我假设 Flex 足够聪明,可以使用与父 JSP/HTML 页面所使用的同一会话) 。然后只需按以下方式将其存储在会话中:

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

您可以按以下方式在同一会话中的任何后续请求中访问它:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文