JSP Directives with example

"resin" is the server which supports java script and java language but most of the servers support only java language.
<%@ page language="perl" %> The above statement is called page directive. This page directive gives an instruction to the jsp compiler that we are using the perl language. A page directive is an instruction to the jsp compiler. Some more page directives are:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
This is an example of multiple import directives.
The above multiple page directives can be clubbed together as a single page directive as shown below:
<%@ page import="java.util.Date, java.sql.*" %>
To use the classes or interfaces available in different packages, we can use the import directive as shown below:
<%@ include file="two.jsp" %>
This include directive is similar to requestDispatcher.include in servlets.
A simple example of include directive is given below.
File: Two.jsp
<%@ 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">
<title>Two page</title>
</head>
<body>
This is from two.jsp
</body>
</html>

File: One.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ include file="Two.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>One page</title>
</head>
<body>
This is from one.jsp
</body>
</html>

Run One.jsp and you get the output as shown below:
When the jsp compiler compiles one.jsp the compiler merges the content of two.jsp with one.jsp as shown in the above output. After merging a single servlet is generated. The included file need not be a jsp file.

No comments:

Post a Comment