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 void doGet​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
    Process HTTP requests to fetch resources
    (package private) java.lang.String getContentType​(java.lang.String name)
    Return a content type based on the extension.
    static java.lang.String getPackageName()
    Package name from where resources are currently being fetched
    void init()
    Sets the resource directory based on the WAR installation directory and the defined package name setPackageName(String)
    static void setPackageName​(java.lang.String packageName)
    Change name of the package/directory holding the resources It may be a binary (dot-separated) name

    Methods inherited from class javax.servlet.http.HttpServlet

    doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service

    Methods inherited from class javax.servlet.GenericServlet

    destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • 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.ServletException
      Sets the resource directory based on the WAR installation directory and the defined package name setPackageName(String)
      Overrides:
      init in class javax.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.IOException
      Process HTTP requests to fetch resources
      Overrides:
      doGet in class javax.servlet.http.HttpServlet
      Throws:
      javax.servlet.ServletException
      java.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 to application/octet-stream
      Parameters:
      name - of resource file
      Returns:
      content type