HTML 5 Video Tag does not work with Tomcat (Edited)

Video cannot be played from Tomcat 7 server using absolute path and relative file path:

Edit Resume: I changed the example for the relative path, and I place the video inside the Root-Folder application (still unable to resolve the video error).

We are creating a small video viewing application for local use. Since HTML-5 gives us tremendous support for watching videos, we decided to write a program using a simple servlet / JSP, deploy it on a Tomcat 7 web server .

The application logic is as follows:

  • The root path (Absolute path) is set to the root folder of my application in tomcat.
  • All files and directories inside the root are displayed along with the "go" button.
  • If a video file in the folder is selected by clicking "go", the video viewing page will appear.
  • Regarding the video file, a video page is provided using Expression langauge (EL) to the video tag source.
  • Video should play from the local hard drive to all browser endpoints.

The problem that I encountered is that my video does not play from the tomcat server, but the same html source code displayed in the browser when copying and pasting into the video file works fine. How to make it work from tomcat server?

After editing: I changed my application to configure the relative path in the tomcat myapp root folder, but it does not work. Below is an edited question.

Screen fonts of my application:


First step: click the link

Stage one


Step two: select a video or folder to view

Stage two


Third step: playing the video (here I get an error message) Stage three


Server. The following HTML was displayed in the browser (copied from the view source):

<!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="G:\\To-See\\Ravi_sir_joke.m4v"> </source> </video> </body> </html> 

When the same source is copied and pasted onto a sample html page anywhere on the computer, the video works fine. Below is shown that.

Expected Output


After editing: The server provided the correct relative path that contains the video. The video does not work yet.

 <!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="../ROOT-VIDEO/Ravi_sir_joke.m4v" > </source> </video> </body> </html> 

The video is present in the root directory of the application:

Tomcat root-video root

I pasted the edited program on this page for reference. Please correct me and help me clear the video error.



Program

Package Structure:

Package structure


web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- Welcome page --> <!-- <welcome-file-list> <welcome-file>/welcome.do</welcome-file> </welcome-file-list> --> <!-- JSF mapping --> <servlet> <servlet-name>Controller</servlet-name> <servlet-class>com.cluster.vapp.controller.ControllerServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <servlet-mapping> <servlet-name>Controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 

Servlet servlet:

  package com.cluster.vapp.controller; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.cluster.vapp.fileutils.FileUtil; import com.cluster.vapp.fileutils.SearchResult; import com.cluster.vapp.service.VappService; import com.cluster.vapp.service.VappServiceImpl; public class ControllerServlet extends HttpServlet { private static final long serialVersionUID = 1L; private VappService service; public void init() throws ServletException { service = new VappServiceImpl(); } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // HttpSession session = request.getSession(); String strServletPath = request.getServletPath(); // debug System.out.println(strServletPath); // end of debug int intServletpath = 0; if (strServletPath.equalsIgnoreCase("/welcome.do")) { intServletpath = 1; } if (strServletPath.equalsIgnoreCase("/verify.do")) { intServletpath = 2; } if (strServletPath.equalsIgnoreCase("/searchRoot.do")) { intServletpath = 3; } switch (intServletpath) { case 1: {// welcome.do RequestDispatcher requestDispatcher = request .getRequestDispatcher("./JSP/welcome.jsp"); requestDispatcher.forward(request, response); break; } case 2: { // verify.do if (service.isVideoFile(request.getParameter("path_name"))) { String strVideoPath = service.findRelative(request .getParameter("path_name")); request.setAttribute("VIDEO_PATH", FileUtil.adjustPathName(strVideoPath)); RequestDispatcher requestDispatcher = request .getRequestDispatcher("./JSP/video.jsp"); requestDispatcher.forward(request, response); } else { List<SearchResult> listSearchResults = service .searchDirectory(request.getParameter("path_name")); request.setAttribute("LIST_SEARCH_RESULT", listSearchResults); RequestDispatcher requestDispatcher = request .getRequestDispatcher("./JSP/search.jsp"); requestDispatcher.forward(request, response); } break; } case 3: {// searchRoot.do List<SearchResult> listSearchResults = service .searchRootDirectory(); request.setAttribute("LIST_SEARCH_RESULT", listSearchResults); RequestDispatcher requestDispatcher = request .getRequestDispatcher("./JSP/search.jsp"); requestDispatcher.forward(request, response); break; } } } } 

VappServiceImpl.java

 package com.cluster.vapp.service; import java.io.File; import java.util.ArrayList; import java.util.List; import com.cluster.vapp.fileutils.FileUtil; import com.cluster.vapp.fileutils.SearchResult; public class VappServiceImpl implements VappService{ public static final String ROOT_PATH = "F:\\apache-tomcat-7.0.33\\webapps\\balaji\\ROOT-VIDEO"; public static final String BASE_PATH = "F:\\apache-tomcat-7.0.33\\webapps\\balaji"; public List<SearchResult> searchRootDirectory() { List<String> listDirectoryNames = FileUtil.fetchFileNames(ROOT_PATH); List<SearchResult> listSearchResults = new ArrayList<SearchResult>(); for (String dirName : listDirectoryNames) { SearchResult result = new SearchResult(); result.setStrName(dirName); result.setStrPath(ROOT_PATH + "\\" + dirName); listSearchResults.add(result); } return listSearchResults; } public boolean isVideoFile(String pStrPath) { File file = new File(pStrPath); // System.out.println("Is file There: " + file.exists()); if (file.isFile()) return true; else return false; } public List<SearchResult> searchDirectory(String pStrPath) { List<String> listDirectoryNames = FileUtil.fetchFileNames(pStrPath); List<SearchResult> listSearchResults = new ArrayList<SearchResult>(); for (String dirName : listDirectoryNames) { SearchResult result = new SearchResult(); result.setStrName(dirName); result.setStrPath(pStrPath + "\\" + dirName); listSearchResults.add(result); } return listSearchResults; } public String findRelative(String pStrVideoPath){ return FileUtil.findRelativePath(BASE_PATH, pStrVideoPath); } } 

Fileutil.java

 package com.cluster.vapp.fileutils; import java.io.File; import java.util.ArrayList; import java.util.List; import com.cluster.vapp.fileutils.exceptions.InvalidAbsolutePathException; import com.cluster.vapp.fileutils.exceptions.InvalidDirectoryNameException; /** * @author Balaji.KR * * @version 1.0 * * The class Contains methods for various file operations. All methods * present will accept only absolute string path of the source and * destination file structure. * */ public class FileUtil { /** * The Method returns the names of the files as a list, in the path given. * * Note: The path name should be a absolute path, and should be a existing * directory. Any violation will lead to corresponding run-time exception. * * * @param pStrDirectory * Location of the directory where it needs to be searched. * @return List of file names as string existing in the directory. */ public static List<String> fetchFileNames(String pStrDirectory) { List<String> listFileNames = new ArrayList<String>(); File directory = new File(pStrDirectory); if (directory.isAbsolute() == false) { throw new InvalidAbsolutePathException( "Directory Path is not Absolute"); } if ((directory.exists() && directory.isDirectory()) == false) { throw new InvalidDirectoryNameException(); } String[] strFileNames = directory.list(); for (String name : strFileNames) { listFileNames.add(name); } return listFileNames; } public static String adjustPathName(String pStrPath) { StringBuilder sb = new StringBuilder(pStrPath); sb.insert(0, "../"); return sb.toString(); } public static String findRelativePath(String pStrBasePath, String pStrAbsolutePath) { return new File(pStrBasePath).toURI() .relativize(new File(pStrAbsolutePath).toURI()).getPath(); } } 

welcome.jsp

 <!DOCTYPE html> <html> <head> <title>Cluster Video App</title> </head> <body> <h1>Cluster Video Application</h1> <br></br> <br></br> <br></br> <br></br> <h1><a href="./searchRoot.do">Browse Videos</a></h1> </form> </body> </html> 

search.jsp

 <!DOCTYPE html> <%@page isELIgnored="false"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstl"%> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> function submitForm(form){form.submit();} </script> <style type="text/css"> div.label{font-size: 30px; color: blue; margin: 10px;} </style> </head> <body> <h1>Click to proceed...</h1> <jstl:forEach var="result" items="${requestScope.LIST_SEARCH_RESULT}"> <form action="./verify.do" method="post"> <div class="label"> ${result.strName} <input type="button" value="Go" onclick="submitForm(this.form);"/> <input type="hidden" name="path_name" value="${result.strPath}"> </div> </form> </jstl:forEach> </body> </html> 

video.jsp

 <!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="${requestScope.VIDEO_PATH}"> </source> </video> </body> </html> 
0
source share
3 answers

Which is for security reasons. Pages loaded from a server cannot download files from a local drive. Copy the video to src / main / webapp / video.m4v. Change the JSP to this:

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="<c:url value="/video.m4v" />"> </source> </video> </body> </html> 
+3
source

put the folder in the WebContent or webapps folder , then enter the path, for example ./folder1/file.mp4 if WebContent does not exist, then create a new project setting using eclipse with DynamicWebProject , insert the tour files there.

I also get this problem, but now its [SOLVED] and I am using tomcat 8 and java 7

0
source

How about checking video.js. It really works. I tested Apache-Tomcat 8.5.

First include these two lines

 <link href="//vjs.zencdn.net/5.11/video-js.min.css" rel="stylesheet"> <script src="//vjs.zencdn.net/5.11/video.min.js"></script> 

and use a video tag like this.

 <video id="my-player" class="video-js" controls preload="auto" poster="//vjs.zencdn.net/v/oceans.png" data-setup='{}'> <source src="//vjs.zencdn.net/v/oceans.mp4#t=5,10" type="video/mp4"></source> <source src="//vjs.zencdn.net/v/oceans.webm" type="video/webm"></source> <source src="//vjs.zencdn.net/v/oceans.ogv" type="video/ogg"></source> <p class="vjs-no-js"> To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank"> supports HTML5 video </a> </p> </video> 
0
source

Source: https://habr.com/ru/post/916993/


All Articles