Beruflich Dokumente
Kultur Dokumente
Advanced Search Log In | Not a Member? ADC Home > Internet & Web > Java > Support
Thats it. Tomcat is now installed under /usr/local/jakarta-tomcat-4.0.1/ or a similar directory if you installed a different version. Please note that you will need to change the filenames and paths in the examples in this tutorial if your version or installation directory are different from mine. Now youll probably want to change ownership of all the Tomcat files and directories to someone other than the administrator. Since I use the login liz on my system, Im changing ownership of all files under jakarta-tomcat4.0.1/ to liz.
root@localhost:local> chown -R liz:staff /usr/local/jakarta-tomcat-4.0.1
Now you can create a file (using your favorite text editor, must be saved with unix line endings) called~/bin/start_tomcat with the following contents:
#!/bin/sh export CATALINA_HOME=/usr/local/jakarta-tomcat-4.0.1 export JAVA_HOME=/usr $CATALINA_HOME/bin/startup.sh
Now you can visit http://localhost:8080/ in your web browser to make sure things worked.
If you are unable to connect to Tomcat, you can check for errors by examining your error log:
liz@localhost:~> cat /usr/local/jakarta-tomcat-4.0.1/logs/catalina.out
in server.xml (and there are many), see your local Tomcat documentation at http://localhost:8080/tomcatdocs/config/index.html or go to http://jakarta.apache.org/tomcat/tomcat-4.0-doc/config/index.html. For now, however, you can follow the instructions in the section below to get up and running.
<Context path="/mine" docBase="mine" debug="0" reloadable="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_mine_log." suffix=".txt" timestamp="true"/> </Context>
<!-- note that I've added this new context entry right above the pre-existing "Examples" context -->
Now that youve added a new context, youll want to create the appropriate directories. For a more complete guide to the standard directory layout defined in the 2.2 Servlet Specification, see your local docs at http://localhost:8080/tomcat-docs/appdev/deployment.html or seehttp://jakarta.apache.org/tomcat/tomcat4.0-doc/appdev/deployment.html. For now, however, Ill guide you through a minimal installation that conforms to the standard. Create the following directories:
liz@localhost:~> cd /usr/local/jakarta-tomcat-4.0.1/webapps liz@localhost:webapps> mkdir mine liz@localhost:webapps> mkdir mine/WEB-INF liz@localhost:webapps> mkdir mine/WEB-INF/classes liz@localhost:webapps> mkdir mine/WEB-INF/lib
Now you need to create a new configuration file for your application. You can do this by copying an example configuration file into your new directory and making a few changes.
liz@localhost:webapps> cp ./tomcat-docs/appdev/web.xml.txt ./mine/WEB-INF/web.xml
Next youll make a few changes to your new web.xml file. First, set the display-name, description, and webmasterparameters to appropriate values for your application.
<display-name>My Web Application</display-name> <description> Examples by Me </description>
[...]
<context-param> <param-name>webmaster</param-name> <param-value>your@email.address</param-value> <description> The EMAIL address of the administrator to whom questions and comments about this application should be addressed. </description> </context-param>
Finally, add a Servlet Definition for your first servlet, along with a mapping so that the servlet can be viewed via a short URL.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter();
out.println("<html>"); out.println("<head>"); out.println("<title>Hola</title>"); out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("<h1> Hi </h1>"); out.println("</body>"); out.println("</html>"); } }
Frameworks/JavaVM.framework/Versions/1.3/Classes/classes.jar:/usr/local/ jakarta-tomcat-4.0.1/common/lib/servlet.jar"
This command will work if youre using the tcsh shell (if youre not sure which youre using, its probably tcsh):
liz@localhost:classes> setenv CLASSPATH "/System/Library/ Frameworks/JavaVM.framework/Versions/1.3/Classes/classes.jar:/usr/local/ jakarta-tomcat-4.0.1/common/lib/servlet.jar"
Now you should be ready to compile the servlet. You will also need to restart Tomcat so that your new web application will be recognized. If you are new to Unix-based environments, please note that javac is like many Unix commands in that it exits silently on success. In other words, if you dont see any error messages, your .java file has been compiled successfully. Ive added some ls commands to the listing below which you can use if youd like to see that your .class file was created.
liz@localhost:classes> ls Hi.java liz@localhost:classes> javac Hi.java liz@localhost:classes> ls Hi.class Hi.java liz@localhost:classes> ~/bin/stop_tomcat liz@localhost:classes> ~/bin/start_tomcat
Now that Tomcat has restarted, you should be ready to load the servlet in your browser.
</servlet-mapping>
Now you can create a file called classes/mail.java that looks like this. Once youve created the file, you can compile it using the javac command. If it wont compile, dont worry. You may need to add another library to your CLASSPATH variable with one of the following commands (depending on your current login shell). For sh or bash, heres the command:
liz@mail:classes> export CLASSPATH="$CLASSPATH:/usr/local/jakarta-tomcat-4.0.1/common/lib/mail.jar"
Now when you visit http://localhost:8080/mine/mail, you should see your new servlet in action:
Conclusion
In this tutorial youve learned how to install and run the Jakarta Tomcat servlet container on your Mac OS X system, run some example servlets, and deploy your own web applications. In Part II youll see how you can use JavaServer Pages (JSP), and the Jakarta Projects JSP Tag Libraries (Taglibs). Part II will also include an overview of Java database programming on Mac OS X using JDBC and MySQL.
More Information