Working with URL-Related Actions
In web application development, web developers usually have to deal with URL manipulation such as importing URL, redirect to another URL...JSTL provides several useful URL-related actions to simplify URL manipulation.
The <c:param> Action
The <c:param> action is used to define a parameter. It is used inside the body of other actions such as <c:import>. <c:url> and <c:redirect> actions. the syntax of the <c:param> action is simple as follows:
<c:param name="paramName" value="value" />
You specify the parameter name in the name attribute and its value in the value attribute.
The <c:import> Action
The <c:import> action enables you to retrieve the content of an URL and then you can process it within JSP page. The syntax of the <c:import> action is as follows:
<c:import url = "url"
[context = "context "]
[var = "varName"
[scope="{page|request|session|application}"]
[charEncoding="charEncoding"]>
<%-- optional body content for <c:param> subtags --%>
</c:import>
The only attribute url is mandatory. The URL could be in absolute or relative form. If it is in relative form, the resource you refer must be inside the web application. Inside the body of the <c:import> action, you can also has parameter which specify by the <c:param> action. Let's take a look at an example of using the <c:import> action:
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<html>
<head>
<title>The c:import Action</title>
</head>
<body>
<c:import url="person.xml" var="person">
</c:import>
<textarea cols="40" rows="15">
<c:out value="${person}" />
</textarea>
</body>
</html>
In the example, we use the <c:import> action to retrieve the content of an XML file resided in the web application. Then we output the content of the XML file into the textarea. Later on if you learn how to process XML, you can parse this XML content to extract data inside it.
The <c:redirect> Action
The <c:redirect> simply to redirect the current page to another page or URL. The syntax of the <c:redirect> is as follows:
<c:redirect url="newurlhere" />
You can put any URL in the url attribute of the <c:rediect> action. Let's take a look at an example of using the <c:redirect> action:
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<html>
<head>
<title>The c:redirect Action</title>
</head>
<body>
<c:if test="${param.postback == 1}">
<c:redirect url="${param.urls}" />
</c:if>
<form action="urlredirection.jsp" method="post">
<label for="urls">Search Engine</label>
<select name="urls">
<option value="http://google.com">Google</option>
<option value="http://yahoo.com">Yahoo</option>
<option value="http://bing.com">Microsoft</option>
</select>
<input type="hidden" name="postback" value="1" />
<input type ="submit" value="Go" />
</form>
</body>
</html>
In the above example, you choose a search engine and click Go button, the page will be redirect to the corresponding URL.

The <c:url> Action
The <c:url> action enables you to format an URL correctly. Inside the <c:url> action, you can put multiple <c:param> to construct URL. Here is an example of using the <c:url> action:
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<html>
<head>
<title>The c:url Action</title>
</head>
<body>
<a href="<c:url value="http://localhost/JSTLDemo/curl.jsp">
<c:param name="mode" value="demo" />
</c:url>">The c:url Action Demo</a>
</body>
</html>
In the above example, we construct a link: http://localhost/JSTLDemo/curl.jsp?mode=demo by using c:url action. We use the <c:param> to define the query string which is mode with the value demo.