0% found this document useful (0 votes)
60 views

Java Servlet Prgramming: Chapter #2

The document discusses Java servlets, including: - What Java servlets are and their purpose in providing dynamic web content. - The basic concepts of servlets like the servlet interface, lifecycle, and request/response model. - How to build, deploy and run a simple login servlet as an example. - How servlets are configured and mapped to URLs in the web.xml deployment descriptor. - Connecting servlets to databases using direct JDBC connections or connection pools.

Uploaded by

Tick Tack
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Java Servlet Prgramming: Chapter #2

The document discusses Java servlets, including: - What Java servlets are and their purpose in providing dynamic web content. - The basic concepts of servlets like the servlet interface, lifecycle, and request/response model. - How to build, deploy and run a simple login servlet as an example. - How servlets are configured and mapped to URLs in the web.xml deployment descriptor. - Connecting servlets to databases using direct JDBC connections or connection pools.

Uploaded by

Tick Tack
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

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

@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
@2008 Huynh Ngoc Tin

Why?
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, useroriented content.

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

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

@2008 Huynh Ngoc Tin

What is servlet container (Web container)?

@2008 Huynh Ngoc Tin

Java Servlet on JEE Platform

@2008 Huynh Ngoc Tin

Servlet API
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 history


Packages: javax.servlet & javax.servlet.http

@2008 Huynh Ngoc Tin

Servlet Interface

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

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

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

@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

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

@2008 Huynh Ngoc Tin

Request & Response

@2008 Huynh Ngoc Tin

Request & Response


Request Package all information that comes from the client. Request from Client -> Server follows the HTTP protocol HttpServletRequest methods can summarize, read the requests information.

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

@2008 Huynh Ngoc Tin

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

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

@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 { // . } }

@2008 Huynh Ngoc Tin

Example Login Servlet


// inside the service method response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); String String strUsername strPassword = request.getParameter("Username"); = request.getParameter("Password");

@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

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

@2008 Huynh Ngoc Tin

Connecting to Database
Directly Using the connection pool Using MySQL JDBC Driver: download and add mysql-connector-java-5.1.7-bin.jar to C:\Sun\AppServer\domains\domain1\lib

Connecting to Database

Connecting to Database - ConnectionPool

Connecting to Database- ConnectionPool

Connecting to Database- Connection Pool

Connecting to Database-Connection Pool

Deploy a web app on Tomcat webserver

@2008 Huynh Ngoc Tin

You might also like