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

Trail: Servlets
Lesson: Calling 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(outside of the tutorial).) Tags that take URLs include those that begin anchors and forms, and a meta tags.

This section uses the Duke's Bookstore ShowCartServlet, CashierServlet, and ReceiptServlet. 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 ShowCartServlet, click the Show Cart link from the Duke's Bookstore main page. If you have a servlet engine 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 ShowCartServlet(in a .java source file).
 

Example Servlet URLs in HTML Tags

The page returned by 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("/bookstore/cashier") +
                            "\">Check Out</a>      " +
                            ...);
                ...
        }
        ...
    }
This code results in an HTML page that has the following anchor:
 
    <a href="/bookstore/cashier">Check Out</a>

If the show cart 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 CashierServlet(in a .java source file) 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("/bookstore/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="/bookstore/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 ReceiptServlet(in a .java source file) 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") +
                        "/bookstore/enter;\">" +
                        "</head>" +
            ...
        }
        ...
    }

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


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

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.