Java
  Home arrow Java arrow Developing Your Servlet Skills
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 
Sun Developer Network 
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

Developing Your Servlet Skills
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2006-09-21

    Table of Contents:
  • Developing Your Servlet Skills
  • Other skills for working with servlets
  • How to code instance variables
  • How to code thread-safe servlets

  • 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


    Developing Your Servlet Skills


    (Page 1 of 4 )

    Last week, you learned how to create, save, and compile a servlet. This week, you will learn more about working with servlets, such as how to code thread-save servlets. This article, the second of three parts, is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).

    How to request a servlet

    Now that you can code, save, and compile a servlet, you’re ready to test the servlet by viewing it in a web browser. To do that, the web server and the servlet engine must be running. Then, you can request the servlet as shown in figure 5-5.

    To test a servlet, you can enter a URL directly in a web browser. However, once you’re done testing a servlet, you’ll usually want to code a web page that requests the servlet. You can do that by coding a Form tag or an Anchor tag that specifies a URL that requests the servlet.

    To request a servlet by entering the URL into a browser, you enter an absolute URL like the two examples in this figure. If you’ve read the last two chapters, you shouldn’t have much trouble understanding how this works. The main difference is that you must enter “servlet” after the document root directory to indicate that you want to run a servlet. Then, you enter the package name for the servlet, followed by a period (or dot), followed by the name of the servlet.

    The first example shows the URL for a servlet that’s stored on a local web server in the email5 directory of the murach directory. The second example shows the URL for the servlet after it’s deployed on the Internet server for www.murach.com.

    When you test a servlet, you will often want to pass parameters to it. To do that, you can add the parameters to the end of the URL. Here, the question mark after the servlet name indicates that one or more parameters will follow. Then, you can code the parameter name, the equals sign, and the parameter value for each parameter that is passed, and you can separate multiple parameters with ampersands (&).

    If you omit a parameter that’s required by the servlet, the getParameter method will return a null value for that parameter. In this figure, for example, only the first parameter value is added to the end of the URL. As a result, two of the values displayed by the servlet are null values.

    To request a servlet from an HTML form, you use the Action attribute of the form to provide a path and filename that points to the servlet. This is illustrated by the last two examples in this figure. Here, the assumption is that the HTML page is in a subdirectory of the document root directory. As a result, the path specified in the Action attribute begins with two periods to navigate back one level to the root directory. Then, the word “servlet” specifies that you want to call a servlet. Last, the package and class names point to the class for the servlet.

    When you use an HTML form to request a servlet, you can use the Method attribute to specify whether you want to use the Get method or the Post method. If you use a Get method to request a servlet from another page, any parameters that are passed to the servlet will be displayed in the browser at the end of the URL. But if you use the Post method to request a servlet, the parameters won’t be displayed at the end of the URL.

    How to request the EmailServlet class


    Figure 5-5.  How to request a servlet

    The syntax for requesting a servlet

      http://host:port/documentRoot/servlet/ packageName.ServletName

    Two URLS that request a servlet

      http://localhost:8080/murach/ servlet/email5.EmailServlet
      http://www.murach.com/servlet/ email5.EmailServlet
     

    How to add parameters to a URL

      EmailServlet?firstName=John&lastName=Smith 
      EmailServlet?firstName=John&lastName= Smith&emailAddress=jsmith@hotmail.com

    Two Form tags that request a servlet

      <form action="../servlet/email5.EmailServlet" method="get">
      <form action="../servlet/email5.EmailServlet" method="post">

    Description

    1. To request a servlet without using an HTML form, enter a URL that requests the servlet in the browser. If necessary, add parameters to the end of the URL.
    2. When you code or enter a URL that requests a servlet, you can add a parameter list to it starting with a question mark and with no intervening spaces. Then, each parameter consists of its name, an equals sign, and its value. To code multiple parameters, use ampersands (&) to separate them.
    3. If an HTML form that uses the Get method requests a servlet, the URL and its parameters will be displayed by the browser. If an HTML form that uses the Post method requests a servlet, the URL will be displayed in the browser, but the parameters won’t be.

    More Java Articles
    More By Murach Publishing


       · 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-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT