Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: What's new in Java Servlet API 2.2? - JavaWorld October 1999

What's new in Java Servlet API 2.2? - JavaWorld October 1999

Tutorial Details:

What's new in Java Servlet API 2.2?
What's new in Java Servlet API 2.2?
By: By Jason Hunter
A full update on the latest Java Servlet API
n August 23, Sun Microsystems published the first public release of the specification for Java Servlet API 2.2 (see the Resources section below for a link to the formal specification). Included in the specification are some very exciting enhancements to servlets. This article describes what's new in version 2.2 of the API, explains the decision-making process behind the changes, and demonstrates how to write servlets using API 2.2 features. To keep the article somewhere near a reasonable length, I'm going to assume that you're familiar with the classes and methods of previous versions of the Java Servlet API. If that's not the case, you can peruse Resources for links to sites that will help get you up to speed.
Java Servlet API 2.2 includes many enhancements that make servlets more powerful than ever:
Servlets are now part of the Java 2 Platform, Enterprise Edition specification
Servlets now embrace the notion of pluggable Web applications, which can be configured and deployed in a server-independent manner
Rules have been provided that define how servlets can be distributed across multiple back-end servers
Response output buffering has been added
Control over HTTP headers has been enhanced
New styles of request dispatching have been added
More advanced error handling can now be used
Several method signatures have been changed to keep the API consistent
Before we begin our examination of these enhancements, let me point out that version 2.2 has been released as a specification only; no Web server yet supports it. Even Sun's official reference implementation is still perhaps a couple months away; it is expected to be released with source code as part of the Jakarta Project (see Resources for information on Jakarta). So be careful, boys and girls; don't try these code examples at home!
Java 2 Platform, Enterprise Edition
One of the first things one notices when reading the Java Servlet API 2.2 specification is that the term servlet engine has been replaced by servlet container. This minor change is indicative of a larger one: the Java Servlet API is now a required API of the Java 2 Platform, Enterprise Edition (J2EE) specification and, throughout J2EE's terminology, container is preferred over engine. The addition of servlets to J2EE has no real effect on pure servlet developers (except for the fact that we have to stop saying "engine"). But it does guarantee that enterprise developers using J2EE will always have support for servlets. (Lucky developers!)
Web applications
Java Servlet API 2.2 includes one new feature so significant it may change the way the Web works. That feature: Web applications.
A Web application, as defined in the servlet specification, is a collection of servlets, JavaServer Pages (JSPs), HTML documents, images, and other Web resources that are set up in such a way as to be portably deployed across any servlet-enabled Web server. Installing a Web app is simple. Gone are the days of detailed instruction sheets telling you how to install third-party Web components, with different instructions for each type of Web server. With Web apps, the entire application can be contained in a single archive file and deployed by placing the file into a specific directory.
War: What is it good for?
Web app archive files have the extension .war , which stands for Web application archive.
War files are actually jar files (created using the jar utility) saved with an alternate extension. Using the jar format allows jar files to be stored in compressed form and have their contents digitally signed. The .war file extension was chosen over .jar to let people and tools know to treat them differently.
Inside a war file you might find a file listing like this:
index.html
howto.jsp
feedback.jsp
images/banner.gif
images/jumping.gif
WEB-INF/web.xml
WEB-INF/lib/jspbean.jar
WEB-INF/classes/MyServlet.class
WEB-INF/classes/com/mycorp/frontend/CorpServlet.class
WEB-INF/classes/com/mycorp/frontend/SupportClass.class
On install, a war file can be mapped to any URI prefix path on the server. The war file then handles all requests beginning with that prefix. For example, if the war file above were installed under the prefix /demo , the server would use it to handle all requests beginning with /demo . A request for /demo/index.html would serve the index.html file from the war file. A request for /demo/howto.jsp or /demo/images/banner.gif would also serve content from the war file.
About the WEB-INF directory
The WEB-INF directory is special. The files there are not served directly to the client; instead, they contain Java classes and configuration information for the Web app. The directory behaves like a jar file's META-INF directory; it contains metainformation about the archive contents.
The WEB-INF/classes directory contains the class files for this Web app's servlets and support classes. WEB-INF/lib contains classes stored in jar files. For convenience, Web server class loaders automatically look to WEB-INF/classes and WEB-INF/lib for their classes -- no extra install steps are necessary.
The servlets under WEB-INF in this Web app can be invoked using URIs like /demo/servlet/MyServlet and /demo/servlet/com.mycorp.frontend.CorpServlet . Notice how every request for this app begins with /demo , even requests for servlets.
The web.xml file in the WEB-INF directory is known as a deployment descriptor. This file contains configuration information about the Web app in which it resides. It's an XML file with a DTD (set of tags and structure) specified in detail as part of the Java Servlet API. The DTD contains over 50 tags, allowing you to specify any of the following:
Graphical icon files for the application
Useful for GUI manipulation.
A description of the app
Information on what the app does, who wrote it, where it can be found,
and so on.
A flag indicating whether or not the app can be distributed
Distributed apps can be spread across multiple back-end servers to
improve performance and add fail-over support, but such apps must be written according to stricter rules than their nondistributed counterparts. For example, objects placed into sessions for a distributed app should be serializable. This flag indicates whether the app has been written in accordance with the stricter rules.
Parameter information for the app
Essentially, these are init parameters for the application.
Registered servlet names
A place to register servlets and give them names. Previously, each
server had a different process for registering servlets, making
deployment difficult.
Servlet init parameters
Pass servlets parameters at initialization time. A new standard
way to accomplish what used to be a server dependent process.
Servlet load order
Specifies which servlets are preloaded, and in what order.
URL mapping rules
Standardized mappings from URLs to servlets. For example,
allow /lite/* to be handled by LiteServer and *.jsp to be handled by
JspServlet (a mapping needed to support JSPs). Note that a /lite/*
mapping for this Web app would handle requests beginning with
/demo/lite/ .
Session timeout defaults
Specify how many minutes of client inactivity can go by before a session times out.
File extension to MIME type mappings
Override server defaults or add mappings not known to the server.
A welcome file list
An ordered list of files to look for when a request comes in for a
directory without specifying the file. Often index.jsp , index.html ,
index.htm .
Error-handling rules
Specify Web pages to handle various kinds of errors. Pages can be
registered based on error code (for example, 404 errors serve some
specific page) or based on exception type (for example, if a servlet throws
javax.servlet.UnavailableException , display some explanatory page).
References to external data sources, such as JNDI
Add resources into the JNDI lookup table, like database connections.
Allow the resources to be located by servlets using a simple name
lookup.
Security constraints
Dictate which pages must be protected, and by what mechanism.
Include built-in form-based authentication.
For those interested, the Web app DTD and a simple example web.xml file can be found in the Resources section. A full description of all the elements in the DTD would extend this article beyond a reasonable length, and may be of little interest in the long run, as these files are likely to be generated by graphical tools; thus, I will not describe the elements here.
The structure of the web.xml file is not in itself important; what interests us is the fact that having a deployment descriptor file allows configuration information to be specified in a server-independent manner, greatly simplifying the deployment process. Because of deployment descriptors, not only are simple servlets portable, but you can now transfer whole self-contained subsections of your site between servers.
As Java Servlet API 2.2 gains in popularity, it's likely that a commercial market for war files will develop. War files will become pluggable Web components, capable of being downloaded and installed and put to work right away -- no matter what your operating system or Web server. $19.95 for a site search engine, anyone?
Deployment descriptors also provide Web-hosting companies with a convenient way to support multiple customers on the same server. Customers can be given control over their individual domains. They can individually manage servlet registration, URL mappings, MIME types, and page-level security constraints -- without needing general access to the Web server.
Now let's take a look at how Web apps are implemented.
Web apps: A programmer's view
From a programmer's point of view, a Web app corresponds to one ServletContext object. All servlets inside a Web app share the same ServletContext


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
What's new in Java Servlet API 2.2? - JavaWorld October 1999

View Tutorial:
What's new in Java Servlet API 2.2? - JavaWorld October 1999

Related Tutorials:

3D graphics programming in Java, Part 3: OpenGL
3D graphics programming in Java, Part 3: OpenGL
 
Untangle your servlet code with reflection
Untangle your servlet code with reflection
 
Boost Struts with
Boost Struts with XSLT and XML
 
Mix protocols transparently in Web applications
Mix protocols transparently in Web applications
 
J2SE 1.4 breathes new life into the CORBA community, Part 1
J2SE 1.4 breathes new life into the CORBA community, Part 1
 
Effort on the edge, Part 1
Effort on the edge, Part 1
 
Servlet 2.4: What's in store
Servlet 2.4: What's in store
 
good design pattern
good design pattern
 
Java and GIS, Part 2: Mobile LBS
Java and GIS, Mobile LBS Using LBS First, let\'s make sure that we understand what an LBS application is. Typically, an LBS application is trying to answer the question \"Where am I?\" and then do something with that information. There are a number
 
Groovy, Java\'s New Scripting Language
Groovy, Java\'s New Scripting Language When some Java developers hear about Groovy, their first reaction often is, as mine was, "Oh, no, not another scripting language for Java." We already have, after all, JavaScript and Rhino, Jython, Jelly, BeanShell,
 
Accessing Databases Using Java and JDBC
This article will show how a Java Application, Applet or Servlet can access data stored in relational databases using the JDBC API.
 
Introduction to JSP
Introduction to JSP Introduction to JSP Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the
 
Internet & Intranets: Java Servlets
What are servlets? "Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to upd
 
Building Java Server Pages
A detailed look at building JSP pages. Should you use JSP or servlets? It mainly depends on the ratio of markup to code. Here you'll also find a guide to the different varieties of tag, and details about the main tags such as and  
ColdCafe ver 1.2
ColdCafe is a macro-processor servlet. This servlet parses html pages and replaces some predefined elements with their values. It is not a new language like JSP, it is just a set of html-preprocessing that keep your hands free for using any web-authoring
 
Introduction to Servlets, JSP, and Servlet Engines
Servlets are the Java Technologies' answer to CGI programming. They are programs which run on the server side and generate dynamic content. Why would one prefer to use Servlets over traditional CGI programming?
 
J2ME Technology Turns 5!
In 2004 the Java 2 Platform, Micro Edition (J2ME) celebrated its fifth anniversary. This article presents where J2ME is today.
 
Accessing Database from servlets through JDBC!
Accessing Database from servlets through JDBC! Java Servlets J ava Servlets are server side components that provides a powerful mechanism for developing server side of web application. Earlier CGI was developed to provide server side capabilities
 
Comparing J2ME Multimedia Options
This article presents the latest developments in MMAPI: the new security considerations raised in MMAPI 1.1, the differences between MMAPI and the MIDP 2.0 Media API, J2ME Wireless Toolkit 2.2 support for MMAPI, and JSR 234, Advanced Multimedia Supplement
 
Easy Emulation With New NetBeans Mobility Pack 4.0
With the click of a button, switch back and forth between different emulation environments while developing one set of code. It's never been this easy to take advantage of Java technology's cross-platform capabilities.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.