SlideShare a Scribd company logo
1 of 22
CHAPTER 4:
SERVLETS AND JAVA SERVER PAGES
(JSP)
4.2 Using Java Server Pages (JSP)
Learning Outcomes
• At the end of the lectures, student should be able to:
– Define JSP
– Compare between Servlets and JSP
– Explain JSP Scripting Elements
– Describe Java Beans and JSP
– Illustrate the process output generated by Servlets
process
– Integrate Servlets and JSP
Introduction
• JSP files are HTML files with special Tags containing
Java source code that provide the development of static
and dynamic web content.
• Goal: Create static and dynamic web content for a Web
Application
• Mostly HTML page, with extension .jsp
• JSP was developed by Sun Microsystems
Example firstJsp.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<%
}
%>
<h3>Click <a href="firstJsp.jsp">Try Again</a><br></h3>
</body>
</html>
JSP interface
Servers that support JSP
• Apache Tomcat
• GlassFish Server
• Allaire Jrun
• JRun
• GNU JSP
Why use JSP
• JSP is easy to learn and allows developers to quickly
produce web sites and applications in an open and
standard way.
• JSP is based on Java, an object-oriented language.
• JSP offers a robust platform for web development.
• Main reasons to use JSP:
- Multi platform
- Advantages of Java.
Comparison Between JSP and
Servlet
JSP SERVLET
JSP is Java inside HTML
Java codes are embedded inside an
HTML page
Servlet is HTML inside Java
HTML page is produced using
Java's out.println() in a Java program.
JSP Scripting Elements (JSP
Tags)
• There are 5 JSP tags
1.JSP Scriptlet
2.JSP Expression
3.JSP Directive
4.JSP Comment
5.JSP Declaration
Explanation of JSP Tags
Tag Name Function
<% %> JSP scriptlet Untuk memasukkan blok kenyataan Java
To insert a block of Java statements.
<%= %> JSP expression Pernyataan Java di mana outputnya di satukan
ke dalam HTML
Java expression whose output is spliced into
HTML
<%@ %> JSP directive Untuk menetapkan syarat yang digunakan untuk
keseluruhan JSP
To set conditions that applies to the entire JSP.
<%-- --%> JSP comment Untuk memberitahu enjin JSP untuk
mengabaikan kod
To tell the JSP engine to ignore code.
<%! %> JSP declaration Untuk mengisytiharkan pembolehubah dan
method bagi JSP
To declare variables and methods for a JSP
JSP Scriptlet <% %>
<body>
<%
String user_id = request.getParameter("user_id");
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "");
Statement st = conn.createStatement();
String sql = "DELETE FROM users WHERE user_id=+'"+(user_id)
+"'";
st.executeUpdate(sql);
response.sendRedirect("application.jsp");
}
catch(Exception e){}
%>
</body>
JSP Expression <%=%>
• <h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
• <%= new java.util.Date() %>
JSP Directive <%@%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
JSP Comment <%-- --%>
<%--
Document : index.jsp
Author : User
--%>
JSP Declaration <%!%>
<%!
int globalCounter = 0;
java.util.Date startDate;
public void jspInit( ) {
startDate = new java.util.Date( );
}
public void jspDestroy( ) {
ServletContext context = getServletConfig().getServletContext( );
context.log("test.jsp was visited " + globalCounter +
" times between " + startDate + " and " + (new Date( )));
}
%>
Standard action elements
Describe Java Beans and JSP
• JavaBean is any class that
– Implements java.io.Serializable interface
– No-argument constructor
• Provide a few getter and setter method
• Never required for use with JSP
JavaBean action elements
Integrate Servlets and JSP
Process output generated by
Servlets process
Summary
• JSP was developed by Sun Microsystems to allow
server side development.
• JSP files are HTML files with special Tags containing
Java source code that provide the dynamic content.
• There are 5 JSP tags
• JSP Scriplet
• JSP Expression
• JSP Directive
• JSP Comment
• JSP Declaration

More Related Content

What's hot

What's hot (20)

Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16
 
Ch. 7 beeing a jsp
Ch. 7 beeing a jsp     Ch. 7 beeing a jsp
Ch. 7 beeing a jsp
 
Thinking in Components
Thinking in ComponentsThinking in Components
Thinking in Components
 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
 
4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού
4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού
4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού
 
Managing JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSManaging JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJS
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
RequireJS
RequireJSRequireJS
RequireJS
 
14 mvc
14 mvc14 mvc
14 mvc
 
276626961 java-j2 ee-course-curriculum-modular
276626961 java-j2 ee-course-curriculum-modular276626961 java-j2 ee-course-curriculum-modular
276626961 java-j2 ee-course-curriculum-modular
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase Connectivity
 
Jsp
JspJsp
Jsp
 
Arpita industrial trainingppt
Arpita industrial trainingpptArpita industrial trainingppt
Arpita industrial trainingppt
 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
 
ภาษาเจเอสพ (Jsp java 555
ภาษาเจเอสพ  (Jsp java 555ภาษาเจเอสพ  (Jsp java 555
ภาษาเจเอสพ (Jsp java 555
 
Jdbc
JdbcJdbc
Jdbc
 
Mule flow complete
Mule flow completeMule flow complete
Mule flow complete
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
 
Requirejs
RequirejsRequirejs
Requirejs
 
Mvc15 (1)
Mvc15 (1)Mvc15 (1)
Mvc15 (1)
 

Viewers also liked

Fp601 chapter 3 - part 2
Fp601   chapter 3 - part 2Fp601   chapter 3 - part 2
Fp601 chapter 3 - part 2Hemo Chella
 
Fp601 chapter 3 - part 1
Fp601   chapter 3 - part 1Fp601   chapter 3 - part 1
Fp601 chapter 3 - part 1Hemo Chella
 
Concept of demand & supply
Concept of demand & supplyConcept of demand & supply
Concept of demand & supplyShrutiSatija
 
Relationship Marketing ppt 31
Relationship  Marketing  ppt 31Relationship  Marketing  ppt 31
Relationship Marketing ppt 31ankit0231
 
Relationship marketing
Relationship  marketingRelationship  marketing
Relationship marketingNaveen Prasad
 
Relationship marketing concept, process and importance
Relationship marketing concept, process and importanceRelationship marketing concept, process and importance
Relationship marketing concept, process and importancegaurav jain
 

Viewers also liked (9)

Chap4 4 1
Chap4 4 1Chap4 4 1
Chap4 4 1
 
Fp601 chapter 3 - part 2
Fp601   chapter 3 - part 2Fp601   chapter 3 - part 2
Fp601 chapter 3 - part 2
 
Fp601 chapter 3 - part 1
Fp601   chapter 3 - part 1Fp601   chapter 3 - part 1
Fp601 chapter 3 - part 1
 
cyberpreneurship
cyberpreneurshipcyberpreneurship
cyberpreneurship
 
Concept of demand & supply
Concept of demand & supplyConcept of demand & supply
Concept of demand & supply
 
Relationship Marketing
Relationship MarketingRelationship Marketing
Relationship Marketing
 
Relationship Marketing ppt 31
Relationship  Marketing  ppt 31Relationship  Marketing  ppt 31
Relationship Marketing ppt 31
 
Relationship marketing
Relationship  marketingRelationship  marketing
Relationship marketing
 
Relationship marketing concept, process and importance
Relationship marketing concept, process and importanceRelationship marketing concept, process and importance
Relationship marketing concept, process and importance
 

Similar to Chap4 4 2 (20)

Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
10 jsp-scripting-elements
10 jsp-scripting-elements10 jsp-scripting-elements
10 jsp-scripting-elements
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
Jsp
JspJsp
Jsp
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp
JspJsp
Jsp
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
J2EE jsp_01
J2EE jsp_01J2EE jsp_01
J2EE jsp_01
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 

More from Hemo Chella

More from Hemo Chella (12)

Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Fp601 chapter 6
Fp601   chapter 6Fp601   chapter 6
Fp601 chapter 6
 
Fp601 chapter 5 -part 1
Fp601   chapter 5 -part 1Fp601   chapter 5 -part 1
Fp601 chapter 5 -part 1
 
Fp601 chapter 5 - part 2
Fp601   chapter 5 - part 2Fp601   chapter 5 - part 2
Fp601 chapter 5 - part 2
 
Fp601 chapter 4
Fp601   chapter 4Fp601   chapter 4
Fp601 chapter 4
 
Fp601 chapter 2
Fp601   chapter 2Fp601   chapter 2
Fp601 chapter 2
 
Fp601 chapter 1
Fp601   chapter 1Fp601   chapter 1
Fp601 chapter 1
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 

Chap4 4 2

  • 1. CHAPTER 4: SERVLETS AND JAVA SERVER PAGES (JSP) 4.2 Using Java Server Pages (JSP)
  • 2. Learning Outcomes • At the end of the lectures, student should be able to: – Define JSP – Compare between Servlets and JSP – Explain JSP Scripting Elements – Describe Java Beans and JSP – Illustrate the process output generated by Servlets process – Integrate Servlets and JSP
  • 3. Introduction • JSP files are HTML files with special Tags containing Java source code that provide the development of static and dynamic web content. • Goal: Create static and dynamic web content for a Web Application • Mostly HTML page, with extension .jsp • JSP was developed by Sun Microsystems
  • 5. <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <% double num = Math.random(); if (num > 0.95) { %> <h2>You'll have a luck day!</h2><p>(<%= num %>)</p> <% } else { %> <h2>Well, life goes on ... </h2><p>(<%= num %>)</p> <% } %> <h3>Click <a href="firstJsp.jsp">Try Again</a><br></h3> </body> </html>
  • 7. Servers that support JSP • Apache Tomcat • GlassFish Server • Allaire Jrun • JRun • GNU JSP
  • 8. Why use JSP • JSP is easy to learn and allows developers to quickly produce web sites and applications in an open and standard way. • JSP is based on Java, an object-oriented language. • JSP offers a robust platform for web development. • Main reasons to use JSP: - Multi platform - Advantages of Java.
  • 9. Comparison Between JSP and Servlet JSP SERVLET JSP is Java inside HTML Java codes are embedded inside an HTML page Servlet is HTML inside Java HTML page is produced using Java's out.println() in a Java program.
  • 10. JSP Scripting Elements (JSP Tags) • There are 5 JSP tags 1.JSP Scriptlet 2.JSP Expression 3.JSP Directive 4.JSP Comment 5.JSP Declaration
  • 11. Explanation of JSP Tags Tag Name Function <% %> JSP scriptlet Untuk memasukkan blok kenyataan Java To insert a block of Java statements. <%= %> JSP expression Pernyataan Java di mana outputnya di satukan ke dalam HTML Java expression whose output is spliced into HTML <%@ %> JSP directive Untuk menetapkan syarat yang digunakan untuk keseluruhan JSP To set conditions that applies to the entire JSP. <%-- --%> JSP comment Untuk memberitahu enjin JSP untuk mengabaikan kod To tell the JSP engine to ignore code. <%! %> JSP declaration Untuk mengisytiharkan pembolehubah dan method bagi JSP To declare variables and methods for a JSP
  • 12. JSP Scriptlet <% %> <body> <% String user_id = request.getParameter("user_id"); int sumcount=0; try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); Statement st = conn.createStatement(); String sql = "DELETE FROM users WHERE user_id=+'"+(user_id) +"'"; st.executeUpdate(sql); response.sendRedirect("application.jsp"); } catch(Exception e){} %> </body>
  • 13. JSP Expression <%=%> • <h2>You'll have a luck day!</h2><p>(<%= num %>)</p> • <%= new java.util.Date() %>
  • 14. JSP Directive <%@%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.sql.*"%> JSP Comment <%-- --%> <%-- Document : index.jsp Author : User --%>
  • 15. JSP Declaration <%!%> <%! int globalCounter = 0; java.util.Date startDate; public void jspInit( ) { startDate = new java.util.Date( ); } public void jspDestroy( ) { ServletContext context = getServletConfig().getServletContext( ); context.log("test.jsp was visited " + globalCounter + " times between " + startDate + " and " + (new Date( ))); } %>
  • 17. Describe Java Beans and JSP • JavaBean is any class that – Implements java.io.Serializable interface – No-argument constructor • Provide a few getter and setter method • Never required for use with JSP
  • 18.
  • 21. Process output generated by Servlets process
  • 22. Summary • JSP was developed by Sun Microsystems to allow server side development. • JSP files are HTML files with special Tags containing Java source code that provide the dynamic content. • There are 5 JSP tags • JSP Scriplet • JSP Expression • JSP Directive • JSP Comment • JSP Declaration

Editor's Notes

  1. Satu lagi tag JSP include directive &amp;lt;%@ include file = “url” %&amp;gt;