SlideShare a Scribd company logo
1 of 20
Java JSPs and Servlets
BITM 3730
Developing Web Applications
Previous Work Review
• http://pirate.shu.edu/~marinom6/work.html
• Please Note only previously due HW assignments are posted on my
pirate.shu.edu web space
• Begin organizing your creating files for this course into an easy to find folder
on your desktop for easy FTP later on
Basics
• Java programming language can be embedded into JSP
• JSP stands for Java Server Pages
• JSP is compiled on servlets
• JSP is a server-side web technology
• The primary function of JSP is rendering content
• The primary function of a servlet is processing
JSP – Java Server Page
• Based on HTML. JSP pages can be based on HTML pages, just change
the extension
• Server-side web technology
• Compiled into servlets at runtime
• Allows for embedding of Java code directly into the script using
<%.....%>
• Requires Apache Tomcat installation on server
Servlet
• Compiled code used to deliver content over the HTTP protocol
• Developed as a Java class conforming to the Java Servlet API
• Typically used in conjunction with JSPs for more extensive processing
JSP vs Servlet
• JSPs are more geared towards rendering content
• Servlets are better suited for processing since they are pre-compiled
• Consider the concept of Model-View-Controller (MVC)
• Model is your business model which houses all of the business logic
• View is your users’ view into your application. In this case it would be JSPs
• Controller is the glue between the model and the view
• Spring and Struts are two popular MVCs used in Java web applications
• Servlets will typically process request data, enrich it (process it) and forward the request
onto a JSP for display
Working Together
• JavaServer Pages (JSP) is a Java standard technology that enables you to write
dynamic, data-driven pages for your Java web applications.
• JSP is built on top of the Java Servlet specification.
• The two technologies typically work together, especially in older Java web
applications.
• From a coding perspective, the most obvious difference between them is that with
servlets you write Java code and then embed client-side markup (like HTML) into
that code, whereas with JSP you start with the client-side script or markup, then
embed JSP tags to connect your page to the Java backend.
JSP vs. Everyone Else
• JSP vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part
is written in Java, not Visual Basic or other MS specific language, so it is more powerful and
easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
• JSP vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to
have plenty of println statements that generate the HTML.
• JSP vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for
"real" programs that use form data, make database connections, and the like.
• JSP vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly
interact with the web server to perform complex tasks like database access and image processing
etc.
• JSP vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.
Methods to Set HTTP Status Code
S.N
o.
Method & Description
1
public void setStatus ( int statusCode )
This method sets an arbitrary status code. The setStatus method
takes an int (the status code) as an argument. If your response
includes a special status code and a document, be sure to
call setStatus before actually returning any of the content with
the PrintWriter.
2
public void sendRedirect(String url)
This method generates a 302 response along with a Location header
giving the URL of the new document.
3
public void sendError(int code, String message)
This method sends a status code (usually 404) along with a short
message that is automatically formatted inside an HTML document
and sent to the client.
Applications of Servlet
• Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page
or it could also come from an applet or a custom HTTP client program.
• Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media
types and compression schemes the browser understands, and so forth.
• Process the data and generate the results. This process may require talking to a database,
executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
• Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in
a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
• Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or
other clients what type of document is being returned (e.g., HTML), setting cookies and caching
parameters, and other such tasks.
Visually
init
public void init(ServletConfig config)
throws ServletException
• Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
• The servlet container calls the init method exactly once after instantiating the servlet. The init
method must complete successfully before the servlet can receive any requests.
• The servlet container cannot place the servlet into service if the init method
• Throws a ServletException
• Does not return within a time period defined by the Web server
destroy
public void destroy()
• Called by the servlet container to indicate to a servlet that the servlet is being taken
out of service. This method is only called once all threads within the servlet's
service method have exited or after a timeout period has passed. After the servlet
container calls this method, it will not call the service method again on this servlet.
• This method gives the servlet an opportunity to clean up any resources that are
being held (for example, memory, file handles, threads) and make sure that any
persistent state is synchronized with the servlet's current state in memory.
Servlet Life Cycle
• Servlet life cycle is governed by init(), service(), and destroy().
• The init() method is called when the servlet is loaded and executes only once.
• After the servlet has been initialized, the service() method is invoked to process a
request.
• The servlet remains in the server address space until it is terminated by the server.
Servlet resources are released by calling destroy().
• No calls to service() are made after destroy() is invoked.
GUIs
• A GUI (graphical user interface) is a system of interactive visual components
for computer software.
• A GUI displays objects that convey information and represent actions that
can be taken by the user.
• The objects change color, size, or visibility when the user interacts with them
Building Assignment 12
<html>
<head><title>First JSP</title></head>
<body>
<%
double num = Math.random();
if (num > 0.99) {
%>
<h2>You'll have a lucky day!</h2><p>(<%= num
%>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num
%>)</p>
<%
}
%>
<a href="<%= request.getRequestURI() %>"><h3>Try
Again</h3></a>
</body>
</html>
Building Project 5
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ...Servlet extends HttpServlet {
// Runs when the servlet is loaded onto the server.
public void init() {
......
}
Building Project 5
// Runs on a thread whenever there is HTTP GET request
// Take 2 arguments, corresponding to HTTP request and response
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the MIME type for the response message
response.setContentType("text/html");
// Write to network
PrintWriter out = response.getWriter();
// Your servlet's logic here
out.println("<html>");
out.println( ...... );
out.println("</html>");
}
Building Project 5
// Runs as a thread whenever there is HTTP POST request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// do the same thing as HTTP GET request
doGet(request, response);
}
Building Project 5
// Runs when the servlet is unloaded from the server.
public void destroy() {
......
}
// Other instance variables and methods
}

More Related Content

Similar to BITM3730Week12.pptx

Servlet.ppt
Servlet.pptServlet.ppt
Servlet.pptkstalin2
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2divzi1913
 
Project First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedProject First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedarya krazydude
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivitypkaviya
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2Long Nguyen
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 
Online grocery store
Online grocery storeOnline grocery store
Online grocery storeKavita Sharma
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...MathivananP4
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet backdoor
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and ServletsRaghu nath
 
Servlets as introduction (Advanced programming)
Servlets as introduction (Advanced programming)Servlets as introduction (Advanced programming)
Servlets as introduction (Advanced programming)Gera Paulos
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 

Similar to BITM3730Week12.pptx (20)

Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Project First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedProject First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be used
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Major project report
Major project reportMajor project report
Major project report
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Servlets
ServletsServlets
Servlets
 
Online grocery store
Online grocery storeOnline grocery store
Online grocery store
 
20jsp
20jsp20jsp
20jsp
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Servlets
ServletsServlets
Servlets
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
Servlets as introduction (Advanced programming)
Servlets as introduction (Advanced programming)Servlets as introduction (Advanced programming)
Servlets as introduction (Advanced programming)
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 

More from MattMarino13

1-22-24 INFO 2106.pptx
1-22-24 INFO 2106.pptx1-22-24 INFO 2106.pptx
1-22-24 INFO 2106.pptxMattMarino13
 
1-24-24 INFO 3205.pptx
1-24-24 INFO 3205.pptx1-24-24 INFO 3205.pptx
1-24-24 INFO 3205.pptxMattMarino13
 
BITM3730 11-14.pptx
BITM3730 11-14.pptxBITM3730 11-14.pptx
BITM3730 11-14.pptxMattMarino13
 
01_Felke-Morris_Lecture_ppt_ch01.pptx
01_Felke-Morris_Lecture_ppt_ch01.pptx01_Felke-Morris_Lecture_ppt_ch01.pptx
01_Felke-Morris_Lecture_ppt_ch01.pptxMattMarino13
 
02slide_accessible.pptx
02slide_accessible.pptx02slide_accessible.pptx
02slide_accessible.pptxMattMarino13
 
Hoisington_Android_4e_PPT_CH01.pptx
Hoisington_Android_4e_PPT_CH01.pptxHoisington_Android_4e_PPT_CH01.pptx
Hoisington_Android_4e_PPT_CH01.pptxMattMarino13
 
AndroidHTP3_AppA.pptx
AndroidHTP3_AppA.pptxAndroidHTP3_AppA.pptx
AndroidHTP3_AppA.pptxMattMarino13
 
9780357132302_Langley11e_ch1_LEAP.pptx
9780357132302_Langley11e_ch1_LEAP.pptx9780357132302_Langley11e_ch1_LEAP.pptx
9780357132302_Langley11e_ch1_LEAP.pptxMattMarino13
 
krajewski_om12 _01.pptx
krajewski_om12 _01.pptxkrajewski_om12 _01.pptx
krajewski_om12 _01.pptxMattMarino13
 
CapsimOpsIntroPPT.Marino.pptx
CapsimOpsIntroPPT.Marino.pptxCapsimOpsIntroPPT.Marino.pptx
CapsimOpsIntroPPT.Marino.pptxMattMarino13
 
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptx
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptxProject Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptx
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptxMattMarino13
 
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptx
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptxProject Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptx
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptxMattMarino13
 
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...MattMarino13
 
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...MattMarino13
 
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...MattMarino13
 
1-23-19 Agenda.pptx
1-23-19 Agenda.pptx1-23-19 Agenda.pptx
1-23-19 Agenda.pptxMattMarino13
 
EDF 8289 Marino PPT.pptx
EDF 8289 Marino PPT.pptxEDF 8289 Marino PPT.pptx
EDF 8289 Marino PPT.pptxMattMarino13
 
Agenda January 20th 2016.pptx
Agenda January 20th 2016.pptxAgenda January 20th 2016.pptx
Agenda January 20th 2016.pptxMattMarino13
 
BITM3730 8-29.pptx
BITM3730 8-29.pptxBITM3730 8-29.pptx
BITM3730 8-29.pptxMattMarino13
 
BITM3730 8-30.pptx
BITM3730 8-30.pptxBITM3730 8-30.pptx
BITM3730 8-30.pptxMattMarino13
 

More from MattMarino13 (20)

1-22-24 INFO 2106.pptx
1-22-24 INFO 2106.pptx1-22-24 INFO 2106.pptx
1-22-24 INFO 2106.pptx
 
1-24-24 INFO 3205.pptx
1-24-24 INFO 3205.pptx1-24-24 INFO 3205.pptx
1-24-24 INFO 3205.pptx
 
BITM3730 11-14.pptx
BITM3730 11-14.pptxBITM3730 11-14.pptx
BITM3730 11-14.pptx
 
01_Felke-Morris_Lecture_ppt_ch01.pptx
01_Felke-Morris_Lecture_ppt_ch01.pptx01_Felke-Morris_Lecture_ppt_ch01.pptx
01_Felke-Morris_Lecture_ppt_ch01.pptx
 
02slide_accessible.pptx
02slide_accessible.pptx02slide_accessible.pptx
02slide_accessible.pptx
 
Hoisington_Android_4e_PPT_CH01.pptx
Hoisington_Android_4e_PPT_CH01.pptxHoisington_Android_4e_PPT_CH01.pptx
Hoisington_Android_4e_PPT_CH01.pptx
 
AndroidHTP3_AppA.pptx
AndroidHTP3_AppA.pptxAndroidHTP3_AppA.pptx
AndroidHTP3_AppA.pptx
 
9780357132302_Langley11e_ch1_LEAP.pptx
9780357132302_Langley11e_ch1_LEAP.pptx9780357132302_Langley11e_ch1_LEAP.pptx
9780357132302_Langley11e_ch1_LEAP.pptx
 
krajewski_om12 _01.pptx
krajewski_om12 _01.pptxkrajewski_om12 _01.pptx
krajewski_om12 _01.pptx
 
CapsimOpsIntroPPT.Marino.pptx
CapsimOpsIntroPPT.Marino.pptxCapsimOpsIntroPPT.Marino.pptx
CapsimOpsIntroPPT.Marino.pptx
 
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptx
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptxProject Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptx
Project Presentation_castroxa_attempt_2021-12-05-18-30-10_No Cap.pptx
 
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptx
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptxProject Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptx
Project Presentation_mirzamad_attempt_2021-12-05-23-35-25_HTML_presentation.pptx
 
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...
Project Presentation_padillni_attempt_2021-12-05-18-52-37_Web Application Pre...
 
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...
Project Presentation_thomasb1_attempt_2021-12-05-17-50-13_Developing Web Apps...
 
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...
Project Presentation_hernana1_attempt_2021-12-05-22-06-56_Miyamoto BITM 3730 ...
 
1-23-19 Agenda.pptx
1-23-19 Agenda.pptx1-23-19 Agenda.pptx
1-23-19 Agenda.pptx
 
EDF 8289 Marino PPT.pptx
EDF 8289 Marino PPT.pptxEDF 8289 Marino PPT.pptx
EDF 8289 Marino PPT.pptx
 
Agenda January 20th 2016.pptx
Agenda January 20th 2016.pptxAgenda January 20th 2016.pptx
Agenda January 20th 2016.pptx
 
BITM3730 8-29.pptx
BITM3730 8-29.pptxBITM3730 8-29.pptx
BITM3730 8-29.pptx
 
BITM3730 8-30.pptx
BITM3730 8-30.pptxBITM3730 8-30.pptx
BITM3730 8-30.pptx
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
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
 
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
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
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
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
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
 
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
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
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 🔝✔️✔️
 
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🔝
 
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
 
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
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
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
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
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
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
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Ă...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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
 

BITM3730Week12.pptx

  • 1. Java JSPs and Servlets BITM 3730 Developing Web Applications
  • 2. Previous Work Review • http://pirate.shu.edu/~marinom6/work.html • Please Note only previously due HW assignments are posted on my pirate.shu.edu web space • Begin organizing your creating files for this course into an easy to find folder on your desktop for easy FTP later on
  • 3. Basics • Java programming language can be embedded into JSP • JSP stands for Java Server Pages • JSP is compiled on servlets • JSP is a server-side web technology • The primary function of JSP is rendering content • The primary function of a servlet is processing
  • 4. JSP – Java Server Page • Based on HTML. JSP pages can be based on HTML pages, just change the extension • Server-side web technology • Compiled into servlets at runtime • Allows for embedding of Java code directly into the script using <%.....%> • Requires Apache Tomcat installation on server
  • 5. Servlet • Compiled code used to deliver content over the HTTP protocol • Developed as a Java class conforming to the Java Servlet API • Typically used in conjunction with JSPs for more extensive processing
  • 6. JSP vs Servlet • JSPs are more geared towards rendering content • Servlets are better suited for processing since they are pre-compiled • Consider the concept of Model-View-Controller (MVC) • Model is your business model which houses all of the business logic • View is your users’ view into your application. In this case it would be JSPs • Controller is the glue between the model and the view • Spring and Struts are two popular MVCs used in Java web applications • Servlets will typically process request data, enrich it (process it) and forward the request onto a JSP for display
  • 7. Working Together • JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic, data-driven pages for your Java web applications. • JSP is built on top of the Java Servlet specification. • The two technologies typically work together, especially in older Java web applications. • From a coding perspective, the most obvious difference between them is that with servlets you write Java code and then embed client-side markup (like HTML) into that code, whereas with JSP you start with the client-side script or markup, then embed JSP tags to connect your page to the Java backend.
  • 8. JSP vs. Everyone Else • JSP vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers. • JSP vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML. • JSP vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like. • JSP vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc. • JSP vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.
  • 9. Methods to Set HTTP Status Code S.N o. Method & Description 1 public void setStatus ( int statusCode ) This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter. 2 public void sendRedirect(String url) This method generates a 302 response along with a Location header giving the URL of the new document. 3 public void sendError(int code, String message) This method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client.
  • 10. Applications of Servlet • Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program. • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth. • Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly. • Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc. • Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.
  • 12. init public void init(ServletConfig config) throws ServletException • Called by the servlet container to indicate to a servlet that the servlet is being placed into service. • The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests. • The servlet container cannot place the servlet into service if the init method • Throws a ServletException • Does not return within a time period defined by the Web server
  • 13. destroy public void destroy() • Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet. • This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.
  • 14. Servlet Life Cycle • Servlet life cycle is governed by init(), service(), and destroy(). • The init() method is called when the servlet is loaded and executes only once. • After the servlet has been initialized, the service() method is invoked to process a request. • The servlet remains in the server address space until it is terminated by the server. Servlet resources are released by calling destroy(). • No calls to service() are made after destroy() is invoked.
  • 15. GUIs • A GUI (graphical user interface) is a system of interactive visual components for computer software. • A GUI displays objects that convey information and represent actions that can be taken by the user. • The objects change color, size, or visibility when the user interacts with them
  • 16. Building Assignment 12 <html> <head><title>First JSP</title></head> <body> <% double num = Math.random(); if (num > 0.99) { %> <h2>You'll have a lucky day!</h2><p>(<%= num %>)</p> <% } else { %> <h2>Well, life goes on ... </h2><p>(<%= num %>)</p> <% } %> <a href="<%= request.getRequestURI() %>"><h3>Try Again</h3></a> </body> </html>
  • 17. Building Project 5 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ...Servlet extends HttpServlet { // Runs when the servlet is loaded onto the server. public void init() { ...... }
  • 18. Building Project 5 // Runs on a thread whenever there is HTTP GET request // Take 2 arguments, corresponding to HTTP request and response public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Set the MIME type for the response message response.setContentType("text/html"); // Write to network PrintWriter out = response.getWriter(); // Your servlet's logic here out.println("<html>"); out.println( ...... ); out.println("</html>"); }
  • 19. Building Project 5 // Runs as a thread whenever there is HTTP POST request public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // do the same thing as HTTP GET request doGet(request, response); }
  • 20. Building Project 5 // Runs when the servlet is unloaded from the server. public void destroy() { ...... } // Other instance variables and methods }

Editor's Notes

  1. https://www3.ntu.edu.sg/home/ehchua/programming/howto/images/HTTP_ClientServerSystem.png