JSP directives || JSP page directives Jsp, Include Directive, JSP Taglib directive

JSP directives

JSP directives are used set the direction for web container at the time translation. JSP directives control the process of complete JSP page. In this tutorial, I am going to discuss the JSP page directives.

There are three types of directives in JSP.

JSP page directives

The attribute which will define inside JSP page directives will be applied on the complete page. for example, if you want to import some java package.

<%@ page attribute="value" %>

There are some attributes which are used with page directives.

  • import
  • isErrorPage
  • errorPage
  • isThreadSafe
  • ContentType
  • language
  • autoflush
  • extends
  • buffer
  • info

import

import is used to import a package or interface. its same as a java or servlet, for example, we can say if I want to use Date class on my page so I have to import java.util package.

<%@ page import="java.util.*" %>

Let’s see the example, below code, will be the print current date.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to codebun.com</h1>
Today date is <%= new Date() %>
</body>
</html>

isErrorPage

Used in an error page like isErrorPage=”true” 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page isErrorPage="true" %>
    
<!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>Insert title here</title>
</head>
<body>
The exception is: <%= exception %>  
<br>
<%=exception.getClass() %>

</body>
</html>


errorPage

An errorPage attribute is used to redirect control on an error page for example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page errorPage="myErrorPage.jsp" %>


<!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>Insert title here</title>
</head>
<body>
<%= 155/0 %>  
</body>
</html>

isThreadSafe

As we know Java support the concept of multithreading. every JSP page is by default thread safe it means the value of an isThreadSafe attribute is true but forcefully we can set its value false. In this case, If multiple requests come on the page than container execute them one by one.

ContentType

Used to set the type of page content. let see the example.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

language

Used to define the type of script language of the page. by default, the language type is Java.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

buffer

The buffer attribute used to set the buffer size in kilobytes to handle output which is generated by the JSP page.The default size of the buffer is 8Kb.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page buffer="32kb" %>

<!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>Insert title here</title>
</head>
<body>
<%= 155/5 %>  
</body>
</html>

autoflush

Used to flush the buffered memory automatic. Its boolean type attribute for example:

<%@page buffer="SIZE in kb" autoFlush="true / false" %>

info

info attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<%@ page info="JSP tutorial by codebun.com" %>  
<!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>Insert title here</title>
</head>
<body>
<% 
out.println(getServletInfo());
%>
</body>
</html>

 

 

Add a Comment

Your email address will not be published. Required fields are marked *

1 × 2 =