Getting the Tomcat Web Server

To develop chat.jsp on your machine, you can download the reference web server for servlets and JSP, the Apache Tomcat server, which you can get from http://jakarta.apache.org/tomcat/ for free. To follow along, download the binary EXE file for your system and run it to install Tomcat.

After installing Tomcat, just follow the directions that come with it to start Tomcat. For example, if you're using Windows, the current version (Tomcat 5.5) installs itself as a Windows service. To start Tomcat, select the Start, Programs, Apache 5.5, Configure Tomcat item and then click the Start button to start Tomcat (or the Stop button to stop it).

To test whether Tomcat is working after you start it, start a browser and navigate to http://localhost:8080, the main page for local installations of Tomcat. You can see the results in Figure 5.2.

Figure 5.2. The Tomcat main page.


The localhost part is the name of the web server, and “localhost” is the name reserved for web servers running on your own machine. The 8080 part is the port number. Each web server uses a port number to keep it separate from other web servers. Web servers usually use port 80, but Tomcat uses port 8080 so it won't interfere with other web servers.

Here's the directory structure that installing Tomcat creates:

Tomcat 5.5
|__bin              Binary executable files
|__common           Classes available to internal classes and Web applications:
|__conf             Configuration files (such as passwords)
|__logs             The server's log files
|__server           Internal Tomcat classes
|__shared           Shared files
|__temp             Temporary files
|__webapps          Directory for Web applications
|__work             Scratch directory for holding temporary files

Note in particular the webapps directory, which is where you put JSPs (and servlets) to make them accessible to your browser. For instance, the files from this chapter will go into the chat directory, so add this new directory to the webapps directory now:

webapps
|__chat                      Holds chat.html and chat.jsp

To satisfy Tomcat, any directory containing web applications must also contain a directory named WEB-INF, with two subdirectories, classes and lib. WEB-INF, classes, and lib may all be empty—as they will be here—but Tomcat will expect them to be there before running your application:

webapps
|__chat                       Holds chat.html and chat.jsp
    |__WEB-INF                Information about Web applications
         |__classes           Java classes used Web applications
         |__lib               JAR files used by Web applications

Create those directories, WEB-INF, classes, and lib now. They'll be empty, but Tomcat needs to see those directories before it will serve any HTML or JSP pages for you.

To get started, store this short demo JSP in a file named ch05_01.jsp in the webappschat directory now:

<HTML>
  <HEAD>
    <TITLE>Using JSP</TITLE>
  </HEAD>

  <BODY>
    <% out.println("This was printed using JSP."); %>
  </BODY>
</HTML>

This JSP uses Java to send the text “This was printed using JSP.” to the browser. The out object is prepared for you by the JSP framework and is ready for use, much as the System.out object is ready for use in standalone Java programs.

Make sure this file is saved as webappschatch05_01.jsp, start Tomcat, and browse to the URL http://localhost:8080/chat/ch05_01.jsp, as shown in Figure 5.3. If things work as expected, you should see in your browser what's shown in Figure 5.3.

Figure 5.3. A first JSP page.


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

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