Advantage of JSP Over Servlet
Advantage of JSP Over Servlet
Advantage of JSP Over Servlet
JSP technology is used to create web application just like Servlet technology. It can be thought of as an
extension to servlet because it provides more functionality than servlet such as expression language, jstl
etc.
A JSP page consists of HTML tags and JSP tags. The jsp pages are easier to maintain than servlet because
we can separate designing and development. It provides some additional features such as Expression
Language, Custom Tag etc.
Advantage of JSP over Servlet
There are many advantages of JSP over servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to servlet technology. We can use all the features of servlet in JSP. In addition
to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP
development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet
technology, we mix our business logic with the presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The servlet code needs to be
updated and recompiled if we have to change the look and feel of the application.
4) Less code than Servlet
In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code. Moreover, we
can use EL, implicit objects etc.
Life cycle of a JSP Page
The JSP pages follows these phases:
Translation of JSP Page
Compilation of JSP Page
Classloading (class file is loaded by the classloader)
Instantiation (Object of the Generated Servlet is created).
Initialization ( jspInit() method is invoked by the container).
Reqeust processing ( _jspService() method is invoked by the container).
Destroy ( jspDestroy() method is invoked by the container).
Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
As depicted in the above diagram, JSP page is translated into servlet by the help of JSP translator. The JSP
translator is a part of webserver that is responsible to translate the JSP page into servlet. Afterthat Servlet page is
compiled by the compiler and gets converted into the class file. Moreover, all the processes that happens in
servlet is performed on JSP later like initialization, committing response to the browser and destroy.
The JSP declaration tag is used to declare global members (global variables & definine methods)
In 'Declaration tag', we're declaring global variables & we're defining entire methods. But we're
not defining part of a method, (i.e) we're not keeping only method statements. We're developing
entire method itself.
Where as in "Scriptlet tag", we're defining part of method (i.e.) only statements which are
defined like a inside method.
Difference between the jsp scriptlet tag and jsp declaration tag?
Jsp Scriptlet Tag
declare variables
not methods.
_jspService()
placed outside the _jspService() method.
method.
JSP Directives :
The jsp directives are messages that tells the web container how to translate a JSP page into the
corresponding servlet.
There are three types of directives:
page directive
include directive
taglib directive
import
contentType
extends
info
buffer
language
isELIgnored
isThreadSafe
autoFlush
session
pageEncoding
errorPage
isErrorPage
1. Scriptlet tag:
Program 3:
test3.jsp
<% int i = 100; i++; %>
i value is : <%= i %>
Output:
i value is : 101
2. Expression tag:
Program 4:
test4.jsp
<% int i = 100; i++; %>
<% int j = i; j += 200; %>
i value is : <%= i %> <br> j value is :
<%= j %>
Output:
i value is : 101
j value is : 301
Explanation:
Here, we developed 2 Scriptlets.
Program 5:
test5.jsp
<%= 1000
%> <br>
<%= 10.105f
%> <br>
<%= 1000.1005
%> <br>
<%= true
%> <br>
<%= false
%> <br>
<%= 'c'
%> <br>
<%= "HELLO"
%> <br>
<%= "Hello " + " Buddy" %> <br>
<%= 100+200
%> <br>
<%= 100*200
%>
Output:
1000
10.105
1000.1005 true false
c HELLO
Hello Buddy 300 20000
Explanation:
We're Printing various types of literals %& Expressions.
Program 6:(Multiple Scriptlets with Multiple Expressions)
test6.jsp
<% int i =100; i+= 10; %>
<%= "i value is : " +i %> <br>
<%
i+= 20; i++;
%>
<%= "i value is : " +i %>
<br>
<%
i+= 30; i++;
%>
i value is : <%= i %>
Output:
i value is : 110 i value is : 131 i value is : 162
Program 7:
test7.jsp
<% java.util.Date date = new java.util.Date(); String s = "Sashi"; %>
Date is : <%= date %> <br> s
value : <%= s %>
Output:
Date is : Wed Apr 16 09:00:37 IST 2014 s value :
Sashi
3. Declaration tag:
Program 8:
test8.jsp
<%!
int i = 100;
void test()
{
}
%>
i value is : <%= i %>
Output:
i value is : 100
Explanation:
Here, in 'Declaration tag', we're declaring i variable. 'i' is a global variable. And we're defining a test()
method. test() method also global method
Program 9:
test9.jsp
<%!
float f = 200.2f; float
test()
{
return (100+f);
}
%>
f value is : <%= f %> <br> test() is : <
%= test() %>
Output:
f value is
: 200.2
test() is
: 300.2
Program 10:
test10.jsp
<%! String s ="Global variable"; %>
S value is : <%= s%>
Output:
S value is : Global variable
Program 11:
test11.jsp
<%! String s ="Global variable"; %>
<% String s = "Local variable"; %>
S value is :
<%= s %>
Output:
S value is : Local variable
Explanation:
Here, in 'Declaration tag', we're declared global variable 's'. same name 's' is declared in "Scriptlet" also.
Then which one will get more preference??
Question
: Global variable or Local
variable?
Answer
: Local variable only will
get more preference.
Program 12:
Ho
Question
: w to access global variable?
Answer
: Use 'this' keyword to access global variables.
test12.jsp
<%! String s ="Global variable"; %>
<% String s = "Local variable"; %>
Global S value is : <%= this.s %> <br>
Local S value is : <%= s %>
Output:
Global S value is : Global variable
Local S value is : Local variable
Explanation:
Here, in 'Declaration tag', we're declared global variable 's'. same name 's' is declared
in "Scriptlet" also.
Local variable is getting more preference if u access directly by using variable name (i.e.) s.
If u like to access global variable 's', then use 'this' keyword. (i.e.)this.s
Program 13:
test13.jsp
<%! int day = 2; %>
<%
if(day == 1) { %> 1st
Day
<% } else if (day ==2) { %> 2nd
Day
<% } else { %> Some
other Day
<% } %>
Output:
2nd Day
Program 14:
test14.jsp
<%
int day =3; %>
Switch Statement is used. <br>
<%
switch(day) {
case
case
1:
out.println("Day 1");
break;
2:
out.println("Day 2");
break;
case 3:
out.println("Day 3");
break;
default:
out.println("Some other day");
}
%>
Output:
Switch Statement is used.
Day 3
Program 15:
test15.jsp
For Loop Example <br> <br>
<%!
int fontSize;
%>
<%