The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Servlets
Lesson: Running Servlets

Calling Servlets from an HTML Page

To invoke a browser from within an HTML page just use the servlet URL in the appropriate HTML tag. (This section assumes knowledge of HTML.) Tags that take URLs include those that begin anchors and forms, and a meta tags.

This section uses the Duke's Bookstore ShowCart, Cashier, and Receipt servlets. Luckily this is the order that the servlets are displayed when you look at your cart and buy your books.

For the most direct access to the ShowCart servlet, click the Show Cart link from the Duke's Bookstore main page. If you have servletrunner or a web server set up to run the example, go to the main page of the bookstore as shown in the previous section. Just for fun, you might want to add a book to your cart before accessing the ShowCart servlet.
 

Example Servlet URLs in HTML Tags

The page returned by the ShowCartServlet has a number of anchors, each of which has a servlet as a destination. The following shows the code for one of those anchors:

    public class ShowCartServlet extends HttpServlet { 

        public void doGet (HttpServletRequest request,
                           HttpServletResponse response)
	    throws ServletException, IOException
        {
                ...
                out.println(... +
                            "<a href=\"" +
                            response.encodeUrl("/servlet/cashier") +
                            "\">Check Out</a>      " +
                            ...);
                ...
        }
        ...
    }
This code results in an HTML page that has the following anchor:
 
    <a href="http://localhost:8080/servlet/cashier>Check Out</a>

If the showcart servlet's page is displayed in your browser, you can see the anchor if you view the source of the page. Then click on the link. The cashier servlet will return the page that contains the next example.

The page displayed by the cashier servlet presents a form that requests user's name and credit card number. The code that prints out the form tag looks like this:
 

    public class CashierServlet extends HttpServlet { 

        public void doGet (HttpServletRequest request,
                           HttpServletResponse response)
	    throws ServletException, IOException
        {
            ...
            out.println(... +
                        "<form action=\"" +
                        response.encodeUrl("/servlet/receipt") +
                        "\" method=\"post\">" +
                        ...
                        "<td><input type=\"text\" name=\"cardname\"" +
                        "value=\"Gwen Canigetit\" size=\"19\"></td>" +
                        ...
                        "<td><input type=\"submit\"" +
                        "value=\"Submit Information\"></td>" +
                        ...
                        "</form>" +
                        ...);
            out.close();
        }
        ...
    }
This code results in an HTML page that has the following tag to begin the form:
 
    <form action="http://localhost:8080/servlet/receipt" method="post">

If the cashier servlet's page is displayed in your browser, you can see the tag that begins the form if you view the source of the page. Then submit the form. The receipt servlet will return the page that contains the next example. The receipt servlet's page resets itself though, so if you want to view the page's HTML source, do it fast!.

The page returned by the receipt servlet has a meta tag that uses a servlet URL as part of the value of the http-equiv attribute. Specifically, the tag directs the page to reset to the main page of Duke's Bookstore after thanking the user for the order. The following shows the code for this tag:

    public class ReceiptServlet extends HttpServlet { 

        public void doPost(HttpServletRequest request,
                           HttpServletResponse response)
	    throws ServletException, IOException
        {
            ...
            out.println("<html>" +
                        "<head><title> Receipt </title>" +
                        "<meta http-equiv=\"refresh\" content=\"4; url=" +
                        "http://" + request.getHeader("Host") +
                        "/servlet/bookstore;\">" +
                        "</head>" +
            ...
        }
        ...
    }

This code results in an HTML page that has the following tag:
 
    <meta http-equiv="refresh"
        content="4; url=http://localhost:8080/servlet/bookstore;">


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search