SlideShare a Scribd company logo
Knowledge Sharing
Agenda What is Servlet? Servlet hierarchy & lifecycle Servlet program structure Deploying servlets on Tomcat HTTP Servlets and HTTP request methods Understanding servlet API Responding to requests Accessing form input data Working with header fields URL redirecting
What is servlet? A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests.  A Servlet is an object that receives a request and generates a response based on that request Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET ( http://en.wikipedia.org/wiki/Java_Servlet )
Advantages of Servlets Faster than CGI scripts because use a different process model. Use standard API that is supported by many Web servers. Have all of the advantages of the Java languages, including ease of development and platform independence. Can access the large set of APIs available for the Java platform.   ( http://java.sun.com )
Servlet V.S. CGI A  s ervlet stays in memory between requests. A CGI program   needs to be loaded and started for each CGI request . A servlet doesn’t run in a separate process.  This removes the overhead of creating a new process for each request  which CGI does. A servlet allows each request to be handled by a separate  Java thread  within the web server process (same amount of threads as request but there only be one copy of the servlet class crea ted in memory).  ( http://www.novocode.com/doc/servlet-essentials/chapter1.html )
Servlet Hierarchy Servlets (javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (javax.servlet.http.HttpServlet) MyServlet
Servlet Lifecycle
Servlet Lifecycle (cont.)
Servlet Program Structure
[Screencast] Create simple servlet
[Showcase] Servlet Lifecycle
[Showcase] Servlet Lifecycle (cont.)
[Showcase] Servlet Lifecycle (cont.)
Deploying Servlets on Tomcat Create a servlet Compile the servlet : No different from compiling java program  ( javac OurServlet.java ) javax.servlet.*  and  javax.servlet.http.*  must be added in Classpath. Create web application folder under webapps folder in Tomcat (web container). Create WEB-INF folder under web application folder. http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
Deploying Servlets on Tomcat (cont) Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. Copy the servlet class file into ‘classes’ folder  Edit web.xml to include servlet’s name and url pattern .  http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
Deploying Servlets on Tomcat (cont) Run Tomcat server and then execute the Servlet http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
HTTP Servlet and HTTP request method
HTTP Servlet and HTTP request method (cont.) HTTP is a request-response oriented protocol. An HTTP request consists of : Request method ( GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE) URI Header  fields Body An HTTP response contains : Result code Header fields and body http://www.novocode.com/doc/servlet-essentials/chapter1.html
Understanding Servlet API Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
Understanding Servlet API (cont) Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
Request  Heade r Accept Accept-charset Accept-encoding Accept-language Authorization Connection Content-length Cookie The most common headers : From Host If-modified-since Pragma Referer User-agent UA-Pixels, UA-color, UA-OS, UA-CPU http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
Request  Heade r (cont.) Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. Call the getHeaderNames() to get an Enumeration of all header names received on the particular request. Reading request header from Servlets  : http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
Accessing form input data Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument.  Call getParameterValues() method to get the parameter that probably have more than one value. Call getParameterNames() to get full list of all parameters received on the particular request. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
URL Redirecting Using sendRedirect() method of HttpServletResponse class. Using forward() method of RequestDispatcher class. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
URL Redirecting (cont.) sendRedirect()  It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value) RequestDispatcher The target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute(). http://www.theserverside.com/discussions/thread.tss?thread_id=26425
URL Redirecting (cont.) sendRedirect()  It will updates the browser history.  RequestDispatcher If use  RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3. Both kinds of redirections are useful, depending on the precise effect you want. http://www.theserverside.com/discussions/thread.tss?thread_id=26425
Thank you Fahmi Jafar (fahmijafar@fahmijafar.net)

More Related Content

PPTX
Android app development ppt
PPTX
Reactjs
PPTX
Full stack development
PDF
Gradle Introduction
PPT
SOLID Design Principles
PPTX
JavaScript Event Loop
PPT
Exception Handling in JAVA
PPTX
Android app development ppt
Reactjs
Full stack development
Gradle Introduction
SOLID Design Principles
JavaScript Event Loop
Exception Handling in JAVA

What's hot (20)

PPTX
Android Widget
PPT
Mvc architecture
PDF
Workshop 22: ReactJS Redux Advanced
PDF
Spring Boot
KEY
Introduction to android testing
PPTX
Introduction to java
PPTX
Introduction to Node.js
PPTX
React hooks
PPTX
Introduction to Node js
PPTX
Android User Interface
PPTX
Angularjs PPT
PPT
Networking & Socket Programming In Java
PDF
Introduction to django framework
PPTX
Android studio ppt
PPTX
SQLite database in android
PPT
android activity
PPT
Singleton design pattern
PPTX
Java Swing
PDF
Android: Intent, Intent Filter, Broadcast Receivers
PPT
Java: GUI
Android Widget
Mvc architecture
Workshop 22: ReactJS Redux Advanced
Spring Boot
Introduction to android testing
Introduction to java
Introduction to Node.js
React hooks
Introduction to Node js
Android User Interface
Angularjs PPT
Networking & Socket Programming In Java
Introduction to django framework
Android studio ppt
SQLite database in android
android activity
Singleton design pattern
Java Swing
Android: Intent, Intent Filter, Broadcast Receivers
Java: GUI
Ad

Viewers also liked (20)

PPT
Java servlet life cycle - methods ppt
PPTX
java Servlet technology
PPT
Java Servlets
PPT
The Timer
RTF
Servlet lifecycle
PDF
Aborto salud-psicologica-adolescentes
PDF
Rep trak2012 leaflet
PPTX
Schengen vizesi
PPT
Java - Servlet - Mazenet Solution
PDF
Servlet
PPT
Web Tech Java Servlet Update1
PPT
Java Servlet
PDF
Introduction tomcat7 servlet3
PDF
Enterprise(d) Tomcat & httpd
PPTX
Ob1k presentation at Java.IL
PDF
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
PDF
Evolve13 cq-commerce-framework
PPTX
e-Eommerce - Framework
PPTX
Websockets on the JVM: Atmosphere to the rescue!
PPTX
Asynchronous design with Spring and RTI: 1M events per second
Java servlet life cycle - methods ppt
java Servlet technology
Java Servlets
The Timer
Servlet lifecycle
Aborto salud-psicologica-adolescentes
Rep trak2012 leaflet
Schengen vizesi
Java - Servlet - Mazenet Solution
Servlet
Web Tech Java Servlet Update1
Java Servlet
Introduction tomcat7 servlet3
Enterprise(d) Tomcat & httpd
Ob1k presentation at Java.IL
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Evolve13 cq-commerce-framework
e-Eommerce - Framework
Websockets on the JVM: Atmosphere to the rescue!
Asynchronous design with Spring and RTI: 1M events per second
Ad

Similar to Knowledge Sharing : Java Servlet (20)

PPT
Servlet (1) also contains code to create it.ppt
PPT
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
PPT
JAVA Servlets
PPT
Servlet1.ppt
PPT
Servlet.ppt
PPT
Servlet.ppt
PPT
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
PPT
1 java servlets and jsp
PPT
PPTX
Chapter 3 servlet & jsp
PDF
Java Servlets.pdf
DOC
Unit5 servlets
PPTX
Servlets api overview
PPT
Servlets
ODP
Servlets
DOC
Servlet basics
PPTX
Http Server Programming in JAVA - Handling http requests and responses
DOC
Java Servlets & JSP
PPTX
Servlets
PDF
Dynamic content generation
Servlet (1) also contains code to create it.ppt
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
JAVA Servlets
Servlet1.ppt
Servlet.ppt
Servlet.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
1 java servlets and jsp
Chapter 3 servlet & jsp
Java Servlets.pdf
Unit5 servlets
Servlets api overview
Servlets
Servlets
Servlet basics
Http Server Programming in JAVA - Handling http requests and responses
Java Servlets & JSP
Servlets
Dynamic content generation

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Spectroscopy.pptx food analysis technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
sap open course for s4hana steps from ECC to s4
Spectroscopy.pptx food analysis technology
Assigned Numbers - 2025 - Bluetooth® Document
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...

Knowledge Sharing : Java Servlet

  • 2. Agenda What is Servlet? Servlet hierarchy & lifecycle Servlet program structure Deploying servlets on Tomcat HTTP Servlets and HTTP request methods Understanding servlet API Responding to requests Accessing form input data Working with header fields URL redirecting
  • 3. What is servlet? A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests. A Servlet is an object that receives a request and generates a response based on that request Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET ( http://en.wikipedia.org/wiki/Java_Servlet )
  • 4. Advantages of Servlets Faster than CGI scripts because use a different process model. Use standard API that is supported by many Web servers. Have all of the advantages of the Java languages, including ease of development and platform independence. Can access the large set of APIs available for the Java platform. ( http://java.sun.com )
  • 5. Servlet V.S. CGI A s ervlet stays in memory between requests. A CGI program needs to be loaded and started for each CGI request . A servlet doesn’t run in a separate process. This removes the overhead of creating a new process for each request which CGI does. A servlet allows each request to be handled by a separate Java thread within the web server process (same amount of threads as request but there only be one copy of the servlet class crea ted in memory). ( http://www.novocode.com/doc/servlet-essentials/chapter1.html )
  • 6. Servlet Hierarchy Servlets (javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (javax.servlet.http.HttpServlet) MyServlet
  • 14. Deploying Servlets on Tomcat Create a servlet Compile the servlet : No different from compiling java program ( javac OurServlet.java ) javax.servlet.* and javax.servlet.http.* must be added in Classpath. Create web application folder under webapps folder in Tomcat (web container). Create WEB-INF folder under web application folder. http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 15. Deploying Servlets on Tomcat (cont) Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. Copy the servlet class file into ‘classes’ folder Edit web.xml to include servlet’s name and url pattern . http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 16. Deploying Servlets on Tomcat (cont) Run Tomcat server and then execute the Servlet http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 17. HTTP Servlet and HTTP request method
  • 18. HTTP Servlet and HTTP request method (cont.) HTTP is a request-response oriented protocol. An HTTP request consists of : Request method ( GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE) URI Header fields Body An HTTP response contains : Result code Header fields and body http://www.novocode.com/doc/servlet-essentials/chapter1.html
  • 19. Understanding Servlet API Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  • 20. Understanding Servlet API (cont) Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  • 21. Request Heade r Accept Accept-charset Accept-encoding Accept-language Authorization Connection Content-length Cookie The most common headers : From Host If-modified-since Pragma Referer User-agent UA-Pixels, UA-color, UA-OS, UA-CPU http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  • 22. Request Heade r (cont.) Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. Call the getHeaderNames() to get an Enumeration of all header names received on the particular request. Reading request header from Servlets : http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  • 23. Accessing form input data Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument. Call getParameterValues() method to get the parameter that probably have more than one value. Call getParameterNames() to get full list of all parameters received on the particular request. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  • 24. URL Redirecting Using sendRedirect() method of HttpServletResponse class. Using forward() method of RequestDispatcher class. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  • 25. URL Redirecting (cont.) sendRedirect() It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value) RequestDispatcher The target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute(). http://www.theserverside.com/discussions/thread.tss?thread_id=26425
  • 26. URL Redirecting (cont.) sendRedirect() It will updates the browser history. RequestDispatcher If use RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3. Both kinds of redirections are useful, depending on the precise effect you want. http://www.theserverside.com/discussions/thread.tss?thread_id=26425