JSP directives tells the web container how to translate a JSP page into a Servlet.
Types of directives
Name | Syntax | Note |
---|---|---|
Page Directive | <%@page attribute="value"%> | Defines attributes applicable to an entire JSP page. |
Include Directive | <%@include file="resource name"%> | To include the contents of any resource. |
Taglib Directive | <%@taglib uri="uri of tag lib" prefix="prefix for tag lib"%> | Used to define a tag library that may contains many tags. |
Sample code for page directive (github)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- page import directive --> <%@ page import="java.util.*" %> <!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=UTF-8"> <title>ourownjava.com</title> </head> <body> <% Date date = new Date(); %> Current time is : <%= date %> </body> </html>
Sample code for include directive (github)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>ourownjava.com</title> </head> <body> <h4>This is the header.jsp</h4> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>ourownjava.com</title> </head> <body> <!-- include directive example --> <%@ include file="header.jsp" %> </body> </html>
Taglib directive example (github)
<!-- taglib directive example --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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=UTF-8"> <title>ourownjava.com</title> </head> <body> <!-- taglib directive example --> <c:out value="output from c out tag"/> </body> </html>