Java 7 A Comprehensive Tutorial (Pg 620 628).pdf


Preview of PDF document java-7-a-comprehensive-tutorial-pg-620-628.pdf

Page 1 2 3 4 5 6 7 8 9

Text preview


614

Java 7: A Comprehensive Tutorial

Using the Deployment Descriptor
As you can see in the previous examples, writing and deploying a servlet
application is easy. One aspect of deployment deals with mapping your
servlet with a path. In the examples, you used the WebServlet annotation
type to map a servlet with a path. There’s another way of doing this, by
using the deployment descriptor. In this section you’ll learn how to
configure your application using the deployment descriptor.
The app26c application contains two servlets, SimpleServlet and
WelcomeServlet to demonstrate how you can use the deployment
descriptor to map servlets. Listings 26.5 and 26.6 show SimpleServlet and
WelcomeServlet, respectively. Note that the servlet classes are not
annotated @WebServlet.
Listing 26.5: The SimpleServlet class
package app26c;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Copyright © 2014. Brainy Software. All rights reserved.

public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 8946L;

}

@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head></head>" +
"<body>Simple Servlet</body></html");
}

Listing 26.6: The WelcomeServlet class
package app26c;
import java.io.IOException;

Kurniawan, Budi. Java 7 : A Comprehensive Tutorial, Brainy Software, 2014. ProQuest Ebook Central,
http://ebookcentral.proquest.com/lib/valencia-ebooks/detail.action?docID=3003885.
Created from valencia-ebooks on 2018-04-23 08:26:32.