JSP Basics

December 30, 2014

Java Server Pages or JSPs are very similar to typical html pages except they are embedded with java code. This simplifies the development of dynamic web applications.

How JSPs are related to Servlets

JSPs are basically an abstraction to Servlets. Before compilation, JSPs are translated into Servlets and then compiled by the servlet container.

JSP Lifecycle

  1. JSP is translated into a Servlet (creating equivalent Java source code for a servlet) and Compiled by the servlet container.
  2. An instance of the corresponding Servlet class is created and initialized by the by calling jspInit() method.
  3. This servlet object stays in the memory and for each request _jspService() method is invoked, passing the request and response objects.
  4. When the container needs to remove the servlet instance from service jspDestroy() method is called.

JSP Syntax

Tag Syntax
Declarations <%!    %>
Script-lets <%     %>
Comments <%--  --%>
Expressions <%=    %>
Directives <%@    %>
Actions/ XML style tags <jsp:ACTION />
<jsp:ACTION>   </jsp:ACTION>


Declarations

This tag is used to define variables and methods belong to the servlet that the page is translated to and these are accessible within the scope of the jsp page.

<%!
private String str = "JSP Declarations.";
public int test(){
   
    //More code
    return 0;
}
%>
<%! Object ob = new Object(); %>


Script-lets

This tag includes the code fragments which are language statements such as loops, flow control statements, etc. Unlike other tags, script-lets can be mixed in with the template text. These code fragments are compiled into the _jspService method generated by the servlet container.

<!-- HTML table with a for loop –> 
<table> 
<% 
for(int i=0;i<5;i++){
%> 
<tr>
<td>HTML table row </td><td><%= i %></td> 

</tr> 
<% } %> 
</table>

Comments

Unlike HTML comments which are transmitted with the output page and can be viewed from page source from the browser, JSP comments appear only in the original JSP page and they do not appear in the resulting Java code either. They are simply skipped by the page compiler.

<!-- This is an HTML comment -->
<%-- This is a JSP comment --%>

Standard java comments can also be used inside code fragments and as opposed to JSP comments, they appear in resulting Java code.

<%
// Standard line comment
/*
Standard block comment
*/
%>

Expressions

Expressions are evaluated as Strings and are written to the output stream. Basically this tag is a shortcut for the out.print() method. A single tag can contain only one expression.

<!-- Declare a variable and a method -->
<%!
private String info = "Expression tag example" ;
public int getValue(){
    return 12345;
}
%>
<!-- Access the declared method and variable using expression tag -->
<p> Value: <%= getValue() %> <p/> 
<p> Info: <%= info %> <p/>


Directives

This tag is used to provide instructions/messages to the web container and takes the form of
<%@ directive attribute="value"%>. There are three kinds of directives; include, page, taglib.

Include Directive

This directive instructs the container to include static content (text files, html, etc.) from a specified relative URL. The reason for the content to be static is because they are added during the translation of JSP page.

<!-- include directive example -–>
<%@include file="copy_right.txt" %>
Taglib Directive
This is powerful concept that is used to define a library of custom tags to be used in the JSP page. To declare the tag, the location of the tag library URI and the short name(prefix) that will be used to access in the page must be provided.

<!-- taglib directive example ––> 
<%@taglib uri="http://example.com/test/my-tag-lib" prefix="myTag" %>
Page Directive
Instructions or Messages provided through page directives apply to the entire page. Multiple page directives or similarly multiple attributes in one directive can be in a page.
Attribute Description
autoFlush Controls the buffer output. By default this is set to "true" and the buffer will be flushed to the output stream when full. If set "false", an overflow of the buffer causes an exception.
<%@page autoFlush="false" %>
buffer Controls the size of the response output buffer.Value can be "none" or a size. Default value is "8kb".
<%@page buffer="16kb" %>
contentType Defines the MIME content type and the character encoding of the page as used in the response. Default value is,
<%@page contentType="text/html; charset=ISO-8859-1"%>
deferredSyntaxAllowedAsLiteral Configures the page to accept the #{ characters as string literals. This is used for backward compatibility since prior to JSP 2.1, the #{} syntax for Expression Language was not reserved.
<%@page deferredSyntaxAllowedAsLiteral="true" %>
errorPage If an uncaught Exception or Error object is thrown by the page, then the page will be redirected to the specified relative URL.
<%@page errorPage="myErrorPage.jsp" %>
extends Defines the super class of the generated servlet of the jsp page.
<%@page extends="some_package.SomeClass" %>
import Use to define a list of classes to be imported to the JSP separated by commas. Behaves much like import statements in a regular Java class.
<%@page import="java.util.ArrayList,java.io.*" %>
info Use to define a String value, usually a description of the current page which can accessed using getServeltInfo() method.
<%@page info="This is a test page" %>
isELIgnored Use to disable evaluation of Expression Language (EL) statements in JSP versions prior to 2.0. Default value is "false".
<%@page isELIgnored="true" %>
isErrorPage Indicates whether the current page is set as a target of another page's errorPage URL. Default value is "false"
<%@page isErroPage="true" %>
isThreadSafe By default this is set to "true". If set "false", the container will allow only one request to be processed at a time.
<%@page isThreadSafe="true" %>
language Indicates the language used in the scripts of the page. Default is "java".
<%@page language="java" %>
pageEncoding Default is "ISO-8859-1". This is same as setting the CHARSET value of the contentType attribute.
<%@page pageEncoding="UTF-8" %>
session Use to indicate whether the corresponding page participates in a session in creating or accessing an already created session object. Default is "true".
<%@page session="false" %>
trimDirectiveWhitespaces Use to eliminate extra white space included in the template text of JSP pages. By default this is set "false"
<%@page trimDirectiveWhitespaces="true" %>

NEXT: JSP Basics: Actions

No comments:

Post a Comment