Java Servlet Prgramming: Chapter #2
Java Servlet Prgramming: Chapter #2
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
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.
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.
Servlet Interface
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.
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.
Servlet Interface
Handle request
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
<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
Call a servlet when user clicks submit or login button from the LoginForm/client side
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