JSP Tags and Directives :-


The basic structure of a JSP Page is most like a HTML file as follows -

<HTML>
<HEAD>
</HEAD>
<BODY>
JSP Content
</BODY>
</HTML>
You can include java into above file by using JSP tags and directives. JSP makes use of the following tags and directives -

1- Declaration tag (<%!     !>) - In JSP, we often need to declare variables and methods, JSP Declaration tag is used for that purpose. It does not generate any screen output. A JSP declaration tag starts with '<%!' and ends with '%>'.
For example -
<%!
private String name;  // declaring variables
public String getName()  // declaring methods
{
 return name;
}        

2- Scriptlet tag (<%     %>) Scriptlet tags starts with '<%' and ends with '%>'. The java code inside it will be included in _jspService() method. So you can define here any valid java code that can go in _jspService() method.
For example -
<%
if(username.equals("admin")  && password.equals("123"))
{
  out.println("Welcome");
}    
else
{
 out.println("Wrong username or password");
}
%>    

3- Expression tag (<%=     %>) Expression tag starts with '<%=' and ends with '%>'. It evaluates any java expression and prints its result on screen.
For example -
<%= Welcome to JSP %>   // It is equivalent to out.println("Welcome to JSP");           
<%= 2+2 %>   // It will produce output - 4
Note that there is no semicolon(;) at the end of expression.          

4- Comments tag- JSP supports two types of comments -
HTML Comments - <!-- HTML Comments -->
JSP Comments - <%-- JSP Comments -%>

5- Page Directives - Page directive inform JSP engine the rules applied to a specific jsp page. It define page specific properties like content-type, character encoding, import a package etc.
For example to include a java package in a JSP file we can use the following syntax -
<%@ page import="java.uti.*" %>  // equivalent to import java.uti.* in plain java
To set the response type we can use the following syntax -
<%@ page contentType="text/html" %>  // equivalent to response.setContentType("text/html") in Servlet   
Page directive can have up to 13 different attributes.

Consider the following sample JSP file -


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%!
private String name;
public String getName()
{
  return name;
}
%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
   <%
   out.println("Welcome to JSP");   
   %>
   <%-- out is an implicit object of javax.servlet.jsp.JSPWriter class availabel in JSP.  --%>
   <%= 2+2 %>
   <%= new Date().toString() %>
    </body>
</html>    
Container will convert the above jsp file into the following servlet -

import java.util.*;
public class -------- extends HttpServlet
{
 private String name;
 
 private String getName()
 {
  return name;
 }
 public void service()throws ServletException, IOException 
 {
  JspWriter out = response.getWriter();
  out.println("text/html;charset=UTF-8");
  out.println("<!DOCTYPE html>");
  out.println("<html>");
  out.println("<head>");
  out.println("<title>Servlet FirstServ</title>");            
  out.println("</head>");
  out.println("<body>");
  out.println("Welcome to JSP");
  out.println(2+2);
  out.println(new Date().toString());
  out.println("</body>");
  out.println("</html>");
 }
}