JSTL Core <c:out> Tag

Introduction :

The main function of the this tag is to display the output to the user. It works like expression tag in jsp <%= ---%>.

Attributes of <c:out/> tag:

  1. value: The name of the attribute whose value is to be displayed.
  2. default: We can use this attribute if the resulting value is null.
  3. escapeXml: It checks whether there is any need to convert the &, <, > etc to their character encoding codes.

Introduction :

The main function of the this tag is to display the output to the user. It works like expression tag in jsp <%= ---%>.

Attributes of <c:out/> tag:

  1. value: The name of the attribute whose value is to be displayed.
  2. default: We can use this attribute if the resulting value is null.
  3. escapeXml: It checks whether there is any need to convert the &, <, > etc to their character encoding codes.



Example of c:out tag :

Below example will describe the real use of c:out tag:

In our example, we have a Servlet named "JSTLServlet" in which which we are setting a request attribute and forwarding the request to jsp page where we will use <c:out/> taglib to display the value of the attribute set in servlet.

JSTLServlet.java

package com.jwt.jsp.jstl;

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 JSTLServlet extends HttpServlet {
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("name", "Java Web Tutor");
		getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
	}
}

As you can see in above code, the servlet is just setting an attribute in request with attribute name "name" and value "JavaWeb Tutor" and then the request is forwarded to the jsp named "result.jsp"

result.jsp

<%@ 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>JSTL c:out Tag Example</title>
</head>
<body>
	<h2>JSTL c:out Tag Example</h2><br/> 
	The value comes from servlet is : <b> <c:out value="${name}" /></b>
</body>
</html>

In the result.jsp page we did following things.

  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".


Output in Browser :

JSTL c:out tag


Download this example(src+lib) developed in eclipse





comments powered by Disqus