Erinevus lehekülje "Apache Tomcat" redaktsioonide vahel

Allikas: Kuutõrvaja
(Servlet)
(HelloOracle)
197. rida: 197. rida:
 
       Context initContext = new InitialContext();
 
       Context initContext = new InitialContext();
 
       Context envContext = (Context) initContext.lookup("java:/comp/env");
 
       Context envContext = (Context) initContext.lookup("java:/comp/env");
       DataSource ds = (DataSource)envContext.lookup("jdbc/epria");       
+
       DataSource ds = (DataSource)envContext.lookup("jdbc/loom");       
 
          
 
          
 
     if (envContext == null) throw new Exception("Error: No Context");
 
     if (envContext == null) throw new Exception("Error: No Context");

Redaktsioon: 8. aprill 2010, kell 16:28

Sissejuhatus

Apache Tomcat on praktiliselt iseseisev veebiserver ja servletide käivitaja.

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed by Sun under the Java Community Process.

Install 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

Tomcat5.gif

Andmebaasi 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:orcl"/>       

   </GlobalNamingResources>

Failis conf/context.xml

 <Context reloadable="true">

   <WatchedResource>WEB-INF/web.xml</WatchedResource>
 
   <ResourceLink name="jdbc/dhloracle"
     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

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");
   }
}

HelloOracle

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

Lingid

http://tomcat.apache.org/tomcat-4.1-doc/proxy-howto.html