advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Articles : Tutorials : Server-Side Web Applications Using Servlets and JSP :

Part 3: First Servlets

Contents
Basic Servlet Structure
A Simple Servlet Generating Plain Text
A Servlet that Generates HTML
Simple HTML-Building Utilities

2. A Simple Servlet Generating Plain Text

Here is a simple servlet that just generates plain text. The following section will show the more usual case where HTML is generated.

2.1 HelloWorld.java

You can also download the source or try it on-line.

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletRequest response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

2.2 Compiling and Installing the Servlet

Note that the specific details for installing servlets vary from Web server to Web server. Please refer to your Web server documentation for definitive directions. I'm using Sun's Java Web Server 2.0 for these examples, where servlets are expected to be in a directory called "servlets" in the JWS installation hierarchy. However, I placed this servlet in a separate package to avoid conflicts with other servlets on this server; you'll want to do the same if you are using a Web server that is used by other people. Thus, HelloWorld.java actually goes in a subdirectory called "hall" in the servlets directory.

If you've never used packages before, you'll probably find that the easiest way to compile things is to go to the directory above the one containing your servlets, and then do:

  • "javac directory\ServletClass.java" (Windows; note the backslash)
  • or "javac directory/ServletClass.java" (Unix; note the forward slash).
    So, in this case, I pop up a DOS window (Windows), change directory to the "servlets" directory, and do "javac hall\HelloWorld.java". Note that, on Windows, most JDK 1.1 versions require a backslash for the directory name with javac, even though they don't for "java".

    This is fixed in JDK 1.2, but since many Web servers are configured to use JDK 1.1, many servlet authors stick with JDK 1.1 for portability. For this simple example, you could do javac directly in the package-specific subdirectory ("hall" in this case), but that fails for later examples when the servlet class uses other classes in the same package. Advanced package users will note that you could also set your CLASSPATH to the "servlets" directory, or keep the source code in a location distinct from the .class files, and use javac's "-d" option to install them.

    2.3 Running the Servlet

    With the Java Web Server, servlets are placed in the "servlets" directory and are invoked via http://host/servlet/ServletName. Note that the directory is "servlets", plural, while the URL refers to "servlet", singular. Since this example was placed in the "hall" package, it would be invoked via http://host/servlet/hall.HelloWorld.

    HelloWorld Servlet Output

    NEXT


    This tutorial is now available as a book: Core Servlets and JavaServer Pages by Marty Hall, published by Sun Microsystems Press. Read all about it at CoreServlets.com


    Server-Side Web Applications using Java Servlets versions 2.1/2.2 and JavaServer Pages (JSP) version 1.0: A Tutorial
    © 1999-2000 Marty Hall.
    All source code freely available for unrestricted use.
    Created for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and on-site Java short courses.
    Please note that this is a first draft of the tutorial, so please send corrections, comments, and suggestions to me at hall@apl.jhu.edu.
    Reprinted with permission from the author. Click here to visit the original version

    How to Add Java Applets to Your Site

    New on the Java Boutique:

    New Review:

    Time Management Made Easy with the Quartz Enterprise Job Scheduler
    Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

    New Applet:

    Reverse Complement
    Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

    Elsewhere on internet.com:

    WebDeveloper Java
    Lots of Java information on webdeveloper.com

    WDVL Java
    Thorough Java resource at the Web Developer's Virtual Library.

    ScriptSearch Java
    Hundreds of free Java code files to download.

    jGuru: Your View of the Java Universe
    Customizable portal with online training, FAQs, regular news updates, and tutorials.

  •  Microsoft Visual Studio 2010 Showcase
     Avaya Developer Showcase
     MSDN Spotlight
     PHP for Windows Showcase
    XML error: undefined entity at line 39
    advertisement
    Receive Articles via our XML/RSS feed
    Receive Articles via our XML/RSS feed

    JavaBytes
    Internet Cyclone
    This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

    Windows 7: From Beta to Final Code in One Year
    Google Shows Off Chrome OS, Releases Source
    Microsoft Shows Off Silverlight 4, IE9 Plans
    Metasploit Expands Vulnerability Test Framework
    HyperCard Reborn?
    Fedora 12 Takes Aim at Linux Networking
    Top Supercomputer Nearly Doubles in Speed
    Fedora 12 Linux Tackles Virtualization
    Apple Gives iPhone Developers App Status Tracker
    Novell Sets OpenSUSE 11.2 Free

    Creating Custom Export Filters for StarOffice with XSLT
    WPF Wonders: Using DataTemplates
    Crystal Reports Family Offers Options for Developers
    Avaya Aura Session Manager video
    Avaya Aura Overview video
    Exploring HTML 5's Audio/Video Multimedia Support
    Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
    Understanding the Cloud Computing Security Vulnerabilities
    Cisco and IBM Target a Greener World
    Upgrade to Visual Studio 2010 with the Ultimate Offer

    Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs