Your SlideShare is downloading. ×
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,614
views

Published on

Introduction to servlets

Introduction to servlets

Published in: Technology

2 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,614
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
      • 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
    • 3. Introduction to servlets
    • 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.
    • 7. Servlet Life Cycle
    • 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
    • 10. Exercise 1 Results
    • 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;> &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. 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