Running the Example Servlets

Tomcat will run as a stand-alone web server, which is very convenient for development. It can also be configured to run as an adjunct to most other web servers to handle only the servlet/JSP requests. That's useful for deployment in real IT environments, and we won't get into it here.

The next step is to try running Tomcat on one of the example servlets that accompany it. First, shut down any other web servers that you have running on your system, so they don't interfere with this example. Then start Tomcat by following the steps in the following box.

Tomcat serves web pages to requests that come through port 8080, so start a browser and give it the URL http://localhost:8080. You can use the name “localhost” or the special IP address “127.0.0.1” that means “this system.” Or if your computer has a DNS name, you can also use that in the URL. You should see the page in Figure 26-1 again.

Figure 26-1. Home page of Tomcat on your system

image

Click on the “Servlet Examples” link displayed at the bottom left of the page, and you will bring up a new page that has the top half-a-dozen examples of servlet-related things you want to do, along with a skeleton of the source code that implements them. Click on the link marked “Hello World execute”. You will see the browser change to a dynamic page reading “Hello World”. This demonstrates that you can successfully run the example servlets on your system. After you have read the basic example here, you can return to this URL and find sample code for the following:

  • Getting the HTTP headers of this request

  • Reading the parameters passed with an HTTP GET request

  • Setting and retrieving a cookie

  • Creating and examining session information

We cover the first two of these in this chapter. Cookies and sessions should be studied with the online Tomcat examples.

We'll now cover the material that you need to understand how servlets work. All this information might look somewhat involved, but writing servlets and JSP is easily done. One of the links reachable from the Tomcat home page is the Servlet API documentation at http://127.0.0.1:8080/tomcat-docs/servletapi/index.html

It's a good idea to keep a browser tab open on that page, and review each class as it is mentioned in the rest of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181