Your SlideShare is downloading. ×
0
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Servlet/JSP course chapter 1: Introduction to servlets

4,785

Published on

Introduction to servlets

Introduction to servlets

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • <iframe src="http://rcm.amazon.com/e/cm?t=marcelblog-20&o=1&p=8&l=as1&asins=1932394389&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
  • Transcript

    • 1. Chaper 1 Servlets / JSP Course Introduction to servlets
    • 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. Introduction to servlets
    • 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. 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. 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. Servlet Life Cycle
    • 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. Exercise 1 Results <ul><li>You should get this result </li></ul>
    • 10. Exercise 1 Results
    • 11. Exercise 1 Results <ul><li>The web client: join_email_list.html </li></ul>&lt; form action = &amp;quot; addToEmailList &amp;quot; method = &amp;quot;post&amp;quot; &gt; &lt; table cellspacing = &amp;quot;5&amp;quot; border = &amp;quot;0&amp;quot; &gt; &lt; tr &gt; &lt; td align = &amp;quot;right&amp;quot; &gt; First name: &lt;/ td &gt; &lt; td &gt;&lt; input type = &amp;quot;text&amp;quot; name = &amp;quot;firstName&amp;quot; &gt;&lt;/ td &gt; &lt;/ tr &gt; &lt; tr &gt; &lt; td align = &amp;quot;right&amp;quot; &gt; Last name: &lt;/ td &gt; &lt; td &gt;&lt; input type = &amp;quot;text&amp;quot; name = &amp;quot;lastName&amp;quot; &gt;&lt;/ td &gt; &lt;/ tr &gt; &lt; tr &gt; &lt; td align = &amp;quot;right&amp;quot; &gt; Email address: &lt;/ td &gt; &lt; td &gt;&lt; input type = &amp;quot;text&amp;quot; name = &amp;quot;emailAddress&amp;quot; &gt;&lt;/ td &gt; &lt;/ tr &gt; &lt; tr &gt; &lt; td &gt;&lt;/ td &gt; &lt; td &gt;&lt; br &gt;&lt; input type = &amp;quot;submit&amp;quot; value = &amp;quot;Submit&amp;quot; &gt;&lt;/ td &gt; &lt;/ tr &gt; &lt;/ table &gt; &lt;/ form &gt; The servlet mapped In the web.xml
    • 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( &amp;quot;firstName&amp;quot; ); String lastName = request .getParameter( &amp;quot;lastName&amp;quot; ); String emailAddress = request .getParameter( &amp;quot;emailAddress&amp;quot; ); // get a relative file name ServletContext sc = getServletContext(); String path = sc.getRealPath( &amp;quot;/WEB-INF/EmailList.txt&amp;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 &amp; Response objects
    • 13. Exercise 1 Results <ul><li>The web component: AddToEmailListServlet.java </li></ul>. . . // send response to browser response.setContentType( &amp;quot;text/html;charset=UTF-8&amp;quot; ); PrintWriter out = response.getWriter(); out.println( . . . + &amp;quot; &lt;table cellspacing=&amp;quot;5&amp;quot; cellpadding=&amp;quot;5&amp;quot; border=&amp;quot;1&amp;quot;&gt; &amp;quot; + &amp;quot; &lt;tr&gt;&lt;td align=&amp;quot;right&amp;quot;&gt;First name:&lt;/td&gt; &amp;quot; + &amp;quot; &lt;td&gt;&amp;quot; + firstName + &amp;quot;&lt;/td&gt; &amp;quot; + &amp;quot; &lt;/tr&gt; &amp;quot; + &amp;quot; &lt;tr&gt;&lt;td align=&amp;quot;right&amp;quot;&gt;Last name:&lt;/td&gt; &amp;quot; + &amp;quot; &lt;td&gt;&amp;quot; + lastName + &amp;quot;&lt;/td&gt; &amp;quot; + &amp;quot; &lt;/tr&gt; &amp;quot; + &amp;quot; &lt;tr&gt;&lt;td align=&amp;quot;right&amp;quot;&gt;Email address:&lt;/td&gt; &amp;quot; + &amp;quot; &lt;td&gt;&amp;quot; + emailAddress + &amp;quot;&lt;/td&gt; &amp;quot; + &amp;quot; &lt;/tr&gt; &amp;quot; + &amp;quot; &lt;/table&gt; &amp;quot; . . . out.close(); . . . The Response to The web client
    • 14. Exercise 1 Results <ul><li>Result to the response </li></ul>
    • 15. Methods provided by ServletRequest for retrieving client-sent parameters
    • 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. 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. Resources To download example code for this chapter go to: http://www.jeetrainers.com

    ×