Introduction to Java Servlets
Java servlets are an integral part of developing a web application in Java. From responding to client requests to processing and returning a response, it's difficult to create a web application of any complexity without the use of servlets at some level. Even Java server pages (JSPs) are compiled by the servlet container into servlets behind the scenes. A web application consisting entirely of JSPs is still primarily a servlet-based website.
-
History
-
Java servlets were introduced by Sun in 1997 as a means to get Java programming into the Internet development scene. Eventually, servlets were incorporated into what is now known as the Java Enterprise Edition (Java EE or J2EE in some contexts), which comprises servlets, JSPs, Enterprise Java Beans (EJB) and several other technologies. Servlets play a large role in web development and are a key part of any Java developer's repertoire.
Servlet Requirements
-
Just as any other web development technology (such as PHP, Perl CGI or ASP) requires special server software and configuration, so do servlets. Servers that are designed to work with Java servlets (and JSPs) are generally known as "servlet containers" or "application servers." These containers have a dual purpose, serving the content (similar to Apache or other "standard" web servers) as well as executing the underlying Java code.
Several free and commercial containers exist; common ones include Apache Tomcat, IBM's WebSphere, Sun's GlassFish and JBoss.
How Servlets Process Requests
-
Servlets follow a standard method of handling client requests. The request comes through the application server, which determines which servlet the request is for and what type of processing the servlet is supposed to perform (such as whether or not the request is an HTTP POST or a GET). The server calls the appropriate functionality on the servlet, which processes the request as desired. Once the servlet finishes with the request, it sends a response to the client.
Servlets and JSPs
-
While it is possible--and even relatively common--to develop a Java-based website using strictly JSPs, including servlets in the design can greatly increase the extensibility and maintainability of the website for future development. By using JSPs strictly for presentation, and performing the vast bulk of logic within servlets, it's easier to extract logic for testing, or even to other classes and libraries where the code can be reused.
Servlet Deployment
-
Developers who come to Java servlets from PHP or similar environments might first be somewhat confused by the development model. Unlike other web development environments, servlets require the compilation and deployment of a web application, most commonly through "WAR" files, or web application archives. This is a version of the standard JAR file with which most Java developers are familiar, and it includes all of the generated servlets, web content and related configuration files.
-
Related Searches
References
Resources
- Photo Credit Building blocks image by Andrius Grigaliunas from Fotolia.com