Package mpjp.server
Class Resource
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
mpjp.server.Resource
- All Implemented Interfaces:
java.io.Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
public class Resource
extends javax.servlet.http.HttpServlet
Simple Servlet for serving resources from a directory/package in
the source code. These files must be part of the WAR and fetched from
the directory where it is installed in the servlet container
The name of the package holding the resources can be managed using the
static methods getPackageName() and setPackageName(String)
Content types are to assigned resources based on their file extensions by
the getContentType(String) method. Currently, only a couple
of types are recognized and default to application/octet-stream
This servlet must be configured in the WEB-INF/web.xml file of you
webapp using elements similar to the following. Notice the tailing
asterisk in the content of the url-pattern element.
<servlet> <servlet-name>resourceServlet</servlet-name> <servlet-class>mpjp.server.Resource</servlet-class> </servlet> <servlet-mapping> <servlet-name>resourceServlet</servlet-name> <url-pattern>/mpjp/resource/*</url-pattern> </servlet-mapping>
- Author:
- José Paulo Leal
zp@dcc.fc.up.pt - See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description Resource() -
Method Summary
Modifier and Type Method Description protected voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)Process HTTP requests to fetch resources(package private) java.lang.StringgetContentType(java.lang.String name)Return a content type based on the extension.static java.lang.StringgetPackageName()Package name from where resources are currently being fetchedvoidinit()Sets the resource directory based on the WAR installation directory and the defined package namesetPackageName(String)static voidsetPackageName(java.lang.String packageName)Change name of the package/directory holding the resources It may be a binary (dot-separated) nameMethods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
-
Constructor Details
-
Resource
public Resource()
-
-
Method Details
-
getPackageName
public static java.lang.String getPackageName()Package name from where resources are currently being fetched- Returns:
- name of package
-
setPackageName
public static void setPackageName(java.lang.String packageName)Change name of the package/directory holding the resources It may be a binary (dot-separated) name- Parameters:
packageName- new package name
-
init
public void init() throws javax.servlet.ServletExceptionSets the resource directory based on the WAR installation directory and the defined package namesetPackageName(String)- Overrides:
initin classjavax.servlet.GenericServlet- Throws:
javax.servlet.ServletException
-
doGet
protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOExceptionProcess HTTP requests to fetch resources- Overrides:
doGetin classjavax.servlet.http.HttpServlet- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
getContentType
java.lang.String getContentType(java.lang.String name)Return a content type based on the extension. Only a couple of types are currently supported. Defaults toapplication/octet-stream- Parameters:
name- of resource file- Returns:
- content type
-