JSP/Servlet | JSTL(JSP Standard Tag Library) | <c:url>, <c:redirect>

URL output <c:url>

The <c:url> tag creates a URL. It can encode the URL and automatically add the context to generate an address.

If the context attribute is specified, the values of value and context must start with /. If the context attribute is omitted, the current context is applied.

Syntax

<c:url value="value" [context="context"] [var="varName"] [scope="{page|request|session|application}"]/>

Attributes

Item Description Required Default
url Base URL Required
context Name of the local application after / Current application
var Variable name that stores the processed URL Displayed page
scope Scope that exposes the processed URL variable page

Example

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
  <head>
  <title>Tag Example</title>
</head>
<body>
  <a href="<c:url value='/hello.jsp?name=Devkuma'/>">cookie.jsp</a><br><br>
  <a href="<c:url value='/' />">Context Root(/)</a>
</body>
</html>

When you click the link tag below, it moves to http://localhost:8080/hello.jsp.

<a href="<c:url value='/hello.jsp?name=Dev Kuma'/>">cookie.jsp</a><br><br>

The URL created by <c:url> is http://localhost:8080/web/jsp/hello.jsp, and the space in the value “Dev Kuma” of the name variable is URL-encoded as %20 when passed.

When you click the link tag below, it moves to http://localhost:8080/web/.

<a href="<c:url value='/'/>">Context Root(/)</a>

Redirect <c:redirect>

Attributes

Item Description Required Default
url URL to redirect in the user’s browser Required
context Name of the local web application after /

Example

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
   <head>
      <title><c:redirect> Tag Example</title>
   </head>

   <body>
      <c:redirect url="http://www.devkuma.com"/>
   </body>
</html>

It redirects to http://www.devkuma.com.