Apache Tomcat
Sisukord
Sissejuhatus
Apache Tomcat http://tomcat.apache.org/ on java rakendusserver, mis sisaldab sellist funktsionaalsust
- java servlettide kasutamine
- java server pages (JSP) kasutamine
- ajp ja http protokolli tugi
2010 aasta kevadel on tarkvara viimane stabiilne versioon 6.x.
Keskkonna ettevalmistamine
- Tomcat kasutamiseks peab olema süsteemis Java
- Tomcat töötab tavaliselt mitteprivilegeeritud kasutaja:grupina tomcat:tomcat
Tarkvara paigaldamine
Apache Tomcat paigaldamiseks on tavaliselt
- operatsioonisüsteemi paketihaldusest või portsudest
- kasutatakse aadressilt http://tomcat.apache.org/ kopeeritud kompileeritud tarkvara
Debian GNU/Linux
TODO
FreeBSD
Installime tomcat serveri
cd /usr/ports/www/jakarta-tomcat5 make install clean
Mille järel antakse kasutaja umbes järgnev info
Installation settings: Destination directory: /usr/local/jakarta-tomcat5.0 Location of JDK: /usr/local/diablo-jdk1.6.0 Location of Java port: java/diablo-jdk16 Running as (user/group): www/www HTTP port: 8180 Shutdown listener port: 8005 AJP 1.3 connector port: 8009 Logfile stdout: /usr/local/jakarta-tomcat5.0/logs/stdout.log Logfile stderr: /usr/local/jakarta-tomcat5.0/logs/stderr.log
Ning seejärel palutakse tirida seoses litsentsitingimustega java ning paigutada see /usr/ports/distfiles
Because of licensing restrictions, you must fetch the distribution manually. Please access http://www.FreeBSDFoundation.org/cgi-bin/download?download=diablo-caffe-freebsd7-amd64-1.6.0_07-b02.tar.bz2 with a web browser and "Accept" the End User License Agreement for "Caffe Diablo 1.6.0". Please open http://java.sun.com/javase/downloads/index.jsp in a web browser and follow the "Download" link for "JDK US DST Timezone Update Tool - 1_3_15" to obtain the time zone update file, tzupdater-1_3_15-2009g.zip. Please place the downloaded file(s) in /usr/ports/distfiles.
Apache paigaldus
cd /usr/ports/www/apache2 make with_PROXY_MODULES=yes install clean
Seadistus
avame /etc kaustas rc.conf ja lisame
apache2_enable="YES" tomcat50_enable="YES"
Stardime java serveri
/usr/local/etc/rc.d/tomcat50 start
seadistame nüüd apache. Läheme lihtsamat teed ja kasutame mod_proxy't mod_jk asemel.
Loome virtualhost httpd.conf'i
<VirtualHost *:80> ServerName www.mingine.ee ProxyPass / http://localhost:8180/ ProxyPassReverse / http://localhost:8180/ </VirtualHost>
selleks ,et wad failide lisamine toimiks tuli minul vähemalt kasutada veel käsku
chown -R www:www /usr/local/jakarta-tomcat5.0/webapps/
Seame ka veebi administraatorile parooli
avame kaustas /usr/local/jakarta-tomcat5.0/conf faili tomcat-users.xml
ja muudame selle sisu selliseks
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <user username="kasutaja" password="salasona341" roles="manager"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="role1" password="tomcat" roles="role1"/> </tomcat-users>
avades nüüd veebi
www.mingine.ee
tervitab meid jakarta avaleht kus saame manager alla logida enda kasutaja ja parooliga ja lisada wad'e ehk java koodi konteinereid.
Muide serveri sisse lisades uploadimine miskipärast ei töötanud, soovides lisada faile mis asuvad samas serveris tuleb kirjutada selle täisrada kujul file:/kaust/rakendus.wad ja deployda
Servlet
HelloWorld
Tomcatiga kaasa tuleva servleti näidise kompileerimiseks sobib öelda nt
$ cd /usr/local/tomcat/webapps/examples/WEB-INF/classes $ javac -cp /usr/local/tomcat/lib/servlet-api.jar HelloWorldExample.java
kus
$ cat HelloWorldExample.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings",request.getLocale()); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Hello World"); } }
Andmebaasi pooli seadistamine servletist kasutmiseks
HelloOracle pooli seadistamine
Failis conf/server.xml
<GlobalNamingResources> ... <Resource name="jdbc/yhendusenimi" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" username="orclkasutajanimi" password="parool" maxActive="20" maxIdle="10" removeAbandoned="true" maxWait="-1" url="jdbc:oracle:thin:@192.168.10.53:1521:loom.loomaaed"/> </GlobalNamingResources>
kus
- Oracle RAC puhul sobib kasutada urli väärtusena
url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = \ (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.13.66)(PORT = 1521)) \ (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.13.67)(PORT = 1521)) \ (LOAD_BALANCE = yes)) \ (CONNECT_DATA = (SERVICE_NAME = loom.loomaaed) \ (FAILOVER_MODE = (TYPE = SELECT)(METHOD = BASIC))))"/>
Failis conf/context.xml
<Context reloadable="true"> <WatchedResource>WEB-INF/web.xml</WatchedResource> <ResourceLink name="jdbc/loom" type="javax.sql.DataSource" global="jdbc/yhendusenimi"/> </Context>
Lisaks peab olema sobib jdbc teek Tomcati teekide kataloogis, nt
/usr/local/tomcat-5.5/lib/ojdbc14.jar
HelloOracle näide
import javax.naming.Context; import javax.naming.InitialContext; import java.sql.*; import java.sql.DriverManager; import java.sql.SQLException; import java.net.*; import javax.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldExample extends HttpServlet { String message = "Not Connected"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings",request.getLocale()); response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection conn = null; ResultSet rst = null; Statement stmt = null; try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource ds = (DataSource)envContext.lookup("jdbc/loom"); if (envContext == null) throw new Exception("Error: No Context"); if (ds == null) throw new Exception("Error: No DataSource"); if (ds != null) conn = ds.getConnection(); if (conn != null) { message = "Got Connection " + conn.toString() + ", "; stmt = conn.createStatement(); rst = stmt.executeQuery("select BANNER from SYS.V_$VERSION"); } if (rst.next()) message = rst.getString(1); rst.close(); rst = null; stmt.close(); stmt = null; conn.close(); // Return to connection pool conn = null; // Make sure we don't close it twice } catch (Exception e) { e.printStackTrace(); } finally { // Always make sure result sets and statements are closed, // and the connection is returned to the pool if (rst != null) { try { rst.close(); } catch (SQLException e) {;} rst = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException e) {;} stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) {;} conn = null; } } out.println(message); } }
Kopileerimiseks
$ javac -cp /usr/local/tomcat/lib/servlet-api.jar:/usr/local/tomcat/lib/ojdbc14.jar HelloWorldExample.java
Käivitamiseks küsida brauseriga nt
http://loomaaed.tartu.ee:8080/examples/servlets/servlet/HelloWorldExample
Kasulikud materjalid
HelloPostgreSQL pooli seadistamine
Failis conf/server.xml
<GlobalNamingResources> ... <Resource name="jdbc/yhendusenimi" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" username="postgreskasutajanimi" password="parool" maxActive="20" maxIdle="10" removeAbandoned="true" maxWait="-1" url="jdbc:postgresql://192.168.1.247/baasinimi"/> </GlobalNamingResources>
kus
Failis conf/context.xml
<Context reloadable="true"> <WatchedResource>WEB-INF/web.xml</WatchedResource> <ResourceLink name="jdbc/loom" type="javax.sql.DataSource" global="jdbc/yhendusenimi"/> </Context>
Lisaks peab olema sobiv jdbc teek Tomcati teekide kataloogis, nt
/usr/local/tomcat-5.5/lib/postgresql-8.4-701.jdbc4.jar
HelloPostgreSQL programmi näide
Samasugne nagu Oracle puhul, ainult select on nt
rst = stmt.executeQuery("select version();");
Kopileerimiseks
$ javac -cp /usr/local/tomcat/lib/servlet-api.jar:/usr/local/tomcat/lib/postgresql-8.4-701.jdbc4.jar HelloWorldExample.java
Rakenduste paigaldamine
Üks viis rakedusi paigaldada (ingl. k. deploy) on kopeerida vastav arhiiv, nt .war fail webapps kataloogi ning töötab Tomcati protsess pakib arhiivi lahti ja rakendus muutub automaatselt kasutatavaks.
Tomcat kasutamine Apache veebiserveriga
TODO
URI teisendamine
Üks võimalus korraldada nii, et brauseris pöördutaks / poole ja Tomcatis /rakendus poole on kasutada RewriteRule'i
JkMount /rakendus* ajp13_worker_teisendatud RewriteEngine On RewriteRule ^/rakendus - [L] RewriteRule ^/(.*) /rakendus/$1 [PT]
Päringute logimine
Tomcat seadistusfaili conf/server.xml Engine sektsioonis sobib kasutada nt sellist konstruktsiooni
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".log" pattern="common" resolveHosts="false"/>
Tulemusena ilmub logi faili logs/localhost_access_log.2010-10-18.log.