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.
- Scriptlet
- Declarative element
- Directive element
- Action element
- Expression
- Comments
- 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
- 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
variable in scriplet 1
For second time request the output
will be as follows -
variable
in declarative 2
variable in scriplet 1
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
- 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:-
- Page directive
- Include directive
- Taglib directive
- 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
|
|
<%@
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
|
|
Login.jsp
|
isErrorPage |
|
Error.jsp
|
isThreadSafe |
|
<% @page
isThreadsafe=”true |
false” %> |
isELIgnored |
|
<% @page
isELIgnored=”true |
false” %> |
Info
|
|
<% @page
info=”This JSP page
displays today climate” %> |
language |
|
<% @page
language=”Java”
%> |
session |
|
<% @page
session=”true |
false” %> |
pageEncoding |
|
<% @page
pageEncoding=”ISO-8859-1”
%> |
to be continued......................
Reference:- http://docs.oracle.com/
No comments:
Post a Comment