我正在使用jsp和amp;创建一个Web项目。 jsp页面中的servlet我在网页上播放视频时声明了视频标签然后我使用javaScript获得了播放位置但播放了未在servlet中传递的位置值。 当我运行web项目时,只运行Index.jsp但值不在servlet中传递。
请帮助我们解决此问题
这是我的代码:
的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 :(得分: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");
祝你好运