Java Servlets:
Java servlets are server-side programs (running inside a web
server) that handle clients' requests and return a customized or dynamic
response for each request. The dynamic response could be based on user's input
(e.g., search, online shopping, online transaction) with data retrieved from
databases or other applications, or time-sensitive data (such as news and stock
prices).
Java servlets typically run on the HTTP protocol. HTTP is an
asymmetrical request-response protocol. The client sends a request message to
the server, and the server returns a response message as illustrated.
Server-Side
Technologies
There are many (competing) server-side technologies
available: Java-based (servlet, JSP, JSF, Struts, Spring, Hibernate), ASP, PHP,
CGI Script, and many others.
Pre-requisites
HTML, Java Programming Language, HTTP and Apache Tomcat
Server, SQL and MySQL Database System, and many others.
Apache Tomcat Server
Servlets are server-side programs run inside a Java-capable
HTTP server. Apache Tomcat Server (@ http://tomcat.apache.org) is the official
Reference Implementation (RI) for Java servlet and JSP, provided free by
open-source foundation Apache (@ http://www.apache.org).
You need to install Tomcat to try out Java servlets. Read
"How to Install Tomcat and Get Started Java Servlet Programming".
I shall denote Tomcat's installed directory as
<CATALINA_HOME>, and assume that Tomcat server is running in port 8080.
Tomcat provides many excellent servlet examples in
"<CATALINA_HOME>\webapps\examples\servlets". You can run these
examples by launching Tomcat and issuing URL http://localhost:8080/examples.
Java Servlet Versions
Java Servlet has these versions: [TODO features and what is
new]
J2EE 1.2 (December 12, 1999) (Java Servlet 2.2, JSP 1.1, EJB
1.1, JDBC 2.0)
J2EE 1.3 (September 24, 2001) (Java Servlet 2.3, JSP 1.2,
EJB 2.0, JDBC 2.1)
J2EE 1.4 (November 11, 2003) (Java Servlet 2.4, JSP 2.0, EJB
2.1, JDBC 3.0)
Java EE 5 (May 11, 2006) (Java Servlet 2.5, JSP 2.1, JSTL
1.2, JSF 1.2, EJB 3.0, JDBC 3.0)
Java EE 6 (December 10, 2009) (Java Servlet 3.0, JSP 2.2/EL
2.2, JSTL 1.2, JSF 2.0, EJB 3.1, JDBC 4.0)
Java EE 7: expected in end of 2012.
Java Servlet is the foundation technology for Java
server-side programming. You need to understand Servlet thoroughly before you could
proceed to other Java server-side technologies such as JavaServer Pages (JSP)
and JavaServer Faces (JSF).
Review of HTTP
A HTTP Servlet runs under the HTTP protocol. It is important
to understanding the HTTP protocol in order to understand server-side programs
(servlet, JSP, ASP, PHP, etc) running over the HTTP. Read "HTTP
Basics", if needed.
The above configuration defines a servlet named
"HelloWroldServlet", implemented in
"mypkg.HelloServlet.class" (written earlier), and maps to URL
"/sayhello", where "/" denotes the context root of this
webapp "helloservlet". In other words, the absolute URL for this
servlet is http://hostname:port/helloservlet/sayhello
Developing and
Deploying Web Applications using IDE
It is a lot more productive and efficient to use an IDE
(such as Eclipse or NetBeans) to develop your web application. You could
start/stop your servers from IDE directly. You could debug your web application
in IDE, like debugging standalone application.
NetBeans: Read "Developing and Deploying Web
Applications in Netbeans".
Eclipse: Read "Developing and Deploying Web
Applications in Eclipse".
Tomcat's Servlet
Examples
Tomcat provides a number of excellent servlet examples in
"<CATALINA_HOME>\webapps\examples". The servlet source files
are kept under
"<CATALINA_HOME>\webapps\examples\WEB-INF\classes", together
with the compiled classes. To run the examples, start Tomcat server and issue
URL http://localhost:8080/examples.
I strongly encourage you to study the examples, Read
"Tomcat's Java Servlet Examples Explained".
Database Servlet
Read "Java Servlet Case Study" and "Java
Servlet Case Study Continue".
Servlet API – A
Deeper Look
A servlet is a Java web component, managed by a servlet
container (such as Apache Tomcat or Glassfish), which generates dynamic content
in response to client's request. A servlet container (or servlet engine) is a
web server extension which provides servlet functionality. A servlet container
contains and manages servlets throughout their lifecycle.
Servlet 3.0
Servlet API 3.0 introduces these annotations to simplify
deployment in javax.servlet.annotation package:
@WebServlet: Define a servlet component
@WebInitParam: Define initialization parameters for a
servlet
@WebListener: Define a listener
@WebFilter: Define a filter
@MultipartConfig: For multipart file upload
For example,
@WebServlet(
name =
"HelloServletExample",
urlPatterns =
{"/sayhello"},
initParams = {
@WebInitParam(name = "param1", value = "value1"),
@WebInitParam(name = "param2", value = "value2")}
)
public class HelloServlet extends HttpServlet { ...... }
@WebServlet
@WebServlet defines a servlet
component and its metadata, with the following attributes:
String[] urlPatterns: An array of
String declaring the url-pattern for servlet-mapping. Default is an empty array
{}.
String[] value: urlPatterns.
String name: servlet-name, default is empty string
"".
loadOnStartup: The load-on-startup order of the servlet,
default is -1.
WebInitParam[] initParams: The init parameters of the
servlet, default is an empty array {}.
boolean asyncSupported: Declares whether the servlet
supports asynchronous operation mode, default is false.
Developing and
Deploying Web Applications using IDE
It is a lot more productive and efficient to use an IDE
(such as Eclipse or NetBeans) to develop your web application. You could
start/stop your servers from IDE directly. You could debug your web application
in IDE, like debugging standalone application.
NetBeans: Read "Developing and Deploying Web
Applications in Netbeans".
Eclipse: Read "Developing and Deploying Web
Applications in Eclipse".
Tomcat's Servlet
Examples
Tomcat provides a number of excellent servlet examples in
"<CATALINA_HOME>\webapps\examples". The servlet source files
are kept under
"<CATALINA_HOME>\webapps\examples\WEB-INF\classes", together
with the compiled classes. To run the examples, start Tomcat server and issue
URL http://localhost:8080/examples.
I strongly encourage you to study the examples, Read
"Tomcat's Java Servlet Examples Explained".
Database Servlet
Read "Java Servlet Case Study" and "Java
Servlet Case Study Continue".
Servlet API – A
Deeper Look
A servlet is a Java web component, managed by a servlet
container (such as Apache Tomcat or Glassfish), which generates dynamic content
in response to client's request. A servlet container (or servlet engine) is a
web server extension which provides servlet functionality. A servlet container
contains and manages servlets throughout their lifecycle.
Servlet 3.0
Servlet API 3.0 introduces these annotations to simplify
deployment in javax.servlet.annotation package:
@WebServlet: Define a servlet component
@WebInitParam: Define initialization parameters for a
servlet
@WebListener: Define a listener
@WebFilter: Define a filter
@MultipartConfig: For multipart file upload
For example,
@WebServlet(
name =
"HelloServletExample",
urlPatterns =
{"/sayhello"},
initParams = {
@WebInitParam(name = "param1", value = "value1"),
@WebInitParam(name = "param2", value = "value2")}
)
public class HelloServlet extends HttpServlet { ...... }
@WebServlet
@WebServlet defines a servlet
component and its metadata, with the following attributes:
String[] urlPatterns: An array of
String declaring the url-pattern for servlet-mapping. Default is an empty array
{}.
String[] value: urlPatterns.
String name: servlet-name, default is empty string
"".
loadOnStartup: The load-on-startup order of the servlet,
default is -1.
WebInitParam[] initParams: The init parameters of the
servlet, default is an empty array {}.
boolean asyncSupported: Declares whether the servlet
supports asynchronous operation mode, default is false.
No comments:
Post a Comment