2017-08-29 25 views
0

我在jsp頁面中使用jsp創建web項目&我在網頁上播放視頻時聲明視頻標記,然後使用javaScript獲取播放位置,但播放位置值未在servlet中傳遞。 當我運行web項目時,只運行Index.jsp,但值不傳遞給servlet。如何在servlet中播放視頻時獲取javaScript變量值

請幫助我們解決這個問題

這是我的代碼:

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 


    <html> 
    <body> 

     <video id="myVideo" width="320" height="176" controls> 
     <source src="mov_bbb.mp4" type="video/mp4"> 
     <source src="mov_bbb.ogg" type="video/ogg"> 
     Your browser does not support HTML5 video. 
    </video> 
    <p>Playback position: <span id="demo"></span></p> 

    <script> 
    // Get the video element with id="myVideo" 
    var vid = document.getElementById("myVideo"); 


    vid.ontimeupdate = function() {myFunction()}; 

    function myFunction() { 
    // Display the current position of the video in a <p> element with id="demo" 
     var time=document.getElementById("demo").innerHTML = vid.currentTime; 
     $.ajax({ 
      url:'http://localhost:8084/demotime/NewServlet', 
      data:{ 
       myPost:time 
      }, 
      type:'POST' 
     }); 
    } 

    </script> 

    <p>Video courtesy of <a href="https://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p> 

    </body> 
    </html> 

這是Servlet的文件

package servlet; 

    import java.io.IOException; 
    import java.io.PrintWriter; 
    import javax.servlet.ServletException; 
    import javax.servlet.annotation.WebServlet; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import javax.servlet.http.HttpSession; 

    /** 
    * 
    * @author Administrator 
    */ 
    @WebServlet(name ="NewServlet") 
    public class NewServlet extends HttpServlet { 

     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
       throws ServletException, IOException { 
      PrintWriter out = response.getWriter(); 
      try { 
       HttpSession session = request.getSession(); 
       String time=""; 
       if(time!=null){ 
        time=request.getParameter("myPost"); 
        out.print(time); 
       }else{ 
        System.out.println("time is null"); 
       } 
      } catch (Exception e) { 
      } 
    { 

      } 
     } 


    } 

這是web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <servlet> 
     <servlet-name>NewServlet</servlet-name> 
     <servlet-class>servlet.NewServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>NewServlet</servlet-name> 
     <url-pattern>/NewServlet</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
     </welcome-file-list> 
</web-app> 
+0

你爲什麼不嘗試用http:// localhost:8084/demotime/NewServlet?myPost =「+ time替換'url:'http:// localhost:8084/demotime/NewServlet'。我的意思是與URL傳遞時間一起作爲查詢參數,其中mypost是參數,您可以從servlet訪問 –

回答

0

如果是單參數,你可以嘗試通過作爲查詢參數如下圖所示

JS:

var time=document.getElementById("demo").innerHTML = vid.currentTime; 
$.ajax({ 
    url:'http://localhost:8084/demotime/NewServlet? myPost'+time, 
    data:{ 
     myPost:time 
    }, 
    type:'POST' 
}); 

的Servlet:

String time=request.getParameter("myPost"); 

祝你好運

+0

,但sir值仍然相同 –

+0

我想在沒有任何事件的情況下傳遞javaScript變量值 –

+0

視頻播放時如何獲取視頻播放時間 –

相關問題