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
- 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.
- 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.
- 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.
Next: Other skills for working with servlets >>
More Java Articles
More By Murach Publishing
|
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.
|
|