alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (ConcatServlet.java)

This example Jetty source code file (ConcatServlet.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

concatservlet, http, httpservlet, httpservlet, httpservletresponse, io, ioexception, request, requestdispatcher, response, servlet, servletcontext, servletcontext, servletexception, servletexception, string, string

The Jetty ConcatServlet.java source code

package org.mortbay.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/* ------------------------------------------------------------ */
/** Concatenation Servlet
 * This servlet may be used to concatenate multiple resources into
 * a single response.  It is intended to be used to load multiple
 * javascript or css files, but may be used for any content of the 
 * same mime type that can be meaningfully concatenated.
 * <p>
 * The servlet uses {@link RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
 * to combine the requested content, so dynamically generated content
 * may be combined (Eg engine.js for DWR).
 * <p>
 * The servlet uses parameter names of the query string as resource names
 * relative to the context root.  So these script tags:
 * <pre>
 *  <script type="text/javascript" src="../js/behaviour.js"></script>
 *  <script type="text/javascript" src="../js/ajax.js&/chat/chat.js"></script>
 *  <script type="text/javascript" src="../chat/chat.js"></script>
 * </pre> can be replaced with the single tag (with the ConcatServlet mapped to /concat):
 * <pre>
 *  <script type="text/javascript" src="../concat?/js/behaviour.js&/js/ajax.js&/chat/chat.js"></script>
 * </pre>
 * The {@link ServletContext#getMimeType(String)} method is used to determine the 
 * mime type of each resource.  If the types of all resources do not match, then a 415 
 * UNSUPPORTED_MEDIA_TYPE error is returned.
 * <p>
 * If the init parameter "development" is set to "true" then the servlet will run in
 * development mode and the content will be concatenated on every request. Otherwise
 * the init time of the servlet is used as the lastModifiedTime of the combined content
 * and If-Modified-Since requests are handled with 206 NOT Modified responses if 
 * appropriate. This means that when not in development mode, the servlet must be 
 * restarted before changed content will be served.
 * 
 * @author gregw
 *
 */
public class ConcatServlet extends HttpServlet
{
    boolean _development;
    long _lastModified;
    ServletContext _context;

    /* ------------------------------------------------------------ */
    public void init() throws ServletException
    {
        _lastModified=System.currentTimeMillis();
        _context=getServletContext();   
        _development="true".equals(getInitParameter("development"));
    }

    /* ------------------------------------------------------------ */
    /* 
     * @return The start time of the servlet unless in development mode, in which case -1 is returned.
     */
    protected long getLastModified(HttpServletRequest req)
    {
        return _development?-1:_lastModified;
    }
    
    /* ------------------------------------------------------------ */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String q=req.getQueryString();
        if (q==null)
        {
            resp.sendError(HttpServletResponse.SC_NO_CONTENT);
            return;
        }
        
        String[] parts = q.split("\\&");
        String type=null;
        for (int i=0;i<parts.length;i++)
        {
            String t = _context.getMimeType(parts[i]);
            if (t!=null)
            {
                if (type==null)
                    type=t;
                else if (!type.equals(t))
                {
                    resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
                    return;
                }
            }   
        }

        if (type!=null)
            resp.setContentType(type);

        for (int i=0;i<parts.length;i++)
        {
            RequestDispatcher dispatcher=_context.getRequestDispatcher(parts[i]);
            if (dispatcher!=null)
                dispatcher.include(req,resp);
        }
    }
}

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty ConcatServlet.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.