Tuesday, June 11, 2013

JSP-Scriplet elements-part1

Hi All,
Welcome to Java-recent.
Today we will discuss about Java server pages(JSP) scripting elements, they are key in developing JSP pages.
Before going to actual topic let’s have a brief introduction about JSP

Earlier to JSP’s we have Servlets where html code is embedded in Java class, but in JSP Java code will be embedded in JSP pages (.jsp)

JSP has scripting elements, which provides various features that help a developer to develop applications faster. These elements reduce lot of drawbacks of Servlet.
Scripting elements improves code readability, maintainability and re usability

Let’s discuss them in detail.
  1. Scriptlet
  2. Declarative element
  3. Directive element
  4. Action element
  5. Expression
  6. Comments

  1. Scriptlets:- In JSP’s Java code will be embedded in Web page, where will the Java code placed?
Java code will be placed Scriptlet element.
Syntax:- <% Java code goes here %>
Note :-
  • Where will this Java code gets placed after translation phase?
  • Whatever code written here will be part of service () method of converted Servlet
  • A Scriptlet can have JAVA statements, local variables, method invocations etc.
  • A JSP page can have any number of Scriptlet elements all the code inside them will be part of service method, they gets executed as per the declaration
XML equivalent is <jsp:scriplet> Java code goes here </ jsp:Scriptlet>
Example:-

date.jsp
<%@ page language="java" %>
<%@ page import="java.util.Date" %>
<%
Date date=new Date();
out.println(date);
%>

Out put:-
Tue Jun 11 20:00:15 IST 2013


  1. Declarative element :- If we want to declare an instance variable or define our own method ,because using Scriptlet whatever code we write will be part of Service method, so how can we achieve this?
JSP provides Declarative elements to do this.
Syntax: - <%! Java code goes here %>
Example:-
<%@ page import="java.util.Date"%>
<%!private int empId;

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}
%>
We will see the difference of using Scriptlet and Declarative element by an example
Scenario:- Declare an Integer variable , count in both Scriptlet and Directive element as below and execute the code more than once and see the difference
Code here
<!--declartive element -->
<%!private int count1=0;%>
<!-- Scriptlet element -->
<%int count2=0;
count1++;
count2++;
out.println("variable in declarative"+count1 +"<br>");
out.println("variable in scriplet"+count2);
%>

Output:-
When we run the above code for first time the output will be as follows-
variable in declarative 1
variable in scriplet 1
For second time request the output will be as follows -
variable in declarative 2
variable in scriplet 1

Observation :-
The variable declared will be incremented by 1 for a subsequent request.
The jsp page will be compiled once and will compile again if we modify any code in jsp,Servlet object is created once.
The scriptlet variable(count2) will be local variable to service method so for every request this method will run and the count2 variable will be re initialized to zero


  1. Directive element: - These are like preprocessors; they provide special information about a JSP page to JVM. Directive elements produce instructions to JVM, they provide messages to web container and tells how to convert a JSP into a Servlet.

For better understanding let’s take an example, suppose I want find today’s date and print it in JSP page? I need to print today's date in a JSP page then I need to import java.util.Date package, how can we implement this in a JSP page?

Here come the use of Directive elements
There are three types of Directive elements:-
  1. Page directive
  2. Include directive
  3. Taglib directive
  1. Page directive:-
    • Will have different attributes required by a page like import, session etc..
    • This tag does not produce any visible content when the page is requested from the web server
Syntax:- <%@ page attribute="value" %>
Attributes of Page directive
  • autoFlush
  • buffer
  • contentType
  • extends
  • errorPage
  • isErrorPage
  • isThreadSafe
  • isELIgnored
  • info
  • language
  • session
  • pageEncoding
We will discuss each one of them
Attribute
Explanation
Example
autoFlush and buffer


  • Buffer attribute is used to define size for flushing the response buffer

  • autoFlush takes Boolean values(true of false),by default the value is true

  • The default value of buffer size is 8kb

  • If we set buffer size value and set autoFlush as false it will throw exception

<%@ page buffer="6kb" autoFlush=”true %>


contentType


This value sets the type of content or MIME type
(Multipurpose Internet Mail Extensions)
<% @page contentType=” text/html; charset=ISO-8859-1 %>
extends
If a JSP converted Servlet need to extend a class we can use this attribute
<% @page extends=”com.ParentClass %>
errorPage
  • The value for this attribute is a jsp page which handles exceptions and errors

  • We have a Login.jsp and we set errorPge value as error.jsp whenever an un checked exception/error occurs the error output will be directed to error.jsp

Login.jsp

<% @page errorPage=”error.jsp %>
isErrorPage
  • This is the most important and widely used attribute

  • If isErrorPage value is set to true then that particular page can act like a error handler if the value is set to false then that particular page cannot act as error handling page

  • The exceptions or user defined error messages will be directed to this page and displayed here


Error.jsp

<% @page isErrorPage=”true | false %>
isThreadSafe
  • It takes Boolean value
  • If the value is set true it implies that JSP container can handle multiple requests
  • If the value is set to false the requests will be processed sequentially
<% @page isThreadsafe=”true | false %>
isELIgnored
  • Expression Language(EL- ${} will be treated as a static content if this attribute is set to false
  • By default the value is true
<% @page isELIgnored=”true | false %>
Info
  • Gives description for a JSP page like what the JSP page do etc.
  • The value is treated like comment
<% @page info=”This JSP page displays today climate %>
language
  • Tells the programming language used in a JSP page
<% @page language=”Java %>
session
  • It take Boolean values, by default set to true
  • If value set to false that particular page will not have access to implicit session object, we cannot use session object methods etc.
<% @page session=”true | false %>
pageEncoding
  • Defines page encoding
<% @page pageEncoding=”ISO-8859-1 %>


to be continued......................

Reference:-  http://docs.oracle.com/ 

No comments:

Post a Comment

Like and Share