SlideShare a Scribd company logo
1 of 13
02-Up Close with Servlets DhrubojyotiKayal
Servlet responses Request Response Sequence Servlet URLs Servlet life cycle Agenda
Exercise
Servlet with simple text response public void doGet(HttpServletRequest request, HttpServletResponse response) 	throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); } Servlet Responses
Servlet with simple text response public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println 		("<!DOCTYPE html>" + 		"<html>" + 		"<head><title>A Test Servlet</title></head>" + 		"<body bgcolor=quot;#fdf5e6quot;>" + 		"<h1>Test</h1>" + 		"<p>Simple servlet for testing.</p>" + 		"</body></html>"); } Other forms – XML, Excel, Word, PDF …. Servlet Responses
Request Response Sequence
Java code com.empweb.web – package public class SimpleServlet extends HttpServlet web.xml register <servlet> 	<servlet-name>SimpleServlet</servlet-name> 	<servlet-class>com.empweb.web.SimpleServlet</servlet-class> 	</servlet> Map url <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/simpleservlet</url-pattern> </servlet-mapping> Final URL – http://domain/context/servlet-name Servlet URLs
init () Executed once when the servlet is first loaded. Not called for each request. service () Called in a new thread by server for each request. Dispatches to doGet, doPost, etc. Do not override this method! doGet () / doPost () / doXXX() Handles GET, POST, etc. requests. Override these to provide desired behavior. destroy() Called when server deletes servlet instance. Not called after each request. Servlet Lifecycle
Servlet Lifecycle
<servlet>  <servlet-name>MyServletName</servlet-name>  <servlet-class>com.mycompany.MyServlet</servlet-class>  <init-param> <param-name> param1 </param-name> <param-value> value1 </param-value> </init-param> <init-param> <param-name> param2 </param-name> <param-value> value2 </param-value> </init-param> ...  </servlet>  Servlet Initialization
@Override public void init() throws ServletException { 	String value = getServletConfig().getInitParameter("param1");  System.out.println(value); } Initial Parameter and Values
Create servlet instances before a request comes in Order of creating servlet instances Load on Startup
Q&A

More Related Content

What's hot

Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberRetrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberBruno Vieira
 
Testing Without Assertions
Testing Without AssertionsTesting Without Assertions
Testing Without AssertionsSAP SE
 
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...Alan Richardson
 
Dart Unit Testing
Dart Unit TestingDart Unit Testing
Dart Unit TestingMatt Norris
 
A Boring Article About a Check of the OpenSSL Project
A Boring Article About a Check of the OpenSSL ProjectA Boring Article About a Check of the OpenSSL Project
A Boring Article About a Check of the OpenSSL ProjectAndrey Karpov
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifePeter Gfader
 
Ajax World Comet Talk
Ajax World Comet TalkAjax World Comet Talk
Ajax World Comet Talkrajivmordani
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentationThanh Robi
 

What's hot (14)

Checking Bitcoin
 Checking Bitcoin Checking Bitcoin
Checking Bitcoin
 
PL/SQL Unit Testing Can Be Fun
PL/SQL Unit Testing Can Be FunPL/SQL Unit Testing Can Be Fun
PL/SQL Unit Testing Can Be Fun
 
Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberRetrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saber
 
Testing Without Assertions
Testing Without AssertionsTesting Without Assertions
Testing Without Assertions
 
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
 
Dart Unit Testing
Dart Unit TestingDart Unit Testing
Dart Unit Testing
 
A Boring Article About a Check of the OpenSSL Project
A Boring Article About a Check of the OpenSSL ProjectA Boring Article About a Check of the OpenSSL Project
A Boring Article About a Check of the OpenSSL Project
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
Testacular
TestacularTestacular
Testacular
 
Ajax World Comet Talk
Ajax World Comet TalkAjax World Comet Talk
Ajax World Comet Talk
 
PL WEB
PL WEBPL WEB
PL WEB
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
jUnit
jUnitjUnit
jUnit
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 

Viewers also liked

Viewers also liked (20)

Celula irene angelica
Celula irene angelicaCelula irene angelica
Celula irene angelica
 
09 packages
09   packages09   packages
09 packages
 
10 access control
10   access control10   access control
10 access control
 
07 flow control
07   flow control07   flow control
07 flow control
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 
08 class and object
08   class and object08   class and object
08 class and object
 
Celulas sara alicia ismael elena.
Celulas  sara alicia ismael elena.Celulas  sara alicia ismael elena.
Celulas sara alicia ismael elena.
 
Fitness Effect
Fitness EffectFitness Effect
Fitness Effect
 
01 handshake
01   handshake01   handshake
01 handshake
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
16 containers
16   containers16   containers
16 containers
 
02 what is java
02   what is java02   what is java
02 what is java
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
Trabajo sergio,manuel,laura ycarolina
Trabajo sergio,manuel,laura ycarolina Trabajo sergio,manuel,laura ycarolina
Trabajo sergio,manuel,laura ycarolina
 
Week1
Week1Week1
Week1
 
11 static
11   static11   static
11 static
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
frankrijk.nl
frankrijk.nl frankrijk.nl
frankrijk.nl
 
C4021 Séance 8: Réseaux Sociaux
C4021 Séance 8: Réseaux SociauxC4021 Séance 8: Réseaux Sociaux
C4021 Séance 8: Réseaux Sociaux
 

Similar to 02 up close with servlets

Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsServlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsJavaEE Trainers
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3mihirio
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentationipolevoy
 
To convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfTo convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfsutharbharat59
 
05 status-codes
05 status-codes05 status-codes
05 status-codessnopteck
 
Sqladria 2009 SRC
Sqladria 2009 SRCSqladria 2009 SRC
Sqladria 2009 SRCtepsum
 
Server-side Technologies in Java
Server-side Technologies in JavaServer-side Technologies in Java
Server-side Technologies in JavaAnirban Majumdar
 
Basic testing with selenium
Basic testing with seleniumBasic testing with selenium
Basic testing with seleniumSøren Lund
 
005428058.pdf
005428058.pdf005428058.pdf
005428058.pdfEidTahir
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introductionTomi Juhola
 
The vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQLThe vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQLLukas Eder
 
Unit testing CourseSites Apache Filter
Unit testing CourseSites Apache FilterUnit testing CourseSites Apache Filter
Unit testing CourseSites Apache FilterWayan Wira
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basicssnopteck
 
JSP diana y yo
JSP diana y yoJSP diana y yo
JSP diana y yomichael
 

Similar to 02 up close with servlets (20)

servlets
servletsservlets
servlets
 
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsServlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
To convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfTo convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdf
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
 
Sqladria 2009 SRC
Sqladria 2009 SRCSqladria 2009 SRC
Sqladria 2009 SRC
 
Jsp
JspJsp
Jsp
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Server-side Technologies in Java
Server-side Technologies in JavaServer-side Technologies in Java
Server-side Technologies in Java
 
Basic testing with selenium
Basic testing with seleniumBasic testing with selenium
Basic testing with selenium
 
005428058.pdf
005428058.pdf005428058.pdf
005428058.pdf
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
The vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQLThe vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQL
 
Unit testing CourseSites Apache Filter
Unit testing CourseSites Apache FilterUnit testing CourseSites Apache Filter
Unit testing CourseSites Apache Filter
 
Presentation
PresentationPresentation
Presentation
 
C:\fakepath\jsp01
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
JSP diana y yo
JSP diana y yoJSP diana y yo
JSP diana y yo
 

More from dhrubo kayal

01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 

More from dhrubo kayal (9)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
05 operators
05   operators05   operators
05 operators
 

02 up close with servlets

  • 1. 02-Up Close with Servlets DhrubojyotiKayal
  • 2. Servlet responses Request Response Sequence Servlet URLs Servlet life cycle Agenda
  • 4. Servlet with simple text response public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); } Servlet Responses
  • 5. Servlet with simple text response public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println ("<!DOCTYPE html>" + "<html>" + "<head><title>A Test Servlet</title></head>" + "<body bgcolor=quot;#fdf5e6quot;>" + "<h1>Test</h1>" + "<p>Simple servlet for testing.</p>" + "</body></html>"); } Other forms – XML, Excel, Word, PDF …. Servlet Responses
  • 7. Java code com.empweb.web – package public class SimpleServlet extends HttpServlet web.xml register <servlet> <servlet-name>SimpleServlet</servlet-name> <servlet-class>com.empweb.web.SimpleServlet</servlet-class> </servlet> Map url <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/simpleservlet</url-pattern> </servlet-mapping> Final URL – http://domain/context/servlet-name Servlet URLs
  • 8. init () Executed once when the servlet is first loaded. Not called for each request. service () Called in a new thread by server for each request. Dispatches to doGet, doPost, etc. Do not override this method! doGet () / doPost () / doXXX() Handles GET, POST, etc. requests. Override these to provide desired behavior. destroy() Called when server deletes servlet instance. Not called after each request. Servlet Lifecycle
  • 10. <servlet> <servlet-name>MyServletName</servlet-name> <servlet-class>com.mycompany.MyServlet</servlet-class> <init-param> <param-name> param1 </param-name> <param-value> value1 </param-value> </init-param> <init-param> <param-name> param2 </param-name> <param-value> value2 </param-value> </init-param> ... </servlet> Servlet Initialization
  • 11. @Override public void init() throws ServletException { String value = getServletConfig().getInitParameter("param1"); System.out.println(value); } Initial Parameter and Values
  • 12. Create servlet instances before a request comes in Order of creating servlet instances Load on Startup
  • 13. Q&A