# Assignment #8a

## Setting up your own tomcat Server on mislab

mislab runs the apache web server software. apache is open-source software, available for free from www.apache.org. Over 60% of the web servers on the internet run the apache web server software.

apache does not support Java servlets by itself. To provide that support, mislab runs the tomcat server software (this is also open-source, free software). For class purposes, each student will run his or her own tomcat server. The following instructions explain how to get your tomcat server up and running.

1\. First, check your grades for this class, and look for your Assigned Tomcat Port Numbers. You will have two assigned port numbers: a primary port number, and a secondary port number. Write these numbers down for later use.

2\. Next, you need to edit one of your system startup files on your mislab account. “Hidden” files are not shown when you get a normal directory listing, such as with the command “ls -l”. To show “all” files, use the command:

`ls -al`

Telnet to your account, and get a listing of all files in your root directory (ls -al). You should see several files whose filenames begin with a period. These are hidden files; they are not normally shown when you get a directory listing (unless you include the “a” switch, of course). You need to edit the file named .profile.

`pico .profile`

Your .profile file contains commands that you want to have executed as part of your login process. For tomcat programming, you need to set the values of four environment variables. You can do this with the export command.

You should see the following four lines in your .profile:

```shell
export JAVA_HOME=/usr/local/jdk
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=/home/bis3523/abc123/tomcat
export CLASSPATH=.:
/usr/local/tomcat/common/lib/servlet.jar:
/usr/local/jdk/bin/mysql.jar
```

Modify the third of these lines to substitute your netid for abc123. Also make sure that your entire path specification is correct (make sure it is /home/bis3523, for instance).

The commands that you type into your .profile are exactly like commands that you would type at the command line. At the command line, you could not press &lt;Enter&gt; in the middle of a command. Similarly, you cannot split any of these commands across multiple lines.

Be sure to check your .profile for accuracy. Each command must be on a single line.

These environment variables specify (1) the location of mislab’s Java installation, (2) mislab’s tomcat (catalina) installation, (3) your tomcat directory, and (4) locations (directories or files) to be searched for Java classes.

3\. You can create your own aliases for commands in your .profile file. For instance, if you want to type "dir" instead of "ls –l", you can include the following line in your .profile:

```shell
alias dir="ls –l"
```

For your convenience, an alias to start your tomcat server, one to shutdown your tomcat server, and one to show all running tomcat servers have been created in your .profile file.

```shell
alias starttom="/usr/local/tomcat/bin/startup.sh"
alias stoptom="/usr/local/tomcat/bin/shutdown.sh"
alias showtom="ps -f -C java"
```

You will be able to type starttom to start your tomcat server, stoptom to shut it down, and showtom to show all running tomcat servers.

<span class="qowt-font4-CourierNew" id="E2189">4</span><span class="qowt-font4-CourierNew" id="E2190">. Press Control-X to exit pico, saving your file.</span>

<span class="qowt-font4-CourierNew" id="E2193">5</span><span class="qowt-font4-CourierNew" id="E2194">. Log out of your account, then log back in. The lines that you just inserted into your .profile file will be processed when you log back in (and every time you log in in the future).</span>

<span class="qowt-font4-CourierNew" id="E2197">Any time that you edit your .profile, you need to log out, then log back in. The commands in your .profile are processed as part of the login process.</span>

<span class="qowt-font4-CourierNew" id="E2200">6</span><span class="qowt-font4-CourierNew" id="E2201">. Under your root directory, make a subdirectory named tomcat:</span>

`<span class="qowt-font4-CourierNew" id="E2204"> mkdir tomcat</span>`

<span class="qowt-font4-CourierNew" id="E2207">7</span><span class="qowt-font4-CourierNew" id="E2208">. Change to your tomcat subdirectory:</span>

`<span class="qowt-font4-CourierNew" id="E2211"> cd tomcat</span>`

<span class="qowt-font4-CourierNew" id="E2214">8</span><span class="qowt-font4-CourierNew" id="E2215">. Copy a</span><span class="qowt-font4-CourierNew" id="E2216">ll of the tomcat files from mislab's</span><span class="qowt-font4-CourierNew" id="E2217"> tomcat directory to your own tomcat subdirectory:</span>

`<span class="qowt-font4-CourierNew" id="E2220"> cp -R /</span><span class="qowt-font4-CourierNew" id="E2221">usr/local</span><span class="qowt-font4-CourierNew" id="E2222">/tomcat/* .</span>`

<span class="qowt-font4-CourierNew" id="E2225"> (The -R switch indicates that all subdirectories should be copied, recursively. The period at the end specifies that the current directory is the destination to which files are to be copied.)</span>

<span class="qowt-font4-CourierNew" id="E2228">9</span><span class="qowt-font4-CourierNew" id="E2229">. Get a direct</span><span class="qowt-font4-CourierNew" id="E2230">ory listing. You should see several</span><span class="qowt-font4-CourierNew" id="E2231"> subdirectories</span><span class="qowt-font4-CourierNew" id="E2232">, including</span><span class="qowt-font4-CourierNew" id="E2233">: conf, logs, webapps, and work. Change to your conf subdirectory.</span>

`<span class="qowt-font4-CourierNew" id="E2236"> cd conf</span>`

<span class="qowt-font4-CourierNew" id="E2239">10</span><span class="qowt-font4-CourierNew" id="E2240">. The file server.xml is used to configure your tomcat server. You need to edit this file to specify your assigned port numbers.</span>

`<span class="qowt-font4-CourierNew" id="E2243"> pico server.xml</span>`

<span class="qowt-font4-CourierNew" id="E2246"> </span><span class="qowt-font4-CourierNew" id="E2247">(</span><span class="qowt-font4-CourierNew" id="E2248">If you do not want to use pico, you could ftp server.xml down to your local computer, edit the file, then ftp it back to your mislab account.)</span>

<span class="qowt-font4-CourierNew" id="E2251">On line 3, change `port=8085`</span><span class="qowt-font4-CourierNew" id="E2252"> to use your assigned secondary port number. (You can press Control-C to see the current line number</span><span class="qowt-font4-CourierNew" id="E2253"> in pico</span><span class="qowt-font4-CourierNew" id="E2254">.)</span>

<span class="qowt-font4-CourierNew" id="E2256"> On line 15, change `port=8080`</span><span class="qowt-font4-CourierNew" id="E2257"> to use your assigned primary port number.</span>

<span class="qowt-font4-CourierNew" id="E2260">11</span><span class="qowt-font4-CourierNew" id="E2261">. Exit pico, saving your edited file.</span>

<span class="qowt-font4-CourierNew" id="E2264">12</span><span class="qowt-font4-CourierNew" id="E2265">. Start your tomcat server:</span>

`<span class="qowt-font4-CourierNew" id="E2268"> starttom</span>`

<span class="qowt-font4-CourierNew" id="E2271"> You should get a message indicating the directories that your server is using. These should correspond to the environment variables that you set in your .profile.</span>

<span class="qowt-font4-CourierNew" id="E2274">13. Check to see if your tomcat server is running:</span>

`<span class="qowt-font4-CourierNew" id="E2277"> showtom</span>`

```qowt-stl-Normal
conf> showtom
UID PID PPID C STIME TTY TIME CMD
rpearson 24611 1 13 11:00 pts/2 00:00:04 /usr/local/jdk/bin/java -Djava.u


```

<span class="qowt-font4-CourierNew" id="E2280"> You should see something like:</span>

<span class="qowt-font4-CourierNew" id="E2294"> (Note: There could be multiple tomcat servers running, since each student will run his or her own server.)</span>

<span class="qowt-font4-CourierNew" id="E2297">14</span><span class="qowt-font4-CourierNew" id="E2298">. Get into your web browser and try to access your server. Go to the following URL:</span>

<span class="qowt-font4-CourierNew" id="E2301"> http://mis</span><span class="qowt-font4-CourierNew" id="E2302">lab.business</span><span class="qowt-font4-CourierNew" id="E2303">.msstate.edu:</span><span class="qowt-font4-CourierNew" id="E2304">8080</span>

<span class="qowt-font4-CourierNew" id="E2307"> (Replace the </span><span class="qowt-font4-CourierNew" id="E2308">808</span><span class="qowt-font4-CourierNew" id="E2309">0</span><span class="qowt-font4-CourierNew" id="E2310"> with your assigned primary port number.)</span>

<span class="qowt-font4-CourierNew" id="E2313"> This should load the index.jsp file that is stored in your tomcat/webapps/ROOT directory. (You may have to try this more that once, since it takes a few moments for your tomcat server to actually get up and running.</span><span class="qowt-font4-CourierNew" id="E2314"> Its first response to a request may be slow, but subsequent responses will be fast.</span><span class="qowt-font4-CourierNew" id="E2315">)</span>

<span class="qowt-font4-CourierNew" id="E2319">15</span><span class="qowt-font4-CourierNew" id="E2320">. You should have the file c</span><span class="qowt-font4-CourierNew" id="E2321">ounter.jsp in your tomcat/webapps/ROOT subdirectory. The .jsp filename extension indicates to the tomcat server that this is a java server page, meaning that it includes jsp code. Given your knowledge of </span><span class="qowt-font4-CourierNew" id="E2322">Java, you can probably look at c</span><span class="qowt-font4-CourierNew" id="E2323">ounter.jsp and determine to some extent what is going on in the page. When your tomcat server gets a request for this file, it (the server) translates the .jsp file into an actual java servlet. To see this in action, telnet to your account, an</span><span class="qowt-font4-CourierNew" id="E2324">d move to tomcat/work/Catalina</span><span class="qowt-font4-CourierNew" id="E2325">/localhost</span><span class="qowt-font4-CourierNew" id="E2326">/ROOT/org/apach/jsp</span><span class="qowt-font4-CourierNew" id="E2327">. You will proba</span><span class="qowt-font4-CourierNew" id="E2328">bly have two files in that directory: index\_jsp.java and index\_jsp.class</span><span class="qowt-font4-CourierNew" id="E2329">.</span><span class="qowt-font4-CourierNew" id="E2330"> These two files were created when you accessed your index.jsp page.</span>

<span class="qowt-font4-CourierNew" id="E2333">16</span><span class="qowt-font4-CourierNew" id="E2334">. In your browser, go to:</span>

<span class="qowt-font4-CourierNew" id="E2337"> http://mislab.</span><span class="qowt-font4-CourierNew" id="E2338">business</span><span class="qowt-font4-CourierNew" id="E2339">.msstate.edu:</span><span class="qowt-font4-CourierNew" id="E2340">808</span><span class="qowt-font4-CourierNew" id="E2341">0</span><span class="qowt-font4-CourierNew" id="E2342">/c</span><span class="qowt-font4-CourierNew" id="E2343">ounter.jsp</span>

<span class="qowt-font4-CourierNew" id="E2346"> (Replace the </span><span class="qowt-font4-CourierNew" id="E2347">808</span><span class="qowt-font4-CourierNew" id="E2348">0</span><span class="qowt-font4-CourierNew" id="E2349"> with your assigned primary port number.)</span>

<span class="qowt-font4-CourierNew" id="E2352"> You should see a page which displays the current date and time, followed by an access counter.</span>

<span class="qowt-font4-CourierNew" id="E2355">17</span><span class="qowt-font4-CourierNew" id="E2356">. Now go back to your telnet session and get a new directory listing. You will see that your tomcat server translated you</span><span class="qowt-font4-CourierNew" id="E2357">r .jsp file into a .java file (c</span><span class="qowt-font4-CourierNew" id="E2358">ounter\_jsp.java), then compiled that into a .class file. Your .class file generated the page that you see in the browser!</span>

<span class="qowt-font4-CourierNew" id="E2361">18</span><span class="qowt-font4-CourierNew" id="E2362">. The first time you access c</span><span class="qowt-font4-CourierNew" id="E2363">ounter.jsp with your browser, there will be a significant delay, because the .jsp file has to be translated into .java, then the .java file has to be compiled into byte code. On subsequent requests for the page, these steps can be skipped, so the desired page will load much more quickly.</span>

<span class="qowt-font4-CourierNew" id="E2366"> Note that any time that you shut down your server, or any time that you edit the .jsp file, Java code generation and compilation will need to occur again. One noticeable effect of this is that the next request for the page will, once again, be slow. Another noticeable effect is that your counter will be reinitialized. This counter will count correctly, as long as you don’t shut down your tomcat server and don’t edit your .jsp file.</span>

<span class="qowt-font4-CourierNew" id="E2370">19</span><span class="qowt-font4-CourierNew" id="E2371">. If you want to do some quick experimentation, you can put your own .html files in your tomcat/webapps/ROOT directory and access them via your own assigned tomcat primary port number.</span>

<span class="qowt-font4-CourierNew" id="E2374">20</span><span class="qowt-font4-CourierNew" id="E2375">. Now that you know that you can process a .jsp correctly, test a servlet. We want to make sure that you can compile a servlet, and that you can then run it from a web page.</span>

<span class="qowt-font4-CourierNew" id="E2378"> You should have the following two files:</span>

`<span class="qowt-font4-CourierNew" id="E2381"> tomcat/webapps/ROOT/s</span><span class="qowt-font4-CourierNew" id="E2382">nooperServlet.html</span>`

`<span class="qowt-font4-CourierNew" id="E2384"> tomcat/webapps/ROOT/WEB-INF/classes/SnooperServlet.java</span>`

<span class="qowt-font4-CourierNew" id="E2387"> Edit SnoooperServlet.html and change the default port number of </span><span class="qowt-font4-CourierNew" id="E2388">8080</span><span class="qowt-font4-CourierNew" id="E2389"> to </span><span class="qowt-font4-CourierNew" id="E2390">your primary tomcat port number (this is in the href attribute of an anc</span><span class="qowt-font4-CourierNew" id="E2391">hor tag). </span>

<span class="qowt-font4-CourierNew" id="E2394">Also change the </span><span class="qowt-font4-CourierNew" id="E2395">abc123</span><span class="qowt-font4-CourierNew" id="E2396"> that you find in that attribute to your net id.</span>

<span class="qowt-font4-CourierNew" id="E2400"> Move to your "classes directory". If you are currently in your home directory, you can use one, or several, cd commands to move to your classes directory, such as:</span>

`<span class="qowt-font4-CourierNew" id="E2403"> cd tomcat/webapps/ROOT/WEB-INF/classes</span>`

<span class="qowt-font4-CourierNew" id="E2406"> or</span>

`<span class="qowt-font4-CourierNew" id="E2409"> cd tomcat</span>`

`<span class="qowt-font4-CourierNew" id="E2411"> cd webapps</span>`

`<span class="qowt-font4-CourierNew" id="E2413"> cd ROOT</span>`

`<span class="qowt-font4-CourierNew" id="E2415"> cd WEB-INF</span>`

`<span class="qowt-font4-CourierNew" id="E2417"> cd classes</span>`

<span class="qowt-font4-CourierNew" id="E2420">21</span><span class="qowt-font4-CourierNew" id="E2421">. Get a directory listing, to be sure that you really have SnooperServlet.java.</span>

`<span class="qowt-font4-CourierNew" id="E2424"> ls -l</span>`

<span class="qowt-font4-CourierNew" id="E2428">22</span><span class="qowt-font4-CourierNew" id="E2429">. Compile SnooperServlet.java.</span>

`<span class="qowt-font4-CourierNew" id="E2432"> javac SnooperServlet.java</span>`

<span class="qowt-font4-CourierNew" id="E2435"> If you get an error message such as:</span>

`<span class="qowt-font4-CourierNew" id="E2438"> -bash: javac: command not found</span>`

<span class="qowt-font4-CourierNew" id="E2441"> then you have an error in the Path environment variable in your .profile. Since your .profile was pre-configured for you, you should not get this error message. If you have accidentally messed up your path, however, you could get this error. Your .profile should contain the following line:</span>

`<span class="qowt-font4-CourierNew" id="E2444"> PATH=$PATH:$HOME/bin:/usr/sbin:/usr/local/jdk/bin</span>`

<span class="qowt-font4-CourierNew" id="E2447"> Since java is in /usr/local/jdk/bin on mislab, this value should point the operating system to the correct file.</span>

<span class="qowt-font4-CourierNew" id="E2450"> If you get an error message such as:</span>

`<span class="qowt-font4-CourierNew" id="E2453"> SnooperServlet.java:10: package javax.servlet does not exist</span>`

<span class="qowt-font4-CourierNew" id="E2455"> or</span>

`<span class="qowt-font4-CourierNew" id="E2457"> SnooperServlet.java:13: cannot resolve symbol</span>`

<span class="qowt-font4-CourierNew" id="E2460"> then you probably have an error in you</span><span class="qowt-font4-CourierNew" id="E2461">r classpath (also set in your .</span><span class="qowt-font4-CourierNew" id="E2462">profil</span><span class="qowt-font4-CourierNew" id="E2463">e</span><span class="qowt-font4-CourierNew" id="E2464">, but you typed this one).</span>

<span class="qowt-font4-CourierNew" id="E2467">23</span><span class="qowt-font4-CourierNew" id="E2468">. Once you have worked the bugs out of your environment, you will successfully compile SnooperServlet.java. Get a directory listing, and make sure that you have a SnooperServlet.class.</span>

<span class="qowt-font4-CourierNew" id="E2471">24</span><span class="qowt-font4-CourierNew" id="E2472">. In your web browser, go to:</span>

<span class="qowt-font4-CourierNew" id="E2475"> http://mislab.</span><span class="qowt-font4-CourierNew" id="E2476">business</span><span class="qowt-font4-CourierNew" id="E2477">.msstate.edu:</span><span class="qowt-font4-CourierNew" id="E2478">808</span><span class="qowt-font4-CourierNew" id="E2479">0</span><span class="qowt-font4-CourierNew" id="E2480">/s</span><span class="qowt-font4-CourierNew" id="E2481">nooperServlet.html</span>

<span class="qowt-font4-CourierNew" id="E2484"> </span><span class="qowt-font4-CourierNew" id="E2485">C</span><span class="qowt-font4-CourierNew" id="E2486">lick the </span><span class="qowt-font4-CourierNew" id="E2487">anchor link</span><span class="qowt-font4-CourierNew" id="E2488">. If you see a display of a lot of information, formatted in a table, you are doing great!</span>

<span class="qowt-font4-CourierNew" id="E2491">25</span><span class="qowt-font4-CourierNew" id="E2492">. When you are finished, you can shut down your tomcat server, or you can leave it running to wait for requests.</span>

`<span class="qowt-font4-CourierNew" id="E2495"> stoptom</span>`