Overview

From the Sun Java Servlet Technology Overview:

Servlets are the Java platform technology of choice for extending and enhancing Web servers. Servlets provide a component-based, platform-independent method for building Web-based applications, without the performance limitations of CGI programs. And unlike proprietary server extension mechanisms (such as the Netscape Server API or Apache modules), servlets are server- and platform-independent. This leaves you free to select a "best of breed" strategy for your servers, platforms, and tools.

Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and crash protection.

Today servlets are a popular choice for building interactive Web applications. Third-party servlet containers are available for Apache Web Server, Microsoft IIS, and others. Servlet containers are usually a component of Web and application servers, such as BEA WebLogic Application Server, IBM WebSphere, Sun Java System Web Server, Sun Java System Application Server, and others.

You might want to check out the latest information on JavaServer Pages (JSP) technology. JSP technology is an extension of the servlet technology created to support authoring of HTML and XML pages. It makes it easier to combine fixed or static template data with dynamic content. Even if you're comfortable writing servlets, there are several compelling reasons to investigate JSP technology as a complement to your existing work.

Servlets in JBoss

Developing and deploying a Servlet to run on JBoss is very straightforward. There are no additional configuration aspects beyond that of developing a traditional Servlet (i.e. servlet and servlet-mapping entries in web.xml).

Packaging

Servlets should be packaged into a WAR (Web Application Archive). The primary deployment descriptor is web.xml. JBoss specific configuration for the web application may be defined in jboss-web.xml.

Example Code

HelloWorldServlet.java

   1 /*
   2  * Copyright (c) 2008 Object Computing, Inc.
   3  * All rights reserved.
   4  */
   5 
   6 package com.ociweb.jboss.example.servlet;
   7 
   8 import javax.servlet.ServletException;
   9 import javax.servlet.http.HttpServlet;
  10 import javax.servlet.http.HttpServletRequest;
  11 import javax.servlet.http.HttpServletResponse;
  12 import java.io.IOException;
  13 import java.io.PrintWriter;
  14 
  15 public class HelloWorldServlet extends HttpServlet {
  16 
  17     @Override
  18     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  19             throws ServletException, IOException {
  20         
  21         PrintWriter out = response.getWriter();
  22 
  23         out.println("<html>");
  24         out.println(" <head>");
  25         out.println("  <title>Hello World - Servlet Example</title>");
  26         out.println(" </head>");
  27         out.println(" <body>");
  28         out.println("  <h1>Hello, World!</h1>");
  29         out.println(" </body>");
  30         out.println("</html>");
  31     }
  32 }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright (c) 2008 Object Computing, Inc.
    All rights reserved.
-->
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>servlet-example</display-name>

    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.ociweb.jboss.example.servlet.HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright (c) 2008 Object Computing, Inc.
    All rights reserved.
-->
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">

<jboss-web>
    <context-root>servlet-example</context-root>
</jboss-web>

Download

The example code may be downloaded and extracted locally. To deploy the example code, first modify the build.properties file in the project root and set deploy.dir to a deployable location on the filesystem (i.e. JBOSS_HOME/server/default/deploy/). Once deploy.dir is defined, start your JBoss instance, and issue: ant deploy. You may test the servlet by opening a browser, and visiting: http://localhost:8080/servlet-example/hello.

See Also

JBossSupport/ExampleServlet (last edited 2008-06-06 21:00:52 by StevenStallion)