Core JSTL c:out Example to print value of attribute in any scope

Core JSTL library is used to perform some common tasks that are needed while working with JSP pages. As we know JSP are mainly invented for solving dynamic UI creation with less java code required. Taglibs provides a some convenient way to work with jsp without writing any java code.

In this tutorial, we will see the use of c taglib to print the value of an attribute stored in any scope.

To print the value of attribute, we use <c:out/> taglib.

Attributes of <c:out/> taglib:

Attribute Name Is mendatory Description
value Yes The name of the attribute whose value is to be printed

The bellow example will illustrate you the real practical use of:

In our example, we have a servlet named “ForwardServlet” which is just setting a request attribute and forwarding the request to our jsp page where we will use <c:out/> taglib to print the value of the attribute set in servlet.

Our Servlet code:


package com.raistudies.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ForwardServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        request.setAttribute("name", "Rai Studies");
        getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

}

As you can see in above code, the servlet is just setting an attribute in request with attribute name “name” and value “Rai Studies” and then the request is forwarded to the jsp named “result.jsp”

Our jsp page:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Example of c:out JSTL</title>
    </head>
    <body>
        <h3>An Example of c:out JSTL...</h3><br/>
        The value comes from servlet is : <b> <c:out value="${name}"/></b>
    </body>
</html>

In our jsp you can get two new things that are used to print the attribute value stored in servlet:

  1. <%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%> : We have registered the core jstl with the prefix “c” to use.
  2. <c:out value=”${name}”/>: The <c:out/> taglib is used to print the value of the attribute named “name”.

While running the example in Tomcat 6 server, we get the following output:

Output of c:out taglib example

Output of c:out taglib example

You can try the example by yourself, download the example from bellow link and import in eclipse to modify and try:

Source + lib: Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>