+ All Categories
Home > Documents > Chapter2 Servlet Programming

Chapter2 Servlet Programming

Date post: 04-Apr-2018
Category:
Upload: tick-tack
View: 222 times
Download: 0 times
Share this document with a friend

of 34

Transcript
  • 7/31/2019 Chapter2 Servlet Programming

    1/34

    @2008 Huynh Ngoc Tin

    Chapter #2

    JAVA SERVLET

    PRGRAMMING

  • 7/31/2019 Chapter2 Servlet Programming

    2/34

    @2008 Huynh Ngoc Tin

    Objective

    Basic concepts

    9 Why?

    9 What is java servlet?

    9

    Servlet container9 Servlet life circle

    9 Servlet API, servlet interface

    9

    Build, deploy, run a java servlet

  • 7/31/2019 Chapter2 Servlet Programming

    3/34

    @2008 Huynh Ngoc Tin

    References

    [1] Chapt er 3. Prent ice Hall Pt r Java(Tm) Ee 5 Tut or ial,

    The (3Rd Edition) (The Java Series)

    [2] Marty Hall. Core Servlet and Java Server Page. Sun

    Micro System. Prentice Hall PTR; 1 edition 2000.

    [3] http://java.sun.com/products/servlet

  • 7/31/2019 Chapter2 Servlet Programming

    4/34

    @2008 Huynh Ngoc Tin

    Basic concepts

    Why?

    What is java servlet?

    What is servlet container?

    Servets on J2EE/JEE Platform Servlet Interface

    Servlet Life Circle

    Servlet context

    Request/Response

  • 7/31/2019 Chapter2 Servlet Programming

    5/34

    The need for dynamic content.

    Applets, one of the earliest attempts toward this goal (the

    client side)

    At the same time, (CGI) scripts were the main technology

    used to generate dynamic content

    Limitation: Lack of scalability, Java Servlet technology

    was created as a portable way to provide dynamic, user-

    oriented content.

    @2008 Huynh Ngoc Tin

    Why?

  • 7/31/2019 Chapter2 Servlet Programming

    6/34

  • 7/31/2019 Chapter2 Servlet Programming

    7/34

    @2008 Huynh Ngoc Tin

    What is servlet container (Web container)?

    A Web container is essentially the component of a Web server

    that interacts with the servlet.

    The Web container is responsible for managing the life cycle of

    servlets

    Mapping a URL to a particular servlet and ensuring that the URL

    requester has the correct access rights.

  • 7/31/2019 Chapter2 Servlet Programming

    8/34

    @2008 Huynh Ngoc Tin

    What is servlet container (Web container)?

  • 7/31/2019 Chapter2 Servlet Programming

    9/34

    @2008 Huynh Ngoc Tin

    Java Servlet on JEE Platform

  • 7/31/2019 Chapter2 Servlet Programming

    10/34

    Thejavax.servlet andjavax.servlet.http packages provide

    interfaces and classes for writing servlets. The Java Servlet API allows a software developer to add

    dynamic content to a Web server using the Java platform. The

    generated content is commonly HTML, but may be other data

    such as XML.

    The HttpServlet class provides methods, such as doGet and

    doPost, for handling HTTP-specific services.

    @2008 Huynh Ngoc Tin

    Servlet API

  • 7/31/2019 Chapter2 Servlet Programming

    11/34

    @2008 Huynh Ngoc Tin

    Servlet API historyPackages: javax.servlet & javax.servlet.http

  • 7/31/2019 Chapter2 Servlet Programming

    12/34

    @2008 Huynh Ngoc Tin

    Servlet Interface

  • 7/31/2019 Chapter2 Servlet Programming

    13/34

    @2008 Huynh Ngoc Tin

    Servlet Interface

    Contains methods which are implemented by your servlets

    Define methods to:

    Init a java servlet.

    Handle requests from the client side.

    Remove servlet from the server.

    Your Servlets can implements Servlet Interface or extends from

    a other class that implemented Servlet Interface such as

    GenericServlet &HttpServlet.

  • 7/31/2019 Chapter2 Servlet Programming

    14/34

    @2008 Huynh Ngoc Tin

    Servlet Interface

    public abstract class GenericServlet

    extends Object implements Servlet,ServletConfig, Serializable

    public abstract class HttpServlet

    extends GenericServlet implements Serializable

    GenericServlet nh ngha 1 servlet c lp

    protocol, nh ngha 1 servlet dng trn

    Web ta c th cho k tha lp HttpServlet.

    http://java.sun.com/j2se/1.5/docs/api/java/lang/Object.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/Servlet.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/ServletConfig.htmlhttp://java.sun.com/j2se/1.5/docs/api/java/io/Serializable.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/GenericServlet.htmlhttp://java.sun.com/j2se/1.5/docs/api/java/io/Serializable.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/GenericServlet.htmlhttp://java.sun.com/j2se/1.5/docs/api/java/io/Serializable.htmlhttp://java.sun.com/j2se/1.5/docs/api/java/io/Serializable.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/ServletConfig.htmlhttp://c/Sun/SDK/docs/api/javax/servlet/Servlet.htmlhttp://java.sun.com/j2se/1.5/docs/api/java/lang/Object.html
  • 7/31/2019 Chapter2 Servlet Programming

    15/34

    @2008 Huynh Ngoc Tin

    Servlet Interface

    Handle request

    9

    servicemethod: servlet Interface define servicemethod to handle requests from the client side.

    9 Servlet Container will call service method tohandle the request from the client side.

  • 7/31/2019 Chapter2 Servlet Programming

    16/34

    @2008 Huynh Ngoc Tin

    Servlet Interface

    HttpRequest methods

    HttpServlet have some more methods to handle

    HttpRequest:9 doGet for handling HTTP GET requests

    9 doPost for handling HTTP POST requests

    9

    The above methods can are called automatically by service

    method

  • 7/31/2019 Chapter2 Servlet Programming

    17/34

    @2008 Huynh Ngoc Tin

    Servlet life circle

    Servlet container have only one instance for each servlet.

    Instance is handle by in it , service, dest royin ServletInterface (JEE API).

    Servlet Container load and init servlets.

    Servlet Container have init servlets before the request come

    from the client.

    Servlet Container have to choose a servlet for the request and

    make decision what servlet will be removed from the server.

  • 7/31/2019 Chapter2 Servlet Programming

    18/34

    @2008 Huynh Ngoc Tin

    Request & Response

  • 7/31/2019 Chapter2 Servlet Programming

    19/34

    @2008 Huynh Ngoc Tin

    Request & Response

    Request

    9 Package all information that comes from the client.

    9 Request from Client -> Server follows the HTTP protocol

    9 HttpServletRequest methods can summarize, read therequests information.

  • 7/31/2019 Chapter2 Servlet Programming

    20/34

    @2008 Huynh Ngoc Tin

    Request & Response

    Response

    9 Package all information that will response for the client.

    9 Information in response object are sent to client by HTTTprotocol

    9 HttpServletReponse methods can summarize, packageinformation into response object.

  • 7/31/2019 Chapter2 Servlet Programming

    21/34

    Request & Response

    Hypertext Transfer Protocol reference to:

    http://djce.org.uk/dumprequest

    http://www.w3.org/Protocols/rfc2616/rfc2616.html

    http://www.faqs.org/rfcs/rfc2616.html

  • 7/31/2019 Chapter2 Servlet Programming

    22/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    Step by step

    Define a class name LoginServlet extends from GenericServlet

    or HttpServlet class Implement/override the following methods

    9 in i t ()

    9

    service()9 destroy()

    Compile and deploy on webserver.

    Call to run by URL from the client side

  • 7/31/2019 Chapter2 Servlet Programming

    23/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    import javax.servlet.http.*;

    import java.io.*;

    import java.sql.*;

    public class LoginServlet extends HttpServlet {

    public void service(HttpServletRequest request,

    HttpServletResponse response) throws IOException {

    // .

    }}

  • 7/31/2019 Chapter2 Servlet Programming

    24/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    // inside theservice method

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    out.println("");String strUsername = request.getParameter("Username");

    String strPassword = request.getParameter("Password");

  • 7/31/2019 Chapter2 Servlet Programming

    25/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    Deploy the servlet on Apache Tomcat Web Server

    Web Server Apache Tomcat (read the document of other webservers to know how to deploy a servlet)

    Step 1: Copy servlet file class into WEB-INF\classes ofyour web application.

    Step 2:

    Add information about your servlet into web.xml file tonotify with the web server or application server.

    WEB-INF\web.xml

  • 7/31/2019 Chapter2 Servlet Programming

    26/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    w eb.xm l fi le (configurat ion)

    < w eb-app>

    LoginServlet

    LoginServlet

    LoginServlet

    /servlets/LoginServlet

    < / w eb-app>

  • 7/31/2019 Chapter2 Servlet Programming

    27/34

    @2008 Huynh Ngoc Tin

    Example Login Servlet

    Call a servlet f rom client side

    Call a servlet when user clicks submit or login button from theLoginForm/client side

    < Form act ion= " ../ ../ serv let s/ LoginServ let "method= "post ">

  • 7/31/2019 Chapter2 Servlet Programming

    28/34

    Directly

    Using the connection pool

    Using MySQL JDBC Driver: download andadd mysql-connector-java-5.1.7-bin.jar to

    C:\Sun\AppServer\domains\domain1\lib

    Connecting to Database

  • 7/31/2019 Chapter2 Servlet Programming

    29/34

    Connecting to Database

  • 7/31/2019 Chapter2 Servlet Programming

    30/34

  • 7/31/2019 Chapter2 Servlet Programming

    31/34

    Connecting to Database- ConnectionPool

  • 7/31/2019 Chapter2 Servlet Programming

    32/34

    Connecting to Database- Connection Pool

  • 7/31/2019 Chapter2 Servlet Programming

    33/34

    Connecting to Database-Connection Pool

  • 7/31/2019 Chapter2 Servlet Programming

    34/34

    @2008 Huynh Ngoc Tin

    Deploy a web app on Tomcat webserver


Recommended