Chaper 1 Servlets / JSP Course Introduction to servlets
Servlet / JSP course topics <ul><li>Chapter 0 Introduction to Java Web Development </li></ul><ul><li>Chapter 1 Introductio...
Introduction to servlets
Introduction to servlets <ul><li>Servlet Definition </li></ul><ul><li>Servlet life cycle </li></ul><ul><li>Exercise  </li>...
What Is a Servlet? <ul><li>A servlet is a Java class that is used to extend the capabilities of servers that host our web ...
Servlet Life Cycle <ul><li>The life cycle of a servlet is controlled by the container in which the servlet has been deploy...
Servlet Life Cycle
Exercise 1 <ul><li>Develope the first java web application that uses servlets only </li></ul><ul><li>Download the file: </...
Exercise 1 Results <ul><li>You should get this result </li></ul>
Exercise 1 Results
Exercise 1 Results <ul><li>The web client: join_email_list.html </li></ul>< form   action = &quot; addToEmailList &quot;  ...
Exercise 1 Results <ul><li>The web component: AddToEmailListServlet.java </li></ul>. . .  protected   void  doPost( HttpSe...
Exercise 1 Results <ul><li>The web component: AddToEmailListServlet.java </li></ul>. . .  // send response to browser resp...
Exercise 1 Results <ul><li>Result to the response </li></ul>
Methods provided by ServletRequest for retrieving client-sent parameters
Life-cycle Servlet Methods <ul><li>Methods to initialize / service / destroy methods  </li></ul><ul><li>The init(ServletCo...
Example of Life-cycle Servlet Methods <ul><li>The web component: AddToEmailListServlet2.java </li></ul>. . .  public   cla...
Resources To download example code for this chapter go to: http://www.jeetrainers.com
Upcoming SlideShare
Loading in...5
×

Servlet/JSP course chapter 1: Introduction to servlets

4,830

Published on

Introduction to servlets

Published in: Technology
2 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,830
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
2
Likes
17
Embeds 0
No embeds

No notes for slide
  • &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;
  • Transcript of "Servlet/JSP course chapter 1: Introduction to servlets"

    1. 1. Chaper 1 Servlets / JSP Course Introduction to servlets
    2. 2. Servlet / JSP course topics <ul><li>Chapter 0 Introduction to Java Web Development </li></ul><ul><li>Chapter 1 Introduction to servlets </li></ul><ul><li>Chapter 2 Introduction to JavaServer Pages </li></ul><ul><li>Chapter 3 How to use the MVC pattern in a Java Web Application </li></ul><ul><li>Chapter 4 How to share information in servlets and JSPs </li></ul><ul><li>Chapter 5 Advanced JSP concepts </li></ul><ul><li>Chapter 6 How to use JavaBeans with JSP </li></ul><ul><li>Chapter 7 How to use the JSP Expression Language (EL) </li></ul><ul><li>Chapter 8 How to use the JSP Standard Tag Library (JSTL) </li></ul><ul><li>Chapter 9 How to use custom JSP tags </li></ul><ul><li>Chapter 10 How to access databases in java web applications </li></ul><ul><li>Chapter 11 How to use JavaMail to send email </li></ul><ul><li>Chapter 12 How to secure java web applications </li></ul><ul><li>Chapter 13 How to download files with Servlets </li></ul><ul><li>Chapter 14 How to work with listeners </li></ul><ul><li>Chapter 15 How to work with filters </li></ul>
    3. 3. Introduction to servlets
    4. 4. Introduction to servlets <ul><li>Servlet Definition </li></ul><ul><li>Servlet life cycle </li></ul><ul><li>Exercise </li></ul><ul><li>Exercise explanation </li></ul><ul><li>Methods provided by ServletRequest for retrieving client-sent parameters </li></ul><ul><li>Life-cycle Servlet Methods </li></ul>
    5. 5. What Is a Servlet? <ul><li>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. </li></ul><ul><li>Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. </li></ul><ul><li>For such applications, Java Servlet technology defines HTTP-specific servlet classes. </li></ul><ul><li>The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. </li></ul><ul><li>All servlets must implement the Servlet interface, which defines life-cycle methods. </li></ul><ul><li>When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. </li></ul><ul><li>The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services. </li></ul><ul><li>This chapter focuses on writing servlets that generate responses to HTTP requests. </li></ul>
    6. 6. Servlet Life Cycle <ul><li>The life cycle of a servlet is controlled by the container in which the servlet has been deployed. </li></ul><ul><li>When a request is mapped to a servlet, the container performs the following steps: </li></ul><ul><ul><li>If an instance of the servlet does not exist, the web container </li></ul></ul><ul><ul><li>Loads the servlet class. </li></ul></ul><ul><ul><li>Creates an instance of the servlet class. </li></ul></ul><ul><ul><li>Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet. </li></ul></ul><ul><ul><li>Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods. </li></ul></ul><ul><li>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. </li></ul>
    7. 7. Servlet Life Cycle
    8. 8. Exercise 1 <ul><li>Develope the first java web application that uses servlets only </li></ul><ul><li>Download the file: </li></ul><ul><ul><li>jspservlet-01.zip </li></ul></ul><ul><li>Unzip it </li></ul><ul><li>Import from Eclipse </li></ul><ul><li>Run it </li></ul>
    9. 9. Exercise 1 Results <ul><li>You should get this result </li></ul>
    10. 10. Exercise 1 Results
    11. 11. Exercise 1 Results <ul><li>The web client: join_email_list.html </li></ul>< 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. 12. Exercise 1 Results <ul><li>The web component: AddToEmailListServlet.java </li></ul>. . . 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. 13. Exercise 1 Results <ul><li>The web component: AddToEmailListServlet.java </li></ul>. . . // 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;> &quot; + &quot; <tr><td align=&quot;right&quot;>First name:</td> &quot; + &quot; <td>&quot; + firstName + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; <tr><td align=&quot;right&quot;>Last name:</td> &quot; + &quot; <td>&quot; + lastName + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; <tr><td align=&quot;right&quot;>Email address:</td> &quot; + &quot; <td>&quot; + emailAddress + &quot;</td> &quot; + &quot; </tr> &quot; + &quot; </table> &quot; . . . out.close(); . . . The Response to The web client
    14. 14. Exercise 1 Results <ul><li>Result to the response </li></ul>
    15. 15. Methods provided by ServletRequest for retrieving client-sent parameters
    16. 16. Life-cycle Servlet Methods <ul><li>Methods to initialize / service / destroy methods </li></ul><ul><li>The init(ServletConfig) can be overloaded by the init() method </li></ul><ul><li>Usually the method service(ServletRequest, ServletResponse) is used internally by the server container </li></ul>
    17. 17. Example of Life-cycle Servlet Methods <ul><li>The web component: AddToEmailListServlet2.java </li></ul>. . . 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. 18. Resources To download example code for this chapter go to: http://www.jeetrainers.com

    ×