0% found this document useful (0 votes)
39 views

Advantage of JSP Over Servlet: JSP Technology Is Used To Create Web Application Just Like Servlet

The document discusses JavaServer Pages (JSP) technology. It provides 3 key points: 1) JSP is used to create web applications similar to servlets. It allows for easier maintenance than servlets by separating design from development. JSP provides additional features like expression language and custom tags. 2) JSP has advantages over servlets like being an extension to servlets, allowing use of servlet features. JSP is also easier to maintain by separating business logic from presentation logic. 3) A JSP page consists of JSP tags and scripting elements that allow Java code to be added. The JSP page goes through translation, compilation, classloading, instantiation, initialization, request
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Advantage of JSP Over Servlet: JSP Technology Is Used To Create Web Application Just Like Servlet

The document discusses JavaServer Pages (JSP) technology. It provides 3 key points: 1) JSP is used to create web applications similar to servlets. It allows for easier maintenance than servlets by separating design from development. JSP provides additional features like expression language and custom tags. 2) JSP has advantages over servlets like being an extension to servlets, allowing use of servlet features. JSP is also easier to maintain by separating business logic from presentation logic. 3) A JSP page consists of JSP tags and scripting elements that allow Java code to be added. The JSP page goes through translation, compilation, classloading, instantiation, initialization, request
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JSP

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 JS page consists of !"#$ tags and JS tags. "he jsp pages are easier to
maintain than servlet because we can separate designing and development. It
provides some additional features such as %xpression $anguage, &ustom "ag etc.
Advantage of JSP over Servlet
"here are many advantages of JS over servlet. "hey are as follows'
1) Extension to Servlet
JS technology is the extension to servlet technology. (e can use all the
features of servlet in JS. In addition to, we can use implicit objects, predefined
tags, expression language and &ustom tags in JS, that makes JS development easy.
2) Easy to maintain
JS 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 reompile and redeploy
If JS page is modified, we don)t need to recompile and redeploy the project.
"he servlet code needs to be updated and recompiled if we have to change the look
and feel of the application.
!) "ess ode t#an Servlet
In JS, we can use a lot of tags such as action tags, jstl, custom tags etc.
that reduces the code. #oreover, we can use %$, implicit objects etc.
repared by Sashi *
"ife yle of a JSP Page
"he JS pages follows these phases'
"ranslation of JS age
&ompilation of JS age
&lassloading +class file is loaded by the classloader,
Instantiation +-bject of the .enerated Servlet is created,.
Initiali/ation + jspInit+, method is invoked by the container,.
0e1eust processing + 2jspService+, method is invoked by the container,.
3estroy + jsp3estroy+, method is invoked by the container,.
Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
As depicted in the above diagram, JS page is translated into servlet by the
help of JS translator. "he JS translator is a part of webserver that is
responsible to translate the JS page into servlet. Afterthat Servlet page is
compiled by the compiler and gets converted into the class file. #oreover, all the
processes that happens in servlet is performed on JS later like initiali/ation,
committing response to the browser and destroy.
repared by Sashi 4
$#at is JavaServer Pages%
JavaServer ages +JS, could be a technology for developing web content that support
dynamic content that helps developers insert java code in !"#$ pages by creating use of
special JS tags, most of that begin with 5lt67 and finish with 75gt6. A JavaServer ages
element could be a form of Java servlet that8s designed to satisfy the role of a program for
a Java internet application. internet developers write JSs as text files that mix !"#$ or
9!"#$ code, 9#$ parts, and embedded JS actions and commands. :sing JS, you8ll be able to
collect input from users through online page forms, gift records from a information or
another supply, and build web content dynamically. JS tags are often used for a range of
functions, like retrieving data from a information or registering user preferences,
accessing Java;eans parts, passing management between pages and sharing data between
re1uests, pages etc.
$#y &se JSP%
JavaServer ages usually serve a similar purpose as programs enforced victimisation
the &ommon entree Interface +&.I,. however JS provide many blessings compared with the &.I.
erformance is considerably higher as a result of JS permits embedding 3ynamic parts in
!"#$ ages itself rather than having a separate &.I files. JS area unit forever compiled
before it8s processed by the server not like &.I<erl which needs the server to load A=
interpreter and therefore the target script on every occasion the page is re1uested.
JavaServer ages area unit designed on high of the Java Servlets AI, thus like Servlets,
JS conjointly has access to all or any the powerful %nterprise Java Apis, as well as J3;&,
J=3I, %J;, JA9 etc. JS pages are often employed in combination with servlets that handle
the business logic, the model supported by Java servlet guide engines. >inally, JS is A=
integral a part of Java electrical engineering, a whole platform for enterprise category
applications. this suggests that JS will play an area within the simplest applications to
the foremost advanced and tightened.
'reating a simple JSP Page
"o create the first jsp page, write some html code as given below, and save it by .jsp
extension. (e have save this file as test*.jsp. ut it in a folder and paste the folder in
the web?apps directory in apache tomcat to run the jsp page.
Diretory str(t(re of JSP
"he directory structure of JS page is same as servlet. (e contains the jsp page outside the
(%;?I=> folder or in any directory.
repared by Sashi @

)o* to r(n a simple JSP Page %
&reate new project inside webapps
webapps ??A basic?sciptlets +=ew roject,.
Program 1:
3evelop test*.jsp in
webapps ???A basic?sciptlets ???A test*.jsp
test+,sp
(elcome to JS
Start the Server,
-pen the (eb browser, and type,
http'<<localhost'BCBC<basic?sciptlets<test.jsp
-(tp(t:
(elcome to JS
Program 2:
3evelop test4.jsp in
webapps ???A basic?sciptlets ???A test4.jsp
test2+,sp
DbA(elcome to JSD<bA DbrA
DiA(elcome to JSD<iA DbrA
DuA(elcome to JSD<uA DbrA
DuADbA(elcome to JS D<bAD<uA
Start the Server,
-pen the (eb browser, and type,
http'<<localhost'BCBC<basic?sciptlets<tes4jsp
-(tp(t:
$elome to JSP
Welcome to JSP
(elcome to JS
$elome to JSP
repared by Sashi E
JSP .ags:
(e cannot keep java code directly in JS.
In JS, java code can be written inside the jsp page using the scriptlet tag.
$et)s see what are the scripting elements first.
.#ere are 3 types of sripting elements:
"he scripting elements provides the ability to insert java code inside the
jsp. "here are three types of scripting elements'
*. Scriptlet tag
4. %xpression tag
@. 3eclaration tag
1+ Sriptlet:
Syntax ' D7 java source code 7A
Inside Scriptlet, we)re keeping the java statements.
Scriptlets are almost look like a statements which are inside a method.
(hatever statements we)re keeping inside a method, same statements, we can
keep inside a Scriptlet.
(hatever statements we)re keeping inside a Scriptlet, those should end with
a semicolon +6,
Any no. of statements we can keep inside a Scriptlet.
Any no. of Scriptlets we can keep in a JS >ile.
2+ Expression:
Syntax ' D7F statement 7A
%xpression is used to print all $iterals, #ethod return values, 5 variable
values.
(hatever u like to print, same thing we can keep inside a %xpression
"he code placed within expression tag is written to the output stream of the
response. So you need not write out.print+, to write data. It is mainly used
to print the values of variable or method.
we cannot keep plain java code.
(hatever statements we)re keeping inside a Scriptlet, those should not end
with a semicolon +6,
3+ Delaration:
Syntax ' D7G field or method declaration 7A
"he JS declaration tag is used to declare fields and methods.
"he code written inside the jsp declaration tag is placed outside the
service+, method of auto generated servlet.
So it doesn)t get memory at each re1uest.
"he JS declaration tag is used to declare global members +global variables 5
definine methods,
In )3eclaration tag), we)re declaring global variables 5 we)re defining
entire methods. ;ut we)re not defining part of a method, +i.e, we)re not
keeping only method statements. (e)re developing entire method itself.
(here as in HScriptlet tagH, we)re defining part of method +i.e., only
statements which are defined like a inside method.
In )3eclaration tag), we can define entire method.
repared by Sashi I
Differene /et*een t#e ,sp sriptlet tag and ,sp delaration tag%
Jsp Sriptlet .ag Jsp Delaration .ag
"he jsp scriptlet tag can only
declare variables not methods.
"he jsp declaration tag can declare
variables as well as methods.
"he declaration of scriptlet tag is
placed inside the 2jspService+,
method.
"he declaration of jsp declaration tag is
placed outside the 2jspService+, method.
JSP Diretives :
"he jsp directives are messages that tells the web container how to
translate a JS page into the corresponding servlet.
"here are three types of directives'
page directive
include directive
taglib directive
import
content"ype
extends
info
buffer
language
is%$Ignored
is"hreadSafe
auto>lush
session
page%ncoding
errorage
is%rrorage
repared by Sashi J
1+ Sriptlet tag:
Program 3:
test3+,sp
D7
int i F *CC6
iKK6
7A
i value is ' D7F i 7A
-(tp(t:
i value is ' *C*
2+ Expression tag:
Program !:
test!+,sp
D7
int i F *CC6
iKK6
7A
D7
int j F i6
j KF 4CC6
7A
i value is ' D7F i 7A DbrA
j value is ' D7F j 7A
-utput'
i value is ' *C*
j value is ' @C*
Explanation:
!ere, we developed 4 Scriptlets.
Program 0:
test0+,sp
D7F *CCC 7A DbrA
D7F *C.*CIf 7A DbrA
D7F *CCC.*CCI 7A DbrA
D7F true 7A DbrA
D7F false 7A DbrA
D7F )c) 7A DbrA
D7F H!%$$-H 7A DbrA
D7F H!ello H K H ;uddyH 7A DbrA
D7F *CCK4CC 7A DbrA
D7F *CCL4CC 7A
repared by Sashi B
-(tp(t:
*CCC
*C.*CI
*CCC.*CCI
true
false
c
!%$$-
!ello ;uddy
@CC
4CCCC
Explanation:
(e)re rinting various types of literals 75 %xpressions.
Program 1:23(ltiple Sriptlets *it# 3(ltiple Expressions)
test1+,sp
D7
int i F*CC6
iKF *C6
7A
D7F Hi value is ' H Ki 7A DbrA
D7
iKF 4C6
iKK6
7A
D7F Hi value is ' H Ki 7A DbrA
D7
iKF @C6
iKK6
7A
i value is ' D7F i 7A
-(tp(t:
i value is ' **C
i value is ' *@*
i value is ' *J4
repared by Sashi M
Program 4:
test4+,sp
D7
java.util.3ate date F new java.util.3ate+,6
String s F HSashiH6
7A
3ate is ' D7F date 7A DbrA
s value ' D7F s 7A
-(tp(t:
3ate is ' (ed Apr *J CN'CC'@B IS" 4C*E
s value ' Sashi
3+ Delaration tag:
Program 5:
test5+,sp
D7G
int i F *CC6
void test+,
O
P
7A
i value is ' D7F i 7A
-(tp(t:
i value is ' *CC
Explanation:
!ere, in )3eclaration tag), we)re declaring i variable. )i) is a global variable.
And we)re defining a test+, method. test+, method also global method
Program 6:
test6+,sp
D7G
float f F 4CC.4f6
float test+,
O
return +*CCKf,6
P
7A
f value is ' D7F f 7A DbrA
test+, is ' D7F test+, 7A
-(tp(t:
f value is ' 4CC.4
test+, is ' @CC.4
repared by Sashi N
Program 17:
test17+,sp
D7G
String s FH.lobal variableH6
7A
S value is ' D7F
s
7A
-(tp(t:
S value is ' .lobal variable
Program 11:
test11+,sp
D7G
String s FH.lobal variableH6
7A
D7
String s F H$ocal variableH6
7A
S value is ' D7F
s
7A
-(tp(t:
S value is ' $ocal variable
Explanation:
!ere, in )3eclaration tag), we)re declared global variable )s).
same name )s) is declared in HScriptletH also.
"hen which one will get more preferenceQQ
Ruestion ' .lobal variable or $ocal variableQ
Answer ' $ocal variable only will get more preference.
repared by Sashi *C
Program 12:
Ruestion ' !ow to access global variableQ
Answer ' :se )this) keyword to access global variables.
test12+,sp
D7G
String s FH.lobal variableH6
7A
D7
String s F H$ocal variableH6
7A
.lobal S value is ' D7F this.s 7A DbrA
$ocal S value is ' D7F s 7A
-(tp(t:
.lobal S value is ' .lobal variable
$ocal S value is ' $ocal variable
Explanation:
!ere, in )3eclaration tag), we)re declared global variable )s).
same name )s) is declared in HScriptletH also.
$ocal 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+,sp
D7G
int day F 46
7A
D7
if+day FF *, O 7A
*st 3ay
D7 P else if +day FF4, O 7A
4nd 3ay
D7 P else O 7A
Some other 3ay
D7 P 7A
-(tp(t:
4nd 3ay
repared by Sashi **
Program 1!:
test1!+,sp
D7 int day F@6 7A
Switch Statement is used. DbrA
D7
switch+day, O
case *'
out.println+H3ay *H,6
break6
case 4'
out.println+H3ay 4H,6
break6
case @'
out.println+H3ay @H,6
break6
default'
out.println+HSome other dayH,6
P
7A
-(tp(t:
Switch Statement is used.
3ay @
Program 10:
test10+,sp
>or $oop %xample DbrA DbrA

D7G
int fontSi/e6
7A
D7 for +fontSi/e F *6 fontSi/eD@6 fontSi/eKK, O 7A
Dfont color F H0edHA D7F fontSi/e 7A
JS "utorial DbrA
D7 P 7A
-(tp(t:
>or $oop %xample
* JS "utorial
4 JS "utorial
repared by Sashi *4
Diretive tag:
Program 11:
test11+,sp
D7S page import F Hjava.util.3ateH 7A
D7
3ate d F new 3ate+,6
7A
D7F H"oday 3ate is ' H 7A
D7F d 7A
-(tp(t:
4nd 3ay
repared by Sashi *@

You might also like