home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / servlets / RedirectServlet.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  3.4 KB  |  101 lines

  1. /*
  2.  * @(#)RedirectServlet.java    1.9 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.IOException;
  23.  
  24. import javax.servlet.*;
  25. import javax.servlet.http.*;
  26.  
  27. /**
  28.  * This is a basic redirection servlet.  It takes a single
  29.  * destination, which it then redirects the browser to... Useful
  30.  * for logging how many times links off of your site are used.
  31.  * Example: http://www.mysite.com/RedirectServlet?http://anothersite.com/
  32.  */
  33. public class RedirectServlet extends HttpServlet { 
  34.  
  35. /**
  36.  * Given a request with either extra path info or a QueryString, redirect
  37.  * browser to appropriate site.
  38.  * @param req Request object the servlet uses to get input.
  39.  * @param res Response object that the servlet uses to send output.
  40.  * @exception ServletException @see HttpServlet
  41.  * @exception IOException occurs due to general network errors.
  42.  */
  43.     public void service(HttpServletRequest req, HttpServletResponse res)
  44.     throws ServletException, IOException
  45.     {
  46.         String location = null;
  47.         String path;
  48.         String query;
  49.         if ((path = req.getPathInfo()) != null) {
  50.             if ((query = req.getQueryString()) != null) 
  51.                 location = path + '?' + query;
  52.             else 
  53.                 location = path;
  54.         } else if ((query = req.getQueryString()) != null) {
  55.             location = decode(query);
  56.         }
  57.         if (location == null) {
  58.             res.sendError(res.SC_INTERNAL_SERVER_ERROR,
  59.               "Destination not set for redirect; " +
  60.               "please inform system admin");
  61.             return;
  62.         }
  63.             res.sendRedirect(location);
  64.  
  65.     }
  66.  
  67. /**
  68.  * Obtain information on this servlet.
  69.  * @return String describing this servlet.
  70.  */
  71.     public String getServletInfo() {
  72.         return "Redirect servlet -- used to send redirects";
  73.     }
  74.  
  75.     /**
  76.      * decode a URLencoded string, so we may use it as a URL
  77.      */
  78.     private String decode(String encoded) {
  79.         
  80.         //speedily leave if we're not needed
  81.         if (encoded.indexOf('%') == -1 ) return encoded;
  82.  
  83.         StringBuffer holdstring = new StringBuffer(encoded.length());
  84.         char holdchar;
  85.  
  86.         for (int count = 0; count < encoded.length(); count++) {
  87.             if (encoded.charAt(count) == '%') {
  88.             //add check for out of bounds
  89.                 holdstring.append((char)Integer.parseInt(encoded.substring(count+1,count+3),16));
  90.                 if (count + 2 >= encoded.length()) 
  91.                     count = encoded.length();
  92.                 else
  93.                     count += 2;
  94.             } else {
  95.                 holdstring.append(encoded.charAt(count));
  96.             }
  97.         }
  98.         return holdstring.toString();
  99.     }
  100. }
  101.