Click here to download eclipse supported ZIP file
I have developed an J2EE application that allows people to watch video.
This implementation is working fine for desktop browsers.
Videos for the Browser are currently encoded in MP4 format and have been tested to work. When someone hits a video URL we have a Video servlet thats writes the bytes to the response. The code is given below:
MIME types to play the video's :
video/mpeg: MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046
video/mp4: MP4 video; Defined in RFC 4337
video/ogg: Ogg Theora or other video (with audio); Defined in RFC 5334
video/quicktime: QuickTime video;
video/webm: WebM Matroska-based open media format
video/x-matroska: Matroska open media format
video/x-ms-wmv: Windows Media Video; Documented in Microsoft KB 288102
video/x-flv: Flash video (FLV files)
Here is the example code to test Video functionality using servlet :
This is VideoServlet.java main class having the application business logic.
package com.cv.servlet.video.play;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class VideoServlet
*
* * @author Chandra Vardhan
*
*/
public class VideoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public VideoServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getParameter("filename");
fileName = "/"+fileName + ".mp4";
ServletContext ct = getServletContext();
InputStream input = ct.getResourceAsStream(fileName);
//response.setContentType("video/quicktime"); //Use this for VLC player
response.setContentType("video/mp4");
response.setHeader("Content-Disposition", "inline; filename=\""
+ fileName + "\"");
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[2096];
int read = 0;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
input.close();
output.flush();
output.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
} |
This is index.html view file and making this as a welcome file for the application.
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Video Servlet</title>
</head>
<body>
<form method="GET" action="VideoServlet">
<br>
<input type="hidden" name="filename" value="javatest" >
<center>
<input type="SUBMIT" value="Click here to Play video...">
</center>
</form>
</body>
</html>
|
This is log4j.properties file having the entries for logging the information into the console/file.
#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n
# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
This is pom.xml file having the entries of dependency jars and information to build the application .
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ServletVideoPaly</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<log4j.version>1.2.16</log4j.version>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<groupId>com.cv.servlet.video.play</groupId>
</project>
|
This is web.xml deplotment descriptor servlet configuration file.
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.1">
<servlet>
<servlet-name>VideoServlet</servlet-name>
<servlet-class>com.cv.servlet.video.play.VideoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VideoServlet</servlet-name>
<url-pattern>/VideoServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app> |
great
ReplyDeleteHi Great article.
ReplyDeleteDoes it support, video pause, video forward operations on the video when it is playing in browser?
You may have the technology to assemble a superior mousetrap, yet in the event that you have no mice or the old mousetrap functions admirably, there is no chance and afterward the technology to fabricate a superior one gets superfluous. IT company North York
ReplyDelete