Java Reference
In-Depth Information
The output should look similar to the following:
ID == 33445
DESCRIPTION == Austin Powers: International Man of Mystery
PRICE == 19.95
QUANTITY == 56
Using XML in a Servlet
Now let's take the previous example and incorporate it into a servlet. The only difference is
that the XML file that you are parsing has been hard coded into the servlet; therefore, you will
need to replace the <SERVER_ROOT> with the location of your installed Tomcat server. The bet-
ter route would be to use an init parameter, but I want to keep the example as simple as possi-
ble. Listing 10.4 contains our servlet example.
L ISTING 10.4
XMLServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.Hashtable;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import SAXHandler;
public class XMLServlet extends HttpServlet {
private static final String CONTENT_TYPE = “text/html”;
private String moviefile = “<SERVER_ROOT>\\djs\\item.xml”;
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
10
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
 
Search WWH ::




Custom Search