A
query string (sometimes also known as a
search string) is an
optional part of a
URL that goes after the
file or
directory name, and begins with a question (
?) mark. Query strings are used to pass variables from page to page on a
web server, thereby maintaining state on a
stateless server.
For example, everything2 uses query strings to pass information on the current node, last visited node, etc... from one page to another.
They look something like this:
?node_id=282&lastnode_id=796891
Most web-enabled programming languages have methods for obtaining the query string from a URL.
In ASP, this is accomplished through the Request.QueryString collection. Variables in the query string can be referenced through Request.QueryString(variable_name). Example (in VBScript) using the query string detailed above:
<%
Response.Write Request.QueryString("node_id") & "<br>"
Response.Write Request.QueryString("lastnode_id")
%>
would result in the following
output to a browser:
282
796891
In JavaScript, retrieving the query string is done with the window object's location.search property. For example:
<script language="javascript">
document.write window.location.search;
<script>
would result in the following output to a
browser:
?node_id=282&lastnode_id=796891
In Java, query string access is implemented in the java.net.URL class's getQuery() method. The method returns a String object containing the query string.