JSP
Top 50 JSP Interview Questions & Answers
Download PDF 1) Explain JSP and tell its uses. JSP stands for Java Server Pages. It is a...
Syntax of Directive:
<%@ directive attribute="" %>
There are three types of directives:
Each one of them is described in detail below with examples:
In this tutorial, you will learn -
Syntax of Page directive:
<%@ page…%>
Following are its list of attributes associated with page directive:
More details about each attribute
Syntax of language:
<%@ page language="value" %>
Here value is the programming language (underlying language)
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>Explanation of code: In the above example, attribute language value is Java which is the underlying language in this case. Hence, the code in expression tags would be compiled using java compiler.
Syntax of extends:
<%@ page extends="value" %>
Here the value represents class from which it has to be inherited.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page extends="demotest.DemoClass" %>
Explanation of the code: In the above code JSP is extending DemoClass which is within demotest package, and it will extend all class features.
Syntax of import:
<%@ page import="value" %>
Here value indicates the classes which have to be imported.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
import="java.util.Date" pageEncoding="ISO-8859-1"%>Explanation of the code:
In the above code, we are importing Date class from java.util package (all utility classes), and it can use all methods of the following class.
Syntax of the contentType:
<%@ page contentType="value" %>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>Explanation of the code:
In the above code, the content type is set as text/html, it sets character encoding for JSP and for generated response page.
Syntax of info:
<%@ page info="value" %>
Here, the value represents the servlet information.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
info="Guru Directive JSP" pageEncoding="ISO-8859-1"%>Explanation of the code:
In the above code, string "Guru Directive JSP" can be retrieved by the servlet interface using getServletInfo()
When it is set to false, then we can indicate the compiler to not create the session by default.
Syntax of session:
<%@ page session="true/false"%>
Here in this case session attribute can be set to true or false
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
session="false"%>Explanation of code:
In the above example, session attribute is set to "false" hence we are indicating that we don't want to create any session in this JSP
Syntax of isThreadSafe:
<% @ page isThreadSafe="true/false" %>
Here true or false represents if synchronization is there then set as true and set it as false.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
isThreadSafe="true"%>Explanation of the code:
In the above code, isThreadSafe is set to "true" hence synchronization will be done, and multiple threads can be used.
This attribute specifies that the buffered output should be flushed automatically or not and default value of that attribute is true.
If the value is set to false the buffer will not be flushed automatically and if its full, we will get an exception.
When the buffer is none then the false is illegitimate, and there is no buffering, so it will be flushed automatically.
Syntax of autoFlush:
<% @ page autoFlush="true/false" %>
Here true/false represents whether buffering has to be done or not
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
autoFlush="false"%>Explanation of the code:
In the above code, the autoflush is set to false and hence buffering won't be done and it has manually flush the output.
Syntax of buffer:
<%@ page buffer="value" %>
Here the value represents the size of the buffer which has to be defined. If there is no buffer, then we can write as none, and if we don't mention any value then the default is 8KB
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
buffer="16KB"%>Explanation of the code:
In the above code, buffer size is mentioned as 16KB wherein the buffer would be of that size
Syntax of isErrorPage:
<%@ page isErrorPage="true/false"%>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
isErrorPage="true"%>Explanation of the code:
In the above code, isErrorPage is set as true. Hence, it will check any other JSPs has errorPage (described in the next attribute) attribute set and it can handle exceptions.
The default is specified as "ISO-8859-1" if any other is not specified.
Syntax of pageEncoding:
<%@ page pageEncoding="vaue" %>
Here value specifies the charset value for JSP
Example:
<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
isErrorPage="true"%>Explanation of the code:
In the above code "pageEncoding" has been set to default charset ISO-8859-1
Syntax of errorPage:
<%@ page errorPage="value" %>
Here value represents the error JSP page value
Example:
<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
errorPage="errorHandler.jsp"%>Explanation of the code:
In the above code, to handle exceptions we have errroHandler.jsp
Syntax of isELIgnored:
<%@ page isELIgnored="true/false" %>
Here, true/false represents the value of EL whether it should be ignored or not.
Example:
<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
isELIgnored="true"%>Explanation of the code:
In the above code, isELIgnored is true and hence Expression Language (EL) is ignored here.
In the below example we are using four attributes(code line 1-2)
Example with four attributes
<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
isELIgnored="false"%>
<%@page import="java.util.Date" %>
<!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>Directive Guru JSP1</title>
</head>
<body>
<a>Date is:</a>
<%= new java.util.Date() %>
</body>
</html>
Explanation of the code:
Code Line 1-2: Here we have defined four attributes i.e.
Code Line 3: Here we have used import attribute, and it is importing "Date class" which is from Java util package, and we are trying to display current date in the code.
When you execute the above code, you will get the following output
Output:
Syntax of include directive:
<%@ include….%>
Example:
Directive_jsp2.jsp (Main file)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="directive_header_jsp3.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>Guru Directive JSP2</title>
</head>
<body>
<a>This is the main file</a>
</body>
</html>
Directive_header_jsp3.jsp (which is included in the main file)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
</head>
<body>
<a>Header file : </a>
<%int count =1; count++;
out.println(count);%> :
</body>
</html>
Explanation of the code:
Directive_jsp2.jsp:
Code Line 3: In this code, we use include tags where we are including the file directive_header_jsp3.jsp into the main file(_jsp2.jsp)and gets the output of both main file and included file.
Directive_header_jsp3.jsp:
Code Line 11-12: We have taken a variable count initialized to 1 and then incremented it. This will give the output in the main file as shown below.
When you execute the above code you get the following output:
Output:
Syntax of taglib directive:
<%@ taglib uri="uri" prefix="value"%>
Here "uri" attribute is a unique identifier in tag library descriptor and "prefix" attribute is a tag name.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="gurutag" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Guru Directive JSP</title>
<gurutag:hello/>
</head>
<body>
</body>
</html>
Explanation of the code:
Code Line 3: Here "taglib" is defined with attributes uri and prefix.
Code Line 9: "gurutag" is the custom tag defined and it can be used anywhere
Download PDF 1) Explain JSP and tell its uses. JSP stands for Java Server Pages. It is a...
JSP actions which use constructs in XML syntax to control the behavior of the servlet engine. We...
In this tutorial, we are going develop sample programs with JSP and using MVC architecture....
What is JSP LifeCycle? JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP...
What is MVC? MVC is an architecture that separates business logic, presentation and data. In MVC, M...
What is JSP Filter? Filters are used for filtering functionality of the Java web application. They...