Add Parameter to URL : URL « Network « Java Tutorial






/**********************************************************************************
 * $URL: https://source.sakaiproject.org/svn/portal/branches/sakai_2-5-4/portal-util/util/src/java/org/sakaiproject/portal/util/URLUtils.java $
 * $Id: URLUtils.java 28982 2007-04-16 21:41:44Z ian@caret.cam.ac.uk $
 ***********************************************************************************
 *
 * Copyright (c) 2006 The Sakai Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 **********************************************************************************/


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author ieb
 * @since Sakai 2.4
 * @version $Rev: 28982 $
 */

public class URLUtils
{

  public static String addParameter(String URL, String name, String value)
  {
    int qpos = URL.indexOf('?');
    int hpos = URL.indexOf('#');
    char sep = qpos == -1 ? '?' : '&';
    String seg = sep + encodeUrl(name) + '=' + encodeUrl(value);
    return hpos == -1 ? URL + seg : URL.substring(0, hpos) + seg
        + URL.substring(hpos);
  }

  /**
   * The same behaviour as Web.escapeUrl, only without the "funky encoding" of
   * the characters ? and ; (uses JDK URLEncoder directly).
   * 
   * @param toencode
   *        The string to encode.
   * @return <code>toencode</code> fully escaped using URL rules.
   */
  public static String encodeUrl(String url)
  {
    try
    {
      return URLEncoder.encode(url, "UTF-8");
    }
    catch (UnsupportedEncodingException uee)
    {
      throw new IllegalArgumentException(uee);
    }
  }

}








19.2.URL
19.2.1.Creating a URL With components
19.2.2.Creating a URL with a single string.
19.2.3.Create a URL that refers to a jar file in the file system
19.2.4.Create a URL that refers to an entry in the jar file
19.2.5.java.net.URL
19.2.6.A class that displays information about a URL
19.2.7.ProtocolTester
19.2.8.Parsing a URL
19.2.9.Eliminate Query
19.2.10.Parse Host
19.2.11.Parse Port
19.2.12.Make a URL from the given string
19.2.13.Create BufferedInputStream from URL
19.2.14.Utility to convert File to URL.
19.2.15.Read from URL
19.2.16.Get Content from a URL
19.2.17.Save binary file from URL
19.2.18.Reading A Web Resource: Opening a URL's stream
19.2.19.Relative URL
19.2.20.Resolve a relative URL
19.2.21.Build query string for URL
19.2.22.URL Equality
19.2.23.URL Splitter
19.2.24.new URL('mailto:your@yourserver.net')
19.2.25.Getting Text from a URL
19.2.26.Getting an Image from a URL
19.2.27.Getting a Jar File Using a URL
19.2.28.File size from URL
19.2.29.Returns true if the URL represents a path, and false otherwise.
19.2.30.Locating files by path or URL
19.2.31.provides a simple interface for assembling GET URLs
19.2.32.Add Parameter to URL
19.2.33.Extracts the base URL from the given URL by stripping the query and anchor part.
19.2.34.Returns the anchor value of the given URL