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

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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
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
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
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
 
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
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 

Recently uploaded (20)

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
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
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
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
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Ă...
 
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...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 

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;