Originally Published: Tuesday, 2 October 2001 Author: Henry Chen
Published to: enhance_articles_sysadmin/Sysadmin Page: 5/6 - [Printable]

Serving Java from Linux

Ever want a server on your box that can serve JSP and Java Servlets but don't want to pay big money for a commercial solution? Then Linux.com has the article for you. Follow author Henry Chen into the land of the sun.

  << Page 5 of 6  >>

Testing and Configuration

Before we start modifying anything, we should first find out whether what we have done so far actually works.  To test this, start the Apache web server:

/etc/init.d/httpd start

Then, open your browser and point it to http://yourhostname/examples/  The default Apache/Mod_jk/Tomcat setup created this example directory.  You should see links to the jsp and the servelets directories.  Inside these directories, there are test JSP scripts and Java Servelets that you can run to make sure that Tomcat and Apache are actually working together.

The following will test whether you can specify where the Java related files are. 

Create a test JSP script with vi /var/www/html/test.jsp (This script is based on a script in the Tomcat Package.)

<%
String vPath = request.getRequestURI();
String rPath = getServletConfig().getServletContext().getRealPath(vPath);
%>

<html>
<head>
<title>
jsp test
</title>
</head>

<body>
<p>The virtual path is <%=vPath%></p>
<p>The real path is <%=rPath%></p>
</body>
</html>

The Apache web server already knows about /var/www/html through httpd.conf  You have to manually configure Tomcat to do the same.  Insert the following line in the Tomcat configuration file vi /usr/tomcat/conf/server.xml - scroll to the end and insert these lines right before </ContextManager>:

<Host name="yourhostname">
<Context path=""
         docBase="/var/www/html"
         crossContext="false"
         debug="0"
         reloadable="true" >
</Context>
</Host>

Then, restart Tomcat:

/etc/init.d/tomcat restart

To test if this works, point your browser to http://yourhostname/test.jsp  You can also use the above as a template to add Java support to other web sites on this server.  For example, if you use NameVirtualHost have the following in your httpd.conf:

<VirtualHost IPAddress>
    ServerName hostname
    DocumentRoot directoryname
</VirtualHost>

Then, you also need to add Tomcat:

<Host name="hostname">
<Context path=""
         docBase="directoryname"
         crossContext="false"
         debug="0"
         reloadable="true" >
</Context>
</Host>

Since you add new information to the configuration files of both Apache and Tomcat, you will have to restart both.  





  << Page 5 of 6  >>