SlideShare a Scribd company logo
Annotations in Servlet
Agenda
• Ease of Development
• Annotation Support
Annotation in Servlets 3.0 specification
• The new specification focuses on
- Ease of development
- Plugability and extensibility
- Asynchronous Support
- Security Enhancements
Ease of Development
• Annotation based declarative-style programming
• Use @WebServlet for developing Servlet
• Use @ServletFilter for developing Filter
• Use @WebServletContextListener for creating ServletContext
Listener
Annotation v/s Deployment Descriptor
• Interesting note:
- Deployment descriptor can be used along with annotation
- Deployment descriptor takes precedence over annotation
- Metadata-complete is a new attribute which contains either true or false
• True means deployment tool will ignore any servlet
annotation
• False means container must scan all the files for
annotation along with the deployment descriptor
Annotation v/s Deployment Descriptor
• @WebServlet(name="mytest",
urlPatterns={"/myurl"},
initParams={ @InitParam(name="n1", value="v1"),
@InitParam(name="n2", value="v2") })
public class TestServlet extends javax.servlet.http.HttpServlet {
....
}
Annotation v/s Deployment Descriptor
• web.xml
<init-param>
<param-name>n1</param-name>
<param-value>v1</param-value>
</init-param>
<init-param>
<param-name>n2</param-name>
<param-value>v2</param-value>
</init-param>
Servlet Context Listener Annotation
• @WebServletContextListener
public class TestServletContextListener implements
javax.servlet.ServletContextListener {
....
public void contextInitialized(ServletContextEvent sce) {
....
}
public void contextDestroyed(ServletContextEvent sce) {
....
}
}
Servlet Filter Annotation
• @ServletFilter(urlPatterns={"/myurl"}.
initParams={ @InitParam(name="mesg", value="my filter") })
public class TestFilter implements javax.servlet.Filter {
....
public void init(FilterConfig filterConfig) throws ServletException {
....
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws
IOException, ServletException {
....
}
public void destroy() {
....
}
}
Summary
• Annotation support is available only from JDK 1.5 or above
• Annotation can be used along with tradition approach.
• Annotation makes development easier

More Related Content

PDF
CIS13: Identity is the New Currency
PDF
CodeIgniter For Project : Lesson 104 - Controller & View
PPTX
Advance java session 18
PPTX
SCWCD : The servlet container : CHAP : 4
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 5...
PDF
Ch5 beeing an application
PPTX
Integrating Servlets and JSP (The MVC Architecture)
PPT
Listeners and filters in servlet
CIS13: Identity is the New Currency
CodeIgniter For Project : Lesson 104 - Controller & View
Advance java session 18
SCWCD : The servlet container : CHAP : 4
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 5...
Ch5 beeing an application
Integrating Servlets and JSP (The MVC Architecture)
Listeners and filters in servlet

Similar to Advance java session 6 (20)

PPT
JEE5 New Features
PPT
Java EE 02-First Servlet
PPTX
SERVLETS (2).pptxintroduction to servlet with all servlets
PPTX
Javax.servlet,http packages
PPT
Chap4 4 1
PDF
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
ODP
servlet 2.5 & JSP 2.0
ODP
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
PDF
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
PDF
Introduction to java servlet 3.0 api javaone 2009
PDF
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
PDF
Spring 3.1 in a Nutshell - JAX London 2011
PPTX
Introduction to JSP.pptx
PPT
Slide 3- Java Web Application-PRJ301.ppt
PPT
Session 2 servlet context and session tracking - Giáo trình Bách Khoa Aptech
PPTX
BITM3730Week12.pptx
PPTX
Java servlets
PDF
HTTP, JSP, and AJAX.pdf
PDF
Java Servlets.pdf
PPTX
Java EE - Servlets API
JEE5 New Features
Java EE 02-First Servlet
SERVLETS (2).pptxintroduction to servlet with all servlets
Javax.servlet,http packages
Chap4 4 1
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
servlet 2.5 & JSP 2.0
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Introduction to java servlet 3.0 api javaone 2009
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring 3.1 in a Nutshell - JAX London 2011
Introduction to JSP.pptx
Slide 3- Java Web Application-PRJ301.ppt
Session 2 servlet context and session tracking - Giáo trình Bách Khoa Aptech
BITM3730Week12.pptx
Java servlets
HTTP, JSP, and AJAX.pdf
Java Servlets.pdf
Java EE - Servlets API
Ad

More from Smita B Kumar (19)

PPTX
Advance java session 20
PPTX
Advance java session 19
PPTX
Advance java session 17
PPTX
Advance java session 16
PPTX
Advance java session 15
PPTX
Advance java session 14
PPTX
Advance java session 13
PPTX
Advance java session 12
PPTX
Advance java session 11
PPTX
Advance java session 10
PPTX
Advance java session 9
PPTX
Advance java session 8
PPTX
Advance java session 7
PPTX
Advance java session 5
PPTX
Advance java session 4
PPTX
Advance java session 3
PPTX
Advance java session 2
PPTX
JEE session 1
PPTX
01 introduction to struts2
Advance java session 20
Advance java session 19
Advance java session 17
Advance java session 16
Advance java session 15
Advance java session 14
Advance java session 13
Advance java session 12
Advance java session 11
Advance java session 10
Advance java session 9
Advance java session 8
Advance java session 7
Advance java session 5
Advance java session 4
Advance java session 3
Advance java session 2
JEE session 1
01 introduction to struts2
Ad

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
KodekX | Application Modernization Development
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
KodekX | Application Modernization Development

Advance java session 6

  • 2. Agenda • Ease of Development • Annotation Support
  • 3. Annotation in Servlets 3.0 specification • The new specification focuses on - Ease of development - Plugability and extensibility - Asynchronous Support - Security Enhancements
  • 4. Ease of Development • Annotation based declarative-style programming • Use @WebServlet for developing Servlet • Use @ServletFilter for developing Filter • Use @WebServletContextListener for creating ServletContext Listener
  • 5. Annotation v/s Deployment Descriptor • Interesting note: - Deployment descriptor can be used along with annotation - Deployment descriptor takes precedence over annotation - Metadata-complete is a new attribute which contains either true or false • True means deployment tool will ignore any servlet annotation • False means container must scan all the files for annotation along with the deployment descriptor
  • 6. Annotation v/s Deployment Descriptor • @WebServlet(name="mytest", urlPatterns={"/myurl"}, initParams={ @InitParam(name="n1", value="v1"), @InitParam(name="n2", value="v2") }) public class TestServlet extends javax.servlet.http.HttpServlet { .... }
  • 7. Annotation v/s Deployment Descriptor • web.xml <init-param> <param-name>n1</param-name> <param-value>v1</param-value> </init-param> <init-param> <param-name>n2</param-name> <param-value>v2</param-value> </init-param>
  • 8. Servlet Context Listener Annotation • @WebServletContextListener public class TestServletContextListener implements javax.servlet.ServletContextListener { .... public void contextInitialized(ServletContextEvent sce) { .... } public void contextDestroyed(ServletContextEvent sce) { .... } }
  • 9. Servlet Filter Annotation • @ServletFilter(urlPatterns={"/myurl"}. initParams={ @InitParam(name="mesg", value="my filter") }) public class TestFilter implements javax.servlet.Filter { .... public void init(FilterConfig filterConfig) throws ServletException { .... } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { .... } public void destroy() { .... } }
  • 10. Summary • Annotation support is available only from JDK 1.5 or above • Annotation can be used along with tradition approach. • Annotation makes development easier