Friday, 8 July 2016

Tomcat

Installation Steps
  • Install java 1.7
  • Download and extract Apache-Tomcat zip to any loacation 
  • Define JAVA_HOME environment variable to java 1.7
  • Download ojdbc5.jar to Apache-Tomcat\lib folder 
  • Place any supporting java classes in WEB-INF\classes directory 
  • run startup.bat file 
  • Invoke jsp from browser
To access admin console of tomcat:
  •  Go to conf folder-> tomcat-users.xml
  • Add role "manager-gui" role using following xml tag
        <role rolename="manager-gui"/>
  • add user with the "manager-gui" role using following xml tag
         <user username="admin" password="admin" roles="manager-gui"/>
  • Remove unwanted users from the list     
  • Restart server 
Connection Pool in TomCat: 
  • Download jdbc driver jar file
    • Place it in WEB-INF/lib directory
  • Define connection pool in WebContent/META-INF/context.xml
  • Define resource reference in WEB-INF/web.xml
    • <resource-ref>
          <description>BugDB Connection Pooling</description>
          <res-ref-name>jdbc/bugap_db </res-ref-name>
          <res-type> javax.sql.DataSource</res-type>
          <res-auth> Container</res-auth>
      </resource-ref>
  • Get connection pool reference in java code
  1.  Using Java Naming and Directory Interface
    public Connection getConnection() { 
    Connection connection = null;  
        try {
            Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
            connection = ds.getConnection();
        }
        catch (Exception e) {
            System.out.println("Connection error: " + e.getMessage());  
        }
        return connection;
    }
  2. Resourcing injection with servlets

    @Resource(name="jdbc/bugap_db")
    private DataSource dataSource;

No comments:

Post a Comment