SlideShare a Scribd company logo
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets Java Servlets - Compiled By Nitin Pai
Servlet basics Servlets are the Java technology for serving HTTP requests. They are particularly used for serving HTTP requests, but they can be used as a basis for serving other requests, too. Basic servlet functionalities implemented by the basic HTTP servlet classes have the features for accessing HTTP request data and managing cookies and sessions. Typically some other technology is used for user interface or representation part. Basic servlets are not that handy for representation matters. Java Servlets - Compiled By Nitin Pai
Servlet info Servlets are intialized (at this moment you may want to initialize some data common etc.) Servlets serve the Service requests Finally, servlets are either destroyed by the container or are terminated due to exceptions. Java Servlets - Compiled By Nitin Pai
Important To Know javax.servlet.Servlet Interface defining methods that all servlets implement javax.servlet.GenericServlet An abstract class for all Servlets javax.servlet.HttpServlet An abstract class for servlets serving HTTP requests. Most importantly, a Http servlet needs to implement the doPost() and doGet() methods, which are called when a post or get request is received.  This is the basis for getting started with the implementation http servlets. Java Servlets - Compiled By Nitin Pai
Apache Tomcat Servlet specification is a standard. To execute servlets, one needs software to implement a standard. Jakarta tomcat of the Apache project is one implementation. For us, it is much easier and much more practical to study servlets using some environment to execute them.  For running Tomcat you need to have: JDK installed and the JAVA_HOME environment variable set CATALINA_HOME set to the tomcat installation directory Java Servlets - Compiled By Nitin Pai
Servers and IDE’s Tomcat is a servlet container used for hosting JSP/Servlets Basic installation is easy Tomcat port (the port that tomcat listens) can be configured, by default it is 8080 Once tomcat is up and running, you may build and install applications. Eclipse and NetBeans are the most commonly used IDE’s used today. Eclipse is available as a zip archive and has a pluggable architecture Java Servlets - Compiled By Nitin Pai
Development & Deployment Structures Deployment is configured in build.xml and built through Ant Application root: build.xml Configures the application properties and how to build/install/remove it. src subdirectory: application sources web subdirectory: web contents (web documentroot) html files, jsp files (more on these later), WEB-INF subdirectory contains web configuration (see examples) build subdirectory: This is where deployment will build the application (the contents are not supplied by the developer, the installation will normally create these files). Again, see examples.  Java Servlets - Compiled By Nitin Pai
Tomcat application installation (deployment) There are many ways for web application deployment Using the Tomcat manager For this, use a war file (a jar file with a name ending in .war). Using the tomcat web manager, upload the war file. There is password authentication here and you need a Tomcat username/password with rights to install applications. Upload the file with managers html interface. Tomcat will unpack the war file and install the application as it is configured in the configuration files. ant installation using the xml file that specifies the installation build.xml specifies the ant operations Ant is in a sense similar to the Make facility on Linux. Java Servlets - Compiled By Nitin Pai
Java Servlet Web Application Servlet development (learning) life cycle Development Defining servlet classes, methods and properties Deployment Servlet mapping to web environment (Deployment on Tomcat) Execution Understand its execution life cycle Java Servlets - Compiled By Nitin Pai
Basic Servlet Structure public class HelloWorld  extends  javax.servlet.http. HttpServlet { public void  doGet ( javax.servlet.http. HttpServletRequest  request,  javax.servlet.http. HttpServletResponse  response )  throws javax.servlet. ServletException , java.io.IOException { … } public void  doPost ( javax.servlet.http. HttpServletRequest  request,  javax.servlet.http. HttpServletResponse  response )  throws javax.servlet. ServletException , java.io.IOException { … } } Java Servlets - Compiled By Nitin Pai
Constructor and “Main” Method Servlet instances are created (invoked) by servlet container automatically when requested – not by user classes or methods No need to define constructor The entry point is NOT the “main” method, but the two methods Use “doGet” or “doPost” to perform tasks Java Servlets - Compiled By Nitin Pai
Servlet Deployment Sample Web content root folder (public_html) The starting point of the whole web application All files and sub-directories goes here: html, images, documents … /public_html/WEB-INF/ This folder contains configuration files  and compiled class Not directly accessible through the web /public_html/WEB-INF/classes/ All compiled classes (servlet classes and other classes) are in this folder Java Servlets - Compiled By Nitin Pai
Servlet Mapping Servlet class needs to be mapped to an accessible URI (mainly through HTTP) For convenience, a servlet can be accessed in a general pattern ( invoker servlet ) http://[domain]/[context]/servlet/[ServletClassName] http://localhost:8080/servletintro/servlet/SimpleServlet Specific mapping: using the configuration file “web.xml” A servlet is specifically mapped to a user defined URL Java Servlets - Compiled By Nitin Pai
“ web.xml” Configuration Using the file  “web.xml” for more  specific mapping The file is in the “WEB-INF” folder Example Servlet class HelloWorld.class Application context: http://localhost:8988/servletintro/ Invoker class mapping http://localhost:8988/servletintro/servlet/HelloWorld Specific mapping http://localhost:8988/servletintro/hello For more mapping examples, see example “web.xml” Java Servlets - Compiled By Nitin Pai <servlet> <servlet-name>HelloW</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloW</servlet-name> <url-pattern>hello</url-pattern> </servlet-mapping>
Tomorrow: Request & Response Java Servlets - Compiled By Nitin Pai Assignment Install Tomcat and run it at localhost:8080 Create a new Web Project “Training Project” in Eclipse Make a new Servlet  com.training.web.LoginFormServlet Map the servlet to  http://localhost:8080/training/Login.do Create a war file and upload it to Tomcat using Eclipse and Tomcat Manager

More Related Content

What's hot (20)

PDF
Spring MVC Framework
Hùng Nguyễn Huy
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PPT
Introduction to .NET Framework
Raghuveer Guthikonda
 
PPTX
A brief introduction to SQLite PPT
JavaTpoint
 
PDF
Spring Boot
Jaydeep Kale
 
PDF
Spring Boot
HongSeong Jeon
 
PPT
Java Persistence API (JPA) Step By Step
Guo Albert
 
PPTX
java Jdbc
Ankit Desai
 
PPT
JDBC – Java Database Connectivity
Information Technology
 
PPTX
Full stack web development
Crampete
 
PPTX
Laravel Tutorial PPT
Piyush Aggarwal
 
PPTX
Spring boot
sdeeg
 
PDF
Nodejs presentation
Arvind Devaraj
 
PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PPTX
SQLite - Overview
Emanuele Bartolesi
 
PPTX
Chapter 3 servlet & jsp
Jafar Nesargi
 
PPTX
Node js introduction
Joseph de Castelnau
 
PDF
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Edureka!
 
PPT
Servlets
Sasidhar Kothuru
 
PPTX
SQLite database in android
Gourav Kumar Saini
 
Spring MVC Framework
Hùng Nguyễn Huy
 
Spring boot Introduction
Jeevesh Pandey
 
Introduction to .NET Framework
Raghuveer Guthikonda
 
A brief introduction to SQLite PPT
JavaTpoint
 
Spring Boot
Jaydeep Kale
 
Spring Boot
HongSeong Jeon
 
Java Persistence API (JPA) Step By Step
Guo Albert
 
java Jdbc
Ankit Desai
 
JDBC – Java Database Connectivity
Information Technology
 
Full stack web development
Crampete
 
Laravel Tutorial PPT
Piyush Aggarwal
 
Spring boot
sdeeg
 
Nodejs presentation
Arvind Devaraj
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
SQLite - Overview
Emanuele Bartolesi
 
Chapter 3 servlet & jsp
Jafar Nesargi
 
Node js introduction
Joseph de Castelnau
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Edureka!
 
SQLite database in android
Gourav Kumar Saini
 

Viewers also liked (9)

PPT
Java Servlets
BG Java EE Course
 
PPT
Java basic
Sonam Sharma
 
PDF
Introduction to Java Programming
Ravi Kant Sahu
 
PPTX
Java programming course for beginners
Eduonix Learning Solutions
 
PPT
Core java concepts
Ram132
 
PPT
Java tutorial PPT
Intelligo Technologies
 
PPTX
Presentation on Core java
mahir jain
 
PPT
Core java slides
Abhilash Nair
 
PPTX
Introduction to java
Veerabadra Badra
 
Java Servlets
BG Java EE Course
 
Java basic
Sonam Sharma
 
Introduction to Java Programming
Ravi Kant Sahu
 
Java programming course for beginners
Eduonix Learning Solutions
 
Core java concepts
Ram132
 
Java tutorial PPT
Intelligo Technologies
 
Presentation on Core java
mahir jain
 
Core java slides
Abhilash Nair
 
Introduction to java
Veerabadra Badra
 
Ad

Similar to Java Servlets (20)

DOC
Java Servlets & JSP
Manjunatha RK
 
PPTX
Web container and Apache Tomcat
Auwal Amshi
 
PDF
Java servlet technology
Minal Maniar
 
PPT
Web Applications and Deployment
BG Java EE Course
 
PPT
1 java servlets and jsp
Ankit Minocha
 
PPT
Web Application Deployment
elliando dias
 
PPTX
UNIT-3 Servlet
ssbd6985
 
PPTX
AJppt.pptx
SachinSingh217687
 
PPT
JEE Course - The Web Tier
odedns
 
PDF
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Arun Gupta
 
PPT
Tomcat
Venkat Pinagadi
 
PPT
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
sindhu991994
 
PPTX
Advance Java Topics (J2EE)
slire
 
PPT
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
PPTX
Java Servlet
Yoga Raja
 
PPT
Lecture 19 dynamic web - java - part 1
Д. Ганаа
 
PPTX
Cis 274 intro
Aren Zomorodian
 
PPTX
Jsp and Servlets
Raghu nath
 
DOC
Unit5 servlets
Praveen Yadav
 
Java Servlets & JSP
Manjunatha RK
 
Web container and Apache Tomcat
Auwal Amshi
 
Java servlet technology
Minal Maniar
 
Web Applications and Deployment
BG Java EE Course
 
1 java servlets and jsp
Ankit Minocha
 
Web Application Deployment
elliando dias
 
UNIT-3 Servlet
ssbd6985
 
AJppt.pptx
SachinSingh217687
 
JEE Course - The Web Tier
odedns
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Arun Gupta
 
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
sindhu991994
 
Advance Java Topics (J2EE)
slire
 
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
Java Servlet
Yoga Raja
 
Lecture 19 dynamic web - java - part 1
Д. Ганаа
 
Cis 274 intro
Aren Zomorodian
 
Jsp and Servlets
Raghu nath
 
Unit5 servlets
Praveen Yadav
 
Ad

Recently uploaded (20)

PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Digital Circuits, important subject in CS
contactparinay1
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 

Java Servlets

  • 1. AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets Java Servlets - Compiled By Nitin Pai
  • 2. Servlet basics Servlets are the Java technology for serving HTTP requests. They are particularly used for serving HTTP requests, but they can be used as a basis for serving other requests, too. Basic servlet functionalities implemented by the basic HTTP servlet classes have the features for accessing HTTP request data and managing cookies and sessions. Typically some other technology is used for user interface or representation part. Basic servlets are not that handy for representation matters. Java Servlets - Compiled By Nitin Pai
  • 3. Servlet info Servlets are intialized (at this moment you may want to initialize some data common etc.) Servlets serve the Service requests Finally, servlets are either destroyed by the container or are terminated due to exceptions. Java Servlets - Compiled By Nitin Pai
  • 4. Important To Know javax.servlet.Servlet Interface defining methods that all servlets implement javax.servlet.GenericServlet An abstract class for all Servlets javax.servlet.HttpServlet An abstract class for servlets serving HTTP requests. Most importantly, a Http servlet needs to implement the doPost() and doGet() methods, which are called when a post or get request is received. This is the basis for getting started with the implementation http servlets. Java Servlets - Compiled By Nitin Pai
  • 5. Apache Tomcat Servlet specification is a standard. To execute servlets, one needs software to implement a standard. Jakarta tomcat of the Apache project is one implementation. For us, it is much easier and much more practical to study servlets using some environment to execute them. For running Tomcat you need to have: JDK installed and the JAVA_HOME environment variable set CATALINA_HOME set to the tomcat installation directory Java Servlets - Compiled By Nitin Pai
  • 6. Servers and IDE’s Tomcat is a servlet container used for hosting JSP/Servlets Basic installation is easy Tomcat port (the port that tomcat listens) can be configured, by default it is 8080 Once tomcat is up and running, you may build and install applications. Eclipse and NetBeans are the most commonly used IDE’s used today. Eclipse is available as a zip archive and has a pluggable architecture Java Servlets - Compiled By Nitin Pai
  • 7. Development & Deployment Structures Deployment is configured in build.xml and built through Ant Application root: build.xml Configures the application properties and how to build/install/remove it. src subdirectory: application sources web subdirectory: web contents (web documentroot) html files, jsp files (more on these later), WEB-INF subdirectory contains web configuration (see examples) build subdirectory: This is where deployment will build the application (the contents are not supplied by the developer, the installation will normally create these files). Again, see examples. Java Servlets - Compiled By Nitin Pai
  • 8. Tomcat application installation (deployment) There are many ways for web application deployment Using the Tomcat manager For this, use a war file (a jar file with a name ending in .war). Using the tomcat web manager, upload the war file. There is password authentication here and you need a Tomcat username/password with rights to install applications. Upload the file with managers html interface. Tomcat will unpack the war file and install the application as it is configured in the configuration files. ant installation using the xml file that specifies the installation build.xml specifies the ant operations Ant is in a sense similar to the Make facility on Linux. Java Servlets - Compiled By Nitin Pai
  • 9. Java Servlet Web Application Servlet development (learning) life cycle Development Defining servlet classes, methods and properties Deployment Servlet mapping to web environment (Deployment on Tomcat) Execution Understand its execution life cycle Java Servlets - Compiled By Nitin Pai
  • 10. Basic Servlet Structure public class HelloWorld extends javax.servlet.http. HttpServlet { public void doGet ( javax.servlet.http. HttpServletRequest request, javax.servlet.http. HttpServletResponse response ) throws javax.servlet. ServletException , java.io.IOException { … } public void doPost ( javax.servlet.http. HttpServletRequest request, javax.servlet.http. HttpServletResponse response ) throws javax.servlet. ServletException , java.io.IOException { … } } Java Servlets - Compiled By Nitin Pai
  • 11. Constructor and “Main” Method Servlet instances are created (invoked) by servlet container automatically when requested – not by user classes or methods No need to define constructor The entry point is NOT the “main” method, but the two methods Use “doGet” or “doPost” to perform tasks Java Servlets - Compiled By Nitin Pai
  • 12. Servlet Deployment Sample Web content root folder (public_html) The starting point of the whole web application All files and sub-directories goes here: html, images, documents … /public_html/WEB-INF/ This folder contains configuration files and compiled class Not directly accessible through the web /public_html/WEB-INF/classes/ All compiled classes (servlet classes and other classes) are in this folder Java Servlets - Compiled By Nitin Pai
  • 13. Servlet Mapping Servlet class needs to be mapped to an accessible URI (mainly through HTTP) For convenience, a servlet can be accessed in a general pattern ( invoker servlet ) http://[domain]/[context]/servlet/[ServletClassName] http://localhost:8080/servletintro/servlet/SimpleServlet Specific mapping: using the configuration file “web.xml” A servlet is specifically mapped to a user defined URL Java Servlets - Compiled By Nitin Pai
  • 14. “ web.xml” Configuration Using the file “web.xml” for more specific mapping The file is in the “WEB-INF” folder Example Servlet class HelloWorld.class Application context: http://localhost:8988/servletintro/ Invoker class mapping http://localhost:8988/servletintro/servlet/HelloWorld Specific mapping http://localhost:8988/servletintro/hello For more mapping examples, see example “web.xml” Java Servlets - Compiled By Nitin Pai <servlet> <servlet-name>HelloW</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloW</servlet-name> <url-pattern>hello</url-pattern> </servlet-mapping>
  • 15. Tomorrow: Request & Response Java Servlets - Compiled By Nitin Pai Assignment Install Tomcat and run it at localhost:8080 Create a new Web Project “Training Project” in Eclipse Make a new Servlet com.training.web.LoginFormServlet Map the servlet to http://localhost:8080/training/Login.do Create a war file and upload it to Tomcat using Eclipse and Tomcat Manager