In web application development, you often have to manipulate URL such as:
- Get the content of a web page for display.
- Redirect users to another URL.
To make it easier for you, 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. The <c:param> is often used inside another action such as <c:import>. <c:url> or <c:redirect> actions. To use the <c:param> action, you use the following syntax:
<c:param name="paramName" value="value" />
Code language: HTML, XML (xml)
There are two attributes:
- Name: is where you put the parameter’s name.
- Value: is where you specify the parameter’s value.
The <c:import> Action
The <c:import> action allows you to retrieve the content of another URL to process it within the 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>
Code language: JavaScript (javascript)
There are several attributes of the import action however only URL attribute is mandatory. The URL could be absolute or relative. If it is a relative URL, the resource that you refer must be inside the web application.
Inside the body of the <c:import> action, you can also have parameters using 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>
Code language: HTML, XML (xml)
In the example, first we used the <c:import> action to retrieve the content of an XML file located inside the web application. Then we placed the content of the XML file into a text area for demonstration purpose.
The <c:redirect> Action
The <c:redirect> simply redirects users from the current URL to another URL.
The syntax of the <c:redirect> is as follows:
<c:redirect url="newurlhere" />
Code language: HTML, XML (xml)
In the url attribute’s value, you specify the new URL or page that you want to redirect to.
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>
Code language: HTML, XML (xml)
In the above example, you choose a search engine and click Go button, the page will redirect to the corresponding search engine’s URL.
The <c:url> Action
The <c:url> action enables you to format a 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>
Code language: HTML, XML (xml)
In the above example, we constructed a link: http://localhost/JSTLDemo/curl.jsp?mode=demo by using c:url action. We used the <c:param> to define the query string which is mode with the value demo.