servlet

Cards (42)

  • CSI3023-Advanced Server-Side Programming
  • CSI3023-Advanced Server-Side Programming
  • Topics
    • Servlet
    • JSP
    • JSF
  • Tiered Architecture

    The two-tiered client-server model has been superseded by the multi-tiered architecture prevalent in the enterprise applications
  • Benefits of having a tiered application

    • Encapsulates rules and functionality together providing for easier maintenance & development
    • Enhances flexibility and reusability of logic and software components
    • Allows developers to focus on the area of their specialty e.g. database, servers, web page, etc.
  • Server Side Development Technologies

    • Servlet
    • JSP
    • JDBC
  • Servlet
    A server side Java based programming language widely used to create dynamic web applications. Servlet runs inside a servlet container.
  • Execution of Servlets

    1. Client sends a request
    2. Web server receives the request
    3. Sends to corresponding servlet
    4. Servlet processes the request
    5. Response to the server
    6. Response to the client
  • Servlet Life Cycle

    1. Servlet is initialized by calling the init() method
    2. Servlet calls service() method to process a client's request
    3. Servlet is terminated by calling the destroy() method
    4. Servlet is garbage collected by the garbage collector of the JVM
  • Servlet API Packages

    • javax.servlet
    • javax.servlet.http
  • Servlet Example
    • HelloWorld
  • Deployment Descriptor (web.xml)

    A xml file where <web-app> is the root element. Web server uses web.xml file to map the URL of the request to the specific code that handles the request.
  • Deployment Descriptor (web.xml)

    • <servlet-name>HelloWorld</servlet-name>
    • <servlet-class>HelloWorld</servlet-class>
    • <url-pattern>/HelloWorld</url-pattern>
  • Using Annotations

    Annotations and web.xml are two ways to configure Java Servlets. Annotations are more convenient and prevent you from having bloated web.xml with hundreds of entries (in case of bigger application). It is also a part of Convention over configuration approach.
  • @WebServlet Annotation
    The @WebServlet annotation is used to declare a servlet. The annotated class must extend the javax.servlet.http.HttpServlet class.
  • Using Annotations

    • @WebServlet("/processForm")
    • public class MyServlet extends HttpServlet { ... }
  • Servlet Annotations

    • displayName
    • description
    • smallIcon
    • largeIcon
  • Servlet Request Methods

    • getParameter()
    • getParameterMap()
    • getParameterValues()
    • getQueryString()
    • getRequestURI()
    • getRequestURL()
    • getMethod()
    • getRemoteAddr()
    • getRemoteHost()
    • getServerName()
    • getServerPort()
  • Servlet Response Methods

    • setContentType()
    • setStatus()
    • setHeader()
    • setIntHeader()
    • setDateHeader()
    • getWriter()
    • getOutputStream()
  • Servlet Request and Response

    • HTML form with action="serv" method="get"
    • Servlet code to get request parameters and print response
  • Servlet Request Dispatcher

    RequestDispacther is an interface used to receive requests from the users and bind it with other files such as HTML file, Servlet file, JSP file etc. Servlet container is responsible to create RequestDispatcher object. RequestDispacther provides forward() and include() methods.
  • Servlet Request Dispatcher

    • HTML form with action="serv" method="post"
    • Servlet code to check password length and forward/include request
  • Servlet Redirection

    The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It works at client side because it uses the url of the browser to make another request.
  • Servlet Redirection

    • Servlet code to redirect to google.com
    • Servlet code to redirect to another servlet named "redirected"
  • Servlet Session Management

    A session is a sequence of related requests and responses between a Web client and a Web server. HTTP is a state-less protocol, so session management is required to remember previous communications.
  • Session Tracking Mechanisms

    • HttpSession
    • Cookies
  • HttpServletResponse
    Servlet interface that provides methods to send a response to the client
  • Redirecting to another servlet

    1. resp.sendRedirect(req.getContextPath() + "/redirected")
    2. Redirected to another servlet named "redirected"
  • Servlet Session Management

    A sequence of related requests and responses between a Web client and a Web server
  • HTTP is a stateless protocol
  • Ways to maintain session
    • HttpSession
    • Cookies
    • Hidden Form Field
    • URL Rewriting
  • Session Tracking

    1. Browser sends first request to server
    2. Server checks if browser has identified with session cookie
    3. If server doesn't know client, it creates new unique identifier and sends cookie response
    4. If server already knows client, it obtains session corresponding to passed unique identifier in cookie
  • HttpSession
    Interface used by servlet container to create a session between Http client and Http server
  • HttpSession Example

    • Checking if user is new or returning
    • Incrementing visit count
    • Displaying session id and last accessed time
  • JDBC
    Java DataBase Connectivity - API for connecting Java programs to relational databases
  • JDBC Steps

    1. Importing Packages
    2. Registering the JDBC Drivers
    3. Opening a Connection to a Database
    4. Creating a Statement Object
    5. Executing a Query and Returning a Result Set Object
    6. Processing the Result Set
    7. Closing the Connection
  • JDBC Packages

    • java.sql
    • javax.sql
  • JDBC Drivers
    • com.mysql.jdbc.Driver
    • oracle.jdbc.driver.OracleDriver
    • com.microsoft.sqlserver.jdbc.SQLServerDriver
    • net.ucanaccess.jdbc.UcanaccessDriver
  • JDBC Connection String/DB URL

    • jdbc:mysql://HOST_NAME:PORT/DATABASE_NAME
    • jdbc:oracle:thin:@HOST_NAME:PORT:SERVICE_NAME
    • jdbc:sqlserver://HOST_NAME:PORT;DatabaseName=DATABASE_NAME
  • JDBC Statement Interfaces

    • Statement
    • Prepared Statement
    • Callable Statement