• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Servlet/JSP course chapter 1: Introduction to servlets
 

Servlet/JSP course chapter 1: Introduction to servlets

on

  • 4,710 views

Introduction to servlets

Introduction to servlets

Statistics

Views

Total Views
4,710
Views on SlideShare
4,627
Embed Views
83

Actions

Likes
14
Downloads
0
Comments
2

6 Embeds 83

http://www.jeetrainers.com 50
http://www.slideshare.net 21
http://jeetrainers.com 6
http://static.slidesharecdn.com 3
http://a0.twimg.com 2
https://twimg0-a.akamaihd.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Good One ! Thanks
    Are you sure you want to
    Your message goes here
    Processing…
  • thank you.....
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Servlet/JSP course chapter 1: Introduction to servlets Servlet/JSP course chapter 1: Introduction to servlets Presentation Transcript

  • 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;> &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
  • 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