35
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Embed Size (px)

Citation preview

Page 1: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Chapter #2

JAVA SERVLET

PRGRAMMING

Page 2: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Objective

Basic concepts

Why?

What is java servlet?

Servlet container

Servlet life circle

Servlet API, servlet interface

Build, deploy, run a java servlet

Page 3: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

References

[1] Chapter 3. Prentice Hall Ptr Java(Tm) Ee 5

Tutorial, 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

Page 4: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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

Page 5: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

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?

Page 6: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

What is java Servlet?

One of technologies of J2EE/JEE Platform.

Java Servlet is Java component that runs on web

container.

Java Servlets “talks” to Web client base on request/

response that is managed by Servlet Container.

Page 7: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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.

Page 8: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

What is servlet container (Web container)?

Page 9: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Java Servlet on JEE Platform

Page 10: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

The javax.servlet and javax.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

Page 11: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

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

Page 12: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Servlet Interface

Page 13: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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.

Page 14: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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 nghĩa 1 servlet

độc lập protocol, để định nghĩa 1

servlet dùng trên Web ta có thể cho

kế thừa lớp HttpServlet.

Page 15: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Servlet Interface

Handle request service method: servlet Interface define

service method to handle requests from the client side.

Servlet Container will call service method to handle the request from the client side.

Page 16: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Servlet Interface

HttpRequest methods• HttpServlet have some more methods to handle

HttpRequest: doGet for handling HTTP GET requests doPost for handling HTTP POST requests …

• The above methods can are called automatically by service method

Page 17: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Servlet life circle

Servlet container have only one instance for each servlet.

Instance is handle by init, service, destroy in Servlet

Interface (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.

Page 18: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Request & Reponse

Page 19: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Request & Reponse

Request Package all information that comes from the client. Request from Client -> Server follows the HTTP

protocol HttpServletRequest methods can summarize, read

the request’s information.

Page 20: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Request & Response

Response Package all information that will response for the

client. Information in response object are sent to client by

HTTT protocol HttpServletReponse methods can summarize,

package information into response object.

Page 21: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

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

Page 22: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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

init() service() destroy()

Compile and deploy on webserver. Call to run by URL from the client side

Page 23: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@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 {

// ….

}

}

Page 24: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Example – Login Servlet

// inside the service method

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

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

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

Page 25: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Example – Login Servlet

Deploy the servlet on Apache Tomcat Web Server

Web Server Apache Tomcat (read the document of other web servers to know how to deploy a servlet) Step 1: Copy servlet file class into WEB-INF\classes

of your web application. Step 2:

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

WEB-INF\web.xml

Page 26: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Example – Login Servlet

web.xml file (configuration)

<web-app>

<servlet>

<servlet-name>LoginServlet</servlet-name>

<servlet-class>LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>LoginServlet</servlet-name>

<url-pattern>/servlets/LoginServlet</url-pattern>

</servlet-mapping>

</web-app>

Page 27: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Example – Login Servlet

• Call a servlet from client side

• Call a servlet when user clicks submit or login button from the LoginForm/client side

• …• <Form action="../../servlets/LoginServlet"

method="post">• …

Page 28: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Directly Using the connection pool

Connecting to Database

Page 29: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Connecting to Database

Page 30: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Connecting to Database

Page 31: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Connecting to Database

Page 32: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Connecting to Database

Page 33: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

Connecting to Database

Page 34: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Deploy a web app on Tomcat webserver

Page 35: @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

@2008 Huynh Ngoc Tin

Exercises

Study by yourself client side programming such as HTML, Java Script.

Build, deploy and run servlets which do the below functions using NetBean 6.1 (include JBoss, GlassFish Application Server) Registration (RegisterServlet) Login (LoginServlet) Change password, registered information

(UpdateUserInformationServlet) Read more about JBoss, GlassFish, Tomcat document to

deploy a java servlet