Chaper 1 Servlets / JSP Course Introduction to servlets
Servlet / JSP course topics Chapter 0 Introduction to Java Web Development Chapter 1 Introduction to servlets Chapter 2 Introduction to JavaServer Pages Chapter 3 How to use the MVC pattern  in a Java Web Application Chapter 4 How to share information in servlets and JSPs Chapter 5 Advanced JSP concepts Chapter 6 How to use JavaBeans with JSP Chapter 7 How to use the JSP Expression Language (EL) Chapter 8 How to use the JSP Standard Tag Library (JSTL) Chapter 9 How to use custom JSP tags Chapter 10 How to access databases in java web applications Chapter 11 How to use JavaMail to send email Chapter 12 How to secure java web applications  Chapter 13 How to download files with Servlets  Chapter 14 How to work with listeners Chapter 15 How to work with filters
Introduction to servlets
Introduction to servlets Servlet Definition Servlet life cycle Exercise  Exercise explanation Methods provided by ServletRequest for retrieving client-sent parameters Life-cycle Servlet Methods
What Is a Servlet? A servlet is a Java class that is used to extend the capabilities of servers that host our web applications accessed under a request-response programming model.  Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.  For such applications, Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.  All servlets must implement the Servlet interface, which defines life-cycle methods.  When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API.  The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services. This chapter focuses on writing servlets that generate responses to HTTP requests.
Servlet Life Cycle The life cycle of a servlet is controlled by the container in which the servlet has been deployed.  When a request is mapped to a servlet, the container performs the following steps: If an instance of the servlet does not exist, the web container  Loads the servlet class. Creates an instance of the servlet class. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet. Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method. Finalization is discussed in Finalizing a Servlet.
Servlet Life Cycle
Exercise 1 Develope the first java web application that uses servlets only Download the file: jspservlet-01.zip Unzip it Import from Eclipse Run it
Exercise 1 Results You should get this result
Exercise 1 Results
Exercise 1 Results The web client: join_email_list.html < form   action = &quot; addToEmailList &quot;   method = &quot;post&quot; > < table   cellspacing = &quot;5&quot;   border = &quot;0&quot; > < tr > < td   align = &quot;right&quot; > First name: </ td > < td >< input   type = &quot;text&quot;   name = &quot;firstName&quot; ></ td > </ tr > < tr > < td   align = &quot;right&quot; > Last name: </ td > < td >< input   type = &quot;text&quot;   name = &quot;lastName&quot; ></ td > </ tr > < tr > < td   align = &quot;right&quot; > Email address: </ td > < td >< input   type = &quot;text&quot;   name = &quot;emailAddress&quot; ></ td > </ tr > < tr > < td ></ td > < td >< br >< input   type = &quot;submit&quot;   value = &quot;Submit&quot; ></ td > </ tr > </ table > </ form > The servlet mapped In the web.xml
Exercise 1 Results The web component: AddToEmailListServlet.java . . .  protected   void  doPost( HttpServletRequest  request , HttpServletResponse  response )  throws  ServletException, IOException { // get parameters from the request String firstName =  request .getParameter( &quot;firstName&quot; ); String lastName =  request .getParameter( &quot;lastName&quot; ); String emailAddress =  request .getParameter( &quot;emailAddress&quot; ); // get a relative file name ServletContext sc = getServletContext(); String path = sc.getRealPath( &quot;/WEB-INF/EmailList.txt&quot; ); // use regular Java objects to write the data to a file User user =  new  User(firstName, lastName, emailAddress); UserIO. add (user, path); . . . The request & Response objects
Exercise 1 Results The web component: AddToEmailListServlet.java . . .  // send response to browser response.setContentType( &quot;text/html;charset=UTF-8&quot; ); PrintWriter out = response.getWriter();  out.println( . . . +  &quot;  <table cellspacing=\&quot;5\&quot; cellpadding=\&quot;5\&quot; border=\&quot;1\&quot;>\n&quot; +  &quot;  <tr><td align=\&quot;right\&quot;>First name:</td>\n&quot; +  &quot;  <td>&quot;  + firstName +  &quot;</td>\n&quot; +  &quot;  </tr>\n&quot; +  &quot;  <tr><td align=\&quot;right\&quot;>Last name:</td>\n&quot; +  &quot;  <td>&quot;  + lastName +  &quot;</td>\n&quot; +  &quot;  </tr>\n&quot; +  &quot;  <tr><td align=\&quot;right\&quot;>Email address:</td>\n&quot; +  &quot;  <td>&quot;  + emailAddress +  &quot;</td>\n&quot; +  &quot;  </tr>\n&quot; +  &quot;  </table>\n&quot; . . . out.close(); . . . The Response to The web client
Exercise 1 Results Result to the response
Methods provided by ServletRequest for retrieving client-sent parameters
Life-cycle Servlet Methods Methods to initialize / service / destroy methods  The init(ServletConfig) can be overloaded by the init() method Usually the method service(ServletRequest, ServletResponse) is used internally by the server container
Example of Life-cycle Servlet Methods The web component: AddToEmailListServlet2.java . . .  public   class  AddToEmailListServlet2  extends  HttpServlet {  // declare an instance variable for the page int   globalCount ;  // instance variables are not thread-safe public   void   init ()  throws  ServletException { globalCount  = 0;  // initialize the instance variable } protected   void  doPost( HttpServletRequest request,  HttpServletResponse response)  throws  ServletException, IOException { // update global count variable globalCount ++;  // this is not thread-safe . . .
Resources To download example code for this chapter go to: http://www.jeetrainers.com

More Related Content

PPT
An Introduction To Java Web Technology
PPTX
Javax.servlet,http packages
PPT
Web Tech Java Servlet Update1
PPT
Java Servlets
PPT
Java servlets
PPT
Knowledge Sharing : Java Servlet
PPTX
Servletarchitecture,lifecycle,get,post
PPT
JAVA Servlets
An Introduction To Java Web Technology
Javax.servlet,http packages
Web Tech Java Servlet Update1
Java Servlets
Java servlets
Knowledge Sharing : Java Servlet
Servletarchitecture,lifecycle,get,post
JAVA Servlets

What's hot (20)

DOC
Java Servlets & JSP
PPTX
Servlets
PPT
1 java servlets and jsp
PPT
Java Servlets
PPTX
Java Servlets
PPT
Java Servlet
PPT
Servlet ppt by vikas jagtap
PPTX
Chapter 3 servlet & jsp
PDF
Java servlet technology
PDF
Servlet and servlet life cycle
DOCX
Servlet
PDF
Java servlets
PPT
Java servlet life cycle - methods ppt
PPTX
PPTX
Core web application development
PPTX
java Servlet technology
PDF
JEE Programming - 04 Java Servlets
PPTX
Servlets
PDF
Servlets
PPT
Java Servlets & JSP
Servlets
1 java servlets and jsp
Java Servlets
Java Servlets
Java Servlet
Servlet ppt by vikas jagtap
Chapter 3 servlet & jsp
Java servlet technology
Servlet and servlet life cycle
Servlet
Java servlets
Java servlet life cycle - methods ppt
Core web application development
java Servlet technology
JEE Programming - 04 Java Servlets
Servlets
Servlets
Ad

Viewers also liked (19)

PPTX
Servlet and JSP Lifecycle
PDF
DISCUS: Distributed Innovation and Scalable Collaboration in Uncertain Settings
PPTX
서블릿(servlet)
PDF
Prototype & jQuery
PPTX
forensic document examiner using graphology science
PPT
Handwriting and Document Examination
PDF
jQuery in 15 minutes
PDF
jQuery Proven Performance Tips & Tricks
PPTX
Jsp (java server page)
PDF
jQuery for beginners
PDF
Learning jQuery in 30 minutes
PDF
Java Web Programming [4/9] : JSP Basic
PPS
Jsp chapter 1
PPSX
Java server pages
PPT
Java Server Pages
PPTX
Jsp presentation
PPT
Jsp ppt
PDF
jQuery Essentials
Servlet and JSP Lifecycle
DISCUS: Distributed Innovation and Scalable Collaboration in Uncertain Settings
서블릿(servlet)
Prototype & jQuery
forensic document examiner using graphology science
Handwriting and Document Examination
jQuery in 15 minutes
jQuery Proven Performance Tips & Tricks
Jsp (java server page)
jQuery for beginners
Learning jQuery in 30 minutes
Java Web Programming [4/9] : JSP Basic
Jsp chapter 1
Java server pages
Java Server Pages
Jsp presentation
Jsp ppt
jQuery Essentials
Ad

Similar to Servlet/JSP course chapter 1: Introduction to servlets (20)

PPT
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
PPTX
C:\fakepath\jsp01
PPT
PPT
29 Jsp
PPT
Servlet11
PPT
Introduction to the Servlet / JSP course
PPT
J2EE - JSP-Servlet- Container - Components
PPT
Server-side Technologies in Java
PPT
Strutsjspservlet
PPT
Struts,Jsp,Servlet
PPT
Strutsjspservlet
PPT
PPT
Jsp Slides
ODP
SCWCD 2. servlet req - resp (cap3 - cap4)
PPTX
JAVA SERVER PAGES
PDF
Bt0083 server side programing 2
PPTX
21CS642 Module 4_2 JSP PPT.pptx VI SEM CSE
PPTX
SERVLETS (2).pptxintroduction to servlet with all servlets
PPT
JSP diana y yo
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
C:\fakepath\jsp01
29 Jsp
Servlet11
Introduction to the Servlet / JSP course
J2EE - JSP-Servlet- Container - Components
Server-side Technologies in Java
Strutsjspservlet
Struts,Jsp,Servlet
Strutsjspservlet
Jsp Slides
SCWCD 2. servlet req - resp (cap3 - cap4)
JAVA SERVER PAGES
Bt0083 server side programing 2
21CS642 Module 4_2 JSP PPT.pptx VI SEM CSE
SERVLETS (2).pptxintroduction to servlet with all servlets
JSP diana y yo

More from JavaEE Trainers (8)

PDF
Introduction tomcat7 servlet3
PDF
Introduction to java servlet 3.0 api javaone 2009
PDF
Introduction to java servlet 3.0 api javaone 2008
PDF
Jsp quick reference card
PDF
jsp, javaserver pages, Card20
PPT
Struts2 course chapter 2: installation and configuration
PPT
Struts2 course chapter 1: Evolution of Web Applications
PPT
Struts2 Course: Introduction
Introduction tomcat7 servlet3
Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2008
Jsp quick reference card
jsp, javaserver pages, Card20
Struts2 course chapter 2: installation and configuration
Struts2 course chapter 1: Evolution of Web Applications
Struts2 Course: Introduction

Recently uploaded (20)

PDF
SaaS reusability assessment using machine learning techniques
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
SaaS reusability assessment using machine learning techniques
A symptom-driven medical diagnosis support model based on machine learning te...
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
SGT Report The Beast Plan and Cyberphysical Systems of Control
LMS bot: enhanced learning management systems for improved student learning e...
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Advancing precision in air quality forecasting through machine learning integ...
Lung cancer patients survival prediction using outlier detection and optimize...
Electrocardiogram sequences data analytics and classification using unsupervi...
Comparative analysis of machine learning models for fake news detection in so...
4 layer Arch & Reference Arch of IoT.pdf
Co-training pseudo-labeling for text classification with support vector machi...
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
Training Program for knowledge in solar cell and solar industry
Enhancing plagiarism detection using data pre-processing and machine learning...
Connector Corner: Transform Unstructured Documents with Agentic Automation

Servlet/JSP course chapter 1: Introduction to servlets

  • 1. Chaper 1 Servlets / JSP Course Introduction to servlets
  • 2. Servlet / JSP course topics Chapter 0 Introduction to Java Web Development Chapter 1 Introduction to servlets Chapter 2 Introduction to JavaServer Pages Chapter 3 How to use the MVC pattern in a Java Web Application Chapter 4 How to share information in servlets and JSPs Chapter 5 Advanced JSP concepts Chapter 6 How to use JavaBeans with JSP Chapter 7 How to use the JSP Expression Language (EL) Chapter 8 How to use the JSP Standard Tag Library (JSTL) Chapter 9 How to use custom JSP tags Chapter 10 How to access databases in java web applications Chapter 11 How to use JavaMail to send email Chapter 12 How to secure java web applications Chapter 13 How to download files with Servlets Chapter 14 How to work with listeners Chapter 15 How to work with filters
  • 4. Introduction to servlets Servlet Definition Servlet life cycle Exercise Exercise explanation Methods provided by ServletRequest for retrieving client-sent parameters Life-cycle Servlet Methods
  • 5. What Is a Servlet? A servlet is a Java class that is used to extend the capabilities of servers that host our web applications accessed under a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services. This chapter focuses on writing servlets that generate responses to HTTP requests.
  • 6. Servlet Life Cycle The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps: If an instance of the servlet does not exist, the web container Loads the servlet class. Creates an instance of the servlet class. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet. Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method. Finalization is discussed in Finalizing a Servlet.
  • 8. Exercise 1 Develope the first java web application that uses servlets only Download the file: jspservlet-01.zip Unzip it Import from Eclipse Run it
  • 9. Exercise 1 Results You should get this result
  • 11. Exercise 1 Results The web client: join_email_list.html < form action = &quot; addToEmailList &quot; method = &quot;post&quot; > < table cellspacing = &quot;5&quot; border = &quot;0&quot; > < tr > < td align = &quot;right&quot; > First name: </ td > < td >< input type = &quot;text&quot; name = &quot;firstName&quot; ></ td > </ tr > < tr > < td align = &quot;right&quot; > Last name: </ td > < td >< input type = &quot;text&quot; name = &quot;lastName&quot; ></ td > </ tr > < tr > < td align = &quot;right&quot; > Email address: </ td > < td >< input type = &quot;text&quot; name = &quot;emailAddress&quot; ></ td > </ tr > < tr > < td ></ td > < td >< br >< input type = &quot;submit&quot; value = &quot;Submit&quot; ></ td > </ tr > </ table > </ form > The servlet mapped In the web.xml
  • 12. Exercise 1 Results The web component: AddToEmailListServlet.java . . . protected void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException { // get parameters from the request String firstName = request .getParameter( &quot;firstName&quot; ); String lastName = request .getParameter( &quot;lastName&quot; ); String emailAddress = request .getParameter( &quot;emailAddress&quot; ); // get a relative file name ServletContext sc = getServletContext(); String path = sc.getRealPath( &quot;/WEB-INF/EmailList.txt&quot; ); // use regular Java objects to write the data to a file User user = new User(firstName, lastName, emailAddress); UserIO. add (user, path); . . . The request & Response objects
  • 13. Exercise 1 Results The web component: AddToEmailListServlet.java . . . // send response to browser response.setContentType( &quot;text/html;charset=UTF-8&quot; ); PrintWriter out = response.getWriter(); out.println( . . . + &quot; <table cellspacing=\&quot;5\&quot; cellpadding=\&quot;5\&quot; border=\&quot;1\&quot;>\n&quot; + &quot; <tr><td align=\&quot;right\&quot;>First name:</td>\n&quot; + &quot; <td>&quot; + firstName + &quot;</td>\n&quot; + &quot; </tr>\n&quot; + &quot; <tr><td align=\&quot;right\&quot;>Last name:</td>\n&quot; + &quot; <td>&quot; + lastName + &quot;</td>\n&quot; + &quot; </tr>\n&quot; + &quot; <tr><td align=\&quot;right\&quot;>Email address:</td>\n&quot; + &quot; <td>&quot; + emailAddress + &quot;</td>\n&quot; + &quot; </tr>\n&quot; + &quot; </table>\n&quot; . . . out.close(); . . . The Response to The web client
  • 14. Exercise 1 Results Result to the response
  • 15. Methods provided by ServletRequest for retrieving client-sent parameters
  • 16. Life-cycle Servlet Methods Methods to initialize / service / destroy methods The init(ServletConfig) can be overloaded by the init() method Usually the method service(ServletRequest, ServletResponse) is used internally by the server container
  • 17. Example of Life-cycle Servlet Methods The web component: AddToEmailListServlet2.java . . . public class AddToEmailListServlet2 extends HttpServlet { // declare an instance variable for the page int globalCount ; // instance variables are not thread-safe public void init () throws ServletException { globalCount = 0; // initialize the instance variable } protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // update global count variable globalCount ++; // this is not thread-safe . . .
  • 18. Resources To download example code for this chapter go to: http://www.jeetrainers.com

Editor's Notes

  • #19: &lt;iframe src=&amp;quot;http://rcm.amazon.com/e/cm?t=marcelblog-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=1932394389&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr&amp;quot; style=&amp;quot;width:120px;height:240px;&amp;quot; scrolling=&amp;quot;no&amp;quot; marginwidth=&amp;quot;0&amp;quot; marginheight=&amp;quot;0&amp;quot; frameborder=&amp;quot;0&amp;quot;&gt;&lt;/iframe&gt;