HtmlVideoServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
package org.magnum.mobilecloud.video.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; /** * Adds an html form to capture and display video metadata. * Allows clients to send HTTP POST * requests with videos that are stored in memory using a list. * Clients can send HTTP GET requests to receive a * listing of the videos that have been sent to the servlet * so far. Stopping the servlet will cause it to lose the history * of videos that have been sent to it because they are stored * in memory. * * @author jules * @author Anonymous * */ public class HtmlVideoServlet extends HttpServlet // Servlets should inherit HttpServlet { private static final long serialVersionUID = 1L; public static final String VIDEO_ADDED = "Video added."; // An in-memory list that the servlet uses to store the // videos that are sent to it by clients private List<Video> videos = new java.util.concurrent.CopyOnWriteArrayList<Video>(); protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Make sure and set the content-type header so that the client // can properly (and securely!) display the content that you send // back resp.setContentType("text/html"); // This PrintWriter allows us to write data to the HTTP // response body that is going to be sent to the client. PrintWriter sendToClient = resp.getWriter(); // UI form sendToClient.write( "<form name='formvideo' method='POST' target='_self'>" + "<fieldset><legend>Video Data</legend>" + "<table><tr>" + "<td><label for='name'>Name: </label></td>" + "<td><input type='text' name='name' id='name' size='64' maxlength='64' /></td>" + "</tr><tr>" + "<td><label for='url'>URL: </label></td>" + "<td><input type='text' name='url' id='url' size='64' maxlength='256' /></td>" + "</tr><tr>" + "<td><label for='duration'>Duration: </label></td>" + "<td><input type='text' name='duration' id='duration' size='16' maxlength='16' /></td>" + "</tr><tr>" + "<td style='text-align: right;' colspan=2><input type='submit' value='Add Video' /></td>" + "</tr></table></fieldset></form>"); // Loop through all of the stored videos and print them out // for the client to see. for (Video v : this.videos) { // For each video, write its name and URL into the HTTP // response body sendToClient.write(v.getName() + " : " + v.getUrl() + " (" + v.getDuration() + ")<br />"); } sendToClient.write("</body></html>"); } /** * This method processes all of the HTTP GET requests routed to the * servlet by the web container. This method loops through the lists * of videos that have been sent to it and generates a plain/text * list of the videos that is sent back to the client. * */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("<html><body>"); processRequest(req, resp); } /** * This method handles all HTTP POST requests that are routed to the * servlet by the web container. * * Sending a post to the servlet with 'name', 'duration', and 'url' * parameters causes a new video to be created and added to the list of * videos. * * If the client fails to send one of these parameters, the servlet generates * an HTTP error 400 (Bad request) response indicating that a required request * parameter was missing. */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // First, extract the HTTP request parameters that we are expecting // from either the URL query string or the url encoded form body String name = req.getParameter("name"); String url = req.getParameter("url"); String durationStr = req.getParameter("duration"); // Check that the duration parameter provided by the client // is actually a number long duration = -1; try{ duration = Long.parseLong(durationStr); }catch(NumberFormatException e){ // The client sent us a duration value that wasn't a number! } // Now, the servlet has to look at each request parameter that the // client was expected to provide and make sure that it isn't null, // empty, etc. if (name == null || url == null || durationStr == null || name.trim().length() < 1 || url.trim().length() < 10 || durationStr.trim().length() < 1 || duration <= 0) { // If the parameters pass our basic validation, we need to // send an HTTP 400 Bad Request to the client and give it // a hint as to what it got wrong. resp.setContentType("text/html"); resp.sendError(400, "Missing ['name','duration','url']."); } else { // It looks like the client provided all of the data that // we need, use that data to construct a new Video object Video v = new Video(name, url, duration); // Add the video to our in-memory list of videos videos.add(v); // Let the client know that we successfully added the video // by writing a message into the HTTP response body resp.getWriter().write("<html><body>" + VIDEO_ADDED); processRequest(req, resp); } } } |
Video.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package org.magnum.mobilecloud.video.servlet; /** * A simple object to represent a video and its URL for viewing. * * @author jules * */ public class Video { private String name; private String url; private long duration; public Video(String name, String url, long duration) { super(); this.name = name; this.url = url; this.duration = duration; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } } |
VideoServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package org.magnum.mobilecloud.video.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; /** * This simple VideoServlet allows clients to send HTTP POST * requests with videos that are stored in memory using a list. * Clients can send HTTP GET requests to receive a plain/test * listing of the videos that have been sent to the servlet * so far. Stopping the servlet will cause it to lose the history * of videos that have been sent to it because they are stored * in memory. * * @author jules * */ public class VideoServlet extends HttpServlet // Servlets should inherit HttpServlet { public static final String VIDEO_ADDED = "Video added."; // An in-memory list that the servlet uses to store the // videos that are sent to it by clients private List<Video> videos = new ArrayList<Video>(); /** * This method processes all of the HTTP GET requests routed to the * servlet by the web container. This method loops through the lists * of videos that have been sent to it and generates a plain/text * list of the videos that is sent back to the client. * */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Make sure and set the content-type header so that the client // can properly (and securely!) display the content that you send // back resp.setContentType("text/plain"); // This PrintWriter allows us to write data to the HTTP // response body that is going to be sent to the client. PrintWriter sendToClient = resp.getWriter(); // Loop through all of the stored videos and print them out // for the client to see. for (Video v : this.videos) { // For each video, write its name and URL into the HTTP // response body sendToClient.write(v.getName() + " : " + v.getUrl() + "\n"); } } /** * This method handles all HTTP POST requests that are routed to the * servlet by the web container. * * Sending a post to the servlet with 'name', 'duration', and 'url' * parameters causes a new video to be created and added to the list of * videos. * * If the client fails to send one of these parameters, the servlet generates * an HTTP error 400 (Bad request) response indicating that a required request * parameter was missing. */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // First, extract the HTTP request parameters that we are expecting // from either the URL query string or the url encoded form body String name = req.getParameter("name"); String url = req.getParameter("url"); String durationStr = req.getParameter("duration"); // Check that the duration parameter provided by the client // is actually a number long duration = -1; try{ duration = Long.parseLong(durationStr); }catch(NumberFormatException e){ // The client sent us a duration value that wasn't a number! } // Make sure and set the content-type header so that the client knows // how to interpret the data that gets sent back resp.setContentType("text/plain"); // Now, the servlet has to look at each request parameter that the // client was expected to provide and make sure that it isn't null, // empty, etc. if (name == null || url == null || durationStr == null || name.trim().length() < 1 || url.trim().length() < 10 || durationStr.trim().length() < 1 || duration <= 0) { // If the parameters pass our basic validation, we need to // send an HTTP 400 Bad Request to the client and give it // a hint as to what it got wrong. resp.sendError(400, "Missing ['name','duration','url']."); } else { // It looks like the client provided all of the data that // we need, use that data to construct a new Video object Video v = new Video(name, url, duration); // Add the video to our in-memory list of videos videos.add(v); // Let the client know that we successfully added the video // by writing a message into the HTTP response body resp.getWriter().write(VIDEO_ADDED); } } } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?xml version="1.0" encoding="ISO-8859-1" ?> <!-- The web.xml file provides basic information to the web container on how to load your servlets into the container and which requests should be routed to each servlet. --> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Video Application</display-name> <description> This is an application that will extract parameters and validate them. It allows clients to send the metadata for Videos. </description> <!-- This set of directives tells the web application container about our servlet class so that it is loaded by the container --> <servlet> <servlet-name>VideoServlet</servlet-name> <servlet-class>org.magnum.mobilecloud.video.servlet.VideoServlet </servlet-class> </servlet> <servlet> <servlet-name>HtmlVideoServlet</servlet-name> <servlet-class>org.magnum.mobilecloud.video.servlet.HtmlVideoServlet </servlet-class> </servlet> <!-- this set of directives tells the web application container which requests should be routed to our servlet. In this case, any request to the path '/video' will be routed to the servlet. You can use wildcards for path matching. For example, '/video/**' would match any sub path of '/video/', such as '/video/foo' and '/video/bar' --> <servlet-mapping> <servlet-name>VideoServlet</servlet-name> <url-pattern>/video</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>HtmlVideoServlet</servlet-name> <url-pattern>/view/video</url-pattern> </servlet-mapping> </web-app> |