Java
  Home arrow Java arrow Page 4 - How to Develop Servlets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

How to Develop Servlets
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-09-14

    Table of Contents:
  • How to Develop Servlets
  • The code for the EmailServlet class
  • How to create a servlet
  • How to save and compile a servlet

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Develop Servlets - How to save and compile a servlet


    (Page 4 of 4 )

    In chapter 4, you learned how to save and compile regular Java classes in Tomcat. Now, figure 5-4 shows you how to save and compile servlet classes.

    Since the EmailServlet class contains a package statement that specifies a package named email5, it must be placed in the email5 subdirectory of the WEB-INF\classes directory that is subordinate to the document root directory. For the book applications, this root directory is webapps\murach.

    This figure also presents four other directory paths that you can use to store your servlet classes. All of these paths store the servlet classes in the WEB-INF\classes directory that’s subordinate to the document root directory. That can be your own document root directory or the default document root directory (ROOT) that’s provided by Tomcat 4.0. Then, if you use packages, you continue the directory structure with one directory for each package level. If, for example, you use a class named EmailServlet in a package named business.email, the directory path for the servlet must be WEB-INF\classes\business\email.

    If you’re using TextPad to save and compile your servlets, you will need to set your classpath so it includes the directory that contains your class files as described in appendix A. Then, TextPad’s Compile Java command will work properly.

    Otherwise, you can use a DOS prompt to compile your packaged classes. To do that, you use the cd command to change the current directory to the WEB-INF\classes directory for your application. Then, you use the javac command to compile the class. When you do that, you can enter the javac command, followed by a space, followed by the package name, followed by a backslash (\), followed by the name of the Java class.

    Where the EmailServlet class is saved

      c:\tomcat\webapps\murach\WEB-INF\classes\email5

    Other places to save your servlet classes

      c:\tomcat\webapps\yourDocumentRoot\ WEB-INF\classes\packageName  
      c:\tomcat\webapps\yourDocumentRoot\ WEB-INF\classes\ 
      c:\tomcat\webapps\ROOT\WEB-INF\classes\
      c:\tomcat\webapps\ROOT\WEB-INF\classes\packageName

    The TextPad dialog box for saving Java files


    Figure 5-4.  How to save and compile a servlet

    The DOS prompt window for compiling the EmailServlet class

    Description

    • Although you can save the source code (.java file) for a servlet in any directory, you must save the compiled class (.class file) in the \WEB-INF\classes subdirectory of the document root directory or in a subdirectory that corresponds to the Java package that the class is in.
    • For simplicity, you can store your servlet source files in the same directory as the corresponding class files, but the source files for a production application are likely to be stored in other directories.
    • To compile your servlets, you can use TextPad’s Compile Java command, your IDE’s compile command, or the javac command from a DOS prompt window.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Murach's Java Servlets and JSP," published...
     

    Buy this book now. This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek