Thursday 16 July 2015

JSP DIRECTIVES


JSP DIRECTIVES

Basically JSP have 3 type of directives :
  1. Page Directives
  2. Include Directive
  3. Taglib Directive
Here we will discuss about Include Directive :
Include directive provide us the facility to reuse the elements.
* Included  elements can contain jsp code so they are inserted into the the page at the time the page is translated  to servlet.
So the main problem with include directive is if we modify the included page content, we have to update the all the main jsp pages that include that page .
There is a another way to overcome this limitation.

How to include file at Page Translation Time with Include Directive

Syntax     <% include file = "relative url"  %>


Example

there are 2 files : IncludeDirective.jsp : this page is included into other jsp page.
IncludeDirective2.jsp : Main Jsp page that include IncludeDirective.jsp page.

IncludeDirective.jsp :

<%--  this page will be included into other jsp page   --%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.util.Date" %>
<%!
private int accessCount = 1;
private Date accessDate = new Date();
private String accessHost = "<I>No previous access</I>";
%>
<P>
<HR>
This page &copy; 2000
<A HREF="http//www.my-company.com/">my-company.com</A>.
This page has been accessed <%= ++accessCount %>
times since server reboot. It was last accessed from
<%= accessHost  %>  at  <%= accessDate %>.
<%  accessHost = request.getRemoteHost();   %>
<%=  request.getContentType()  %>
<%   accessDate = new Date();  %>
</body>
</html>


IncludeDirective2.jsp

<%--  the main jsp page which will include other jsp   --%>


<html>
<head>
<title>Insert title here</title>
</head>
<body>
<P>
Information about our products and services.
<P>
Blah, blah, blah.
<P>
Yadda, yadda, yadda.
<%-- 
this file will be included at the time of translation of jsp page into servlet    --%>
<%@ include file="IncludeDirective.jsp" %>
</body>
</html>


Output  :







 

JSP : Java Server Pages


JAVA SERVER PAGES



"JSP technology is an extension of the servlet technology."


JavaServer Pages (JSP) technology enables you to mix regular, static HTML with dynamically generated content from servlets. 
we write the regular HTML in the normal manner, then enclose the code for the dynamic parts in special tags, most of which start with <%and end with %>

HOW JSP WORKS

The process of making JavaServer Pages accessible on the Web is much simpler than that for servlets. all we need is a web server that support JSP pages . we save our file with .jsp extension and thats all no compiling, no packages, no classpath required.
Behind the scene our JSP page is translated into a servlet where static HTML content being printed to the output stream associated  with servlet's service method.

Inside a JSP Page :

Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions.

SCRIPTING ELEMENTS :   java code that will become part of                                                      resultant servlet.
DIRECTIVES :    control the overall structure of servlet.

ACTION :    control the behaviour of JSP engine.

SCRIPTING ELEMENTS 

  1.  EXPRESSIONS :  which are evaluated and then inserted into servlet's output .                                                                                                           General form :   <%=           %>                                                                      Alternate XML syntax :                                                                                                                                                              <jsp : expression>                                                                        Java Expression                                                                         </jsp : expression>                                                                      
  2. SCRIPTLETS :  which are inserted into servlet's  _service method.                                                                                                   General form:    <%               %>                                               Alternate XML syntax :                                                                                                     <jsp : scriptlets>                                                                             code                                                                                          </jsp : scriptlets>                                                                          
  3. DECLARATION : which are inserted into body of servlet's class outside any method.                                                                         General form:     <%!       %>                                             Alternate XML syntax :                                                                                                     <jsp : declaration>                                                                         code                                                                                         </jsp : declaration>

Static HTML inside a JSP page is called Template Text.

JSP Comments are in the form   <%--   jsp comment        --%> 

A simplest example of 

EXAMPLE :   

ScriptingElement.jsp 

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

<body>

<%--   expression example     --%>
current date and time is :
<%=   new java.util.Date()    %>
<hr>
<%= request.getRemoteHost() %>
<hr>

<%--   delaration example     --%>
<%!   int count =1;    %>

<%--    scriptlet example     --%>
<%      count++;
       out.println(count);   %>
<hr>
</body>
</html>


OUTPUT



Search This Blog

Total Pageviews