Working with URL-Related Actions

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:

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:

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:

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:

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:

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:

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.

  • Was this tutorial helpful ?
  • YesNo