package controller;
import java.io.IOException;
import java.util.ArrayList;
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 com.google.gson.Gson;
import com.sun.corba.se.spi.orbutil.fsm.Action;
import bean.Video;
import dao.Dao;
import daoimpl.daoImpl;
public class DisplayVideo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Dao dao=new daoImpl();
String action=request.getParameter("action");
if(action.equals("display")){
ArrayList<Video>al=dao.getAllVideo();
//PrintWriter pw=response.getWriter();
Gson gson=new Gson();
String json = gson.toJson(al);
System.out.println(json);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
}
}
&#13;
这是我的servlet,我以json格式获取数据。
<html>
<head>
<script type="text/javascript" src='js/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
alert("on document ready");
$('#video').click(function(event) {
alert("on click");
$.post("Controller?action=display",
function (data) {
alert("key"+data);
var json = data;
alert("value"+json);
$.each(json, function(key,value) {
alert("key1"+value);
var v ="<a href=${pageContext.request.contextPath}/videos/"+value.videoUrl+"target='blank'>"+value.videoUrl+"</a>";
$(v).appendTo($content);
});
});
});
});
</script>
</head>
<body>
<div id='content'>
<button id="video">Play</button>
</div>
</body></html>
&#13;
这个我的sucess.jsp文件从这里控制转到servlet并从数据库获取json格式的数据我只保存了数据库中的视频名c.mp4,java.mp4和cpp.mp4我也创建了一个名为videos的文件夹在我的项目中的webcontent文件夹中,也保存在该文件夹中的视频。响应得到正确,但在每个打印警报后,他们在屏幕上没有任何打印。我要求打印所有视频名称和链接的onclick播放视频在iframe或in弹出窗口。请帮助我,我不知道使用的标签。谢谢提前。