如何在flex中使用相同的Httpservice获取更新的数据
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定如何解释“将数据存储到另一个 servlet 中”。听起来您正在谈论将数据分配为 servlet 实例的实例变量。您绝对不应该以这种方式将任何请求或会话范围的数据存储在 servlet 中。您应该分别使用 HttpServletRequest#setAttribute() 和 HttpSession#setAttribute() 来实现此目的。这是因为在整个应用程序生命周期中都使用同一个 Servlet 实例,因此它在所有请求和会话之间共享。这样,访问者 X 就会看到访问者 Y 的数据,而您确实不想看到这些数据。
在这种特殊情况下,您希望在同一会话内的多个请求之间共享数据(我假设 Flex 足够聪明,可以使用与父 JSP/HTML 页面所使用的同一会话) 。然后只需按以下方式将其存储在会话中:
您可以按以下方式在同一会话中的任何后续请求中访问它:
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()
andHttpSession#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:
you can access it in any subsequent requests in the same session the following way: