JS 定時提交 以及 保持在網頁存在的時候session不失效的小技巧


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喲


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM