码迷,mamicode.com
首页 > Web开发 > 详细

JS 定时提交 以及 保持在网页存在的时候session不失效的小技巧

时间:2017-02-15 19:00:37      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:type   choice   class   import   eth   void   parameter   encoding   时间   

1 setInterval(function(){
2     
3     $.post("HlsView",{choice:‘time‘,url_name:"",rtsp:"",ZSflg:""}, function(data) {
4 //这里面处理返回的数据 也就是data
5     }, "json");
6     
7 },60000);   //延时60秒  单位是毫秒级别的

上面是JS代码,中间的提交方法是JQ的 提交   HlsView 指的是要提交到的类,但这个类必须是继承了HttpServlet 这个类的 我们在post方法里面处理提交过来的数据(我这里使用最简单的JSP+servlet的方法,并没有使用框架)

比如

 1 import java.io.IOException;               
 2 import java.text.ParseException;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import javax.servlet.http.HttpSession;
 8 
 9 
10 public class HlsView extends HttpServlet {
11     private static final long serialVersionUID = 1L;
12 
13     protected void doGet(HttpServletRequest request,
14             HttpServletResponse response) throws ServletException {
15 
16     }
17 
18     protected void doPost(HttpServletRequest request,
19             HttpServletResponse response) throws IOException {
20     
21         request.setCharacterEncoding("utf-8");
22         response.setCharacterEncoding("UTF-8");
23         // 设置头文件
24         response.setContentType("text/json;charset=UTF-8");
25         response.setHeader("Pragma", "No-cache");
26         response.setHeader("Cache-Control", "no-cache");
27         response.setDateHeader("Expires", 0);
28 
29             String choice = request.getParameter("choice");
30             String bianhao = request.getParameter("url_name");
31             String rtsp = request.getParameter("rtsp");
32             String ZSflg = request.getParameter("ZSflg");// 有 add、delate
33             String Cname = request.getParameter("Cname"); 
34 
35         //接下来就可以想要怎么处理这些数据了
36 
37     }
38 
39 }

 

保持session在网页没有关闭的时候用就不失效的办法  

     在web.xml 文件里面设置session的有效时间,(里面的时间单位是按照分钟算的)

 

 <session-config>  
    <session-timeout>2</session-timeout>  
</session-config>  

上面设置的有效时间是两分钟,所以我们只需要在两分钟以内提交一次网页就可以保值session在网页没有关闭的时候不会失效了

定是提交的方法一开始就说了的哈

需要注意的是 要记得在你需要提交的页面引入JQuery哟

JS 定时提交 以及 保持在网页存在的时候session不失效的小技巧

标签:type   choice   class   import   eth   void   parameter   encoding   时间   

原文地址:http://www.cnblogs.com/Vziiii/p/6402535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!