Add Parameter to URL : URL « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » URL 




Add Parameter to URL
     
/**********************************************************************************
 * $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 [email protected] $
 ***********************************************************************************
 *
 * 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 == -'?' '&';
    String seg = sep + encodeUrl(name'=' + encodeUrl(value);
    return hpos == -? 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);
    }
  }

}

   
    
    
    
    
  














Related examples in the same category
1.Creating a URL with a single string.
2.Creating a URL With components
3.Converting Between a Filename Path and a URL
4.URL Constructor Test
5.URL Encode Test
6.Get URL Content
7.Get URL Parts
8.Read from a URL
9.Convert a URL to a URI
10.Converting Between a URL and a URI
11.Convert an absolute URI to a URL
12.URL Equality
13.Parsing a URL
14.URL Request
15.URL Get
16.A URL Retrieval Example
17.URL Reader
18.URL Connection ReaderURL Connection Reader
19.Using URLConnection
20.Parse URLParse URL
21.Resolve a relative URL
22.sends e-mail using a mailto: URLsends e-mail using a mailto: URL
23.Convert the absolute URI to a URL object
24.Convert URI to URL
25.Get parts of a url
26.Checks, whether the URL uses a file based protocol.
27.Returns the anchor value of the given URL
28.Extracts the file name from the URL.
29.Creates a relative url by stripping the common parts of the the url.
30.Checks, whether the URL points to the same service. A service is equal if the protocol, host and port are equal.
31.Extracts the base URL from the given URL by stripping the query and anchor part.
32.Returns true if the URL represents a path, and false otherwise.
33.Parse Port
34.Parse Host
35.Given a URL check if its a jar url(jar:!/archive) and if it is, extract the archive entry into the given dest directory and return a file URL to its location
36.check the validity of url pattern according to the spec.
37.A collection of File, URL and filename utility methods
38.Build Relative URL Path
39.Checks that the protocol://host:port part of two URLs are equal
40.Create valid URL from a system id
41.Extract URL File Name
42.Extract the URL page name from the given path
43.Get Domain Name
44.Get Locale From String
45.Get URL Last Modified
46.Get the name of the parent of the given URL path
47.Get the parent of the given URL path
48.Has URLContent Changed
49.Is URL a local file
50.Normalize an URL
51.Normalizes an URL
52.Resolve a relative URL string against an absolute URL string
53.ResourceBundle String manager
54.Save URL contents to a file
55.URL Path: standardize the creation of mutation of path-like structures
56.Utility class for building URLs
57.Add Default Port to a URL If Missing
58.Get Relative Path To URL
59.Download from a URL and save to a file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.