■ WAS(Tomcat) 시작할때 DB 접속을 해서 작업을 해야 할 경우에 Listener를 통해서 작업을 할 수 있다. web.xml에 ServletContextListener를 구현한 listener를 등록하면 Tomcat이 시작할 때마다 listener가 실행된다.
■ maven pom.xml, mysql driver dependency 및 tomcat plugin을 추가한다.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.iotinfra</groupId> <artifactId>pilot.simpletomcat</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.5</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>9900</port> <path>/</path> <contextReloadable>true</contextReloadable> <systemProperties> <JAVA_OPTS>-Xms512m -Xmx512m --XX:MaxPermSize=128m</JAVA_OPTS> </systemProperties> </configuration> </plugin> </plugins> </build> </project>
■ web.xml( ServletContextListener를 구현한 pilot.simpletomcat.StartupListener를 등록한다.)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>simpletomcat</display-name> <listener> <listener-class>pilot.simpletomcat.StartupListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
■ 테스트용 mysql테이블 ‘login_info’ 테이블을 생성한다.
create table `nextdb`.`login_info` ( `pid` int(10) not null auto_increment primary key, `id` varchar(10), `count` int(10) ) engine=InnoDB default character set = utf8;
■ db 접속 정보를 담은 config-properties.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>database configuration</comment> <!-- db정보 --> <entry key="jdbc.driverClass.master">com.mysql.jdbc.Driver</entry> <entry key="jdbc.url.master">jdbc:mysql://127.0.0.1:3306/nextdb</entry> <entry key="jdbc.username.master">nextman</entry> <entry key="jdbc.password.master">pass</entry> </properties>
■ config-properties.xml파일을 읽어서 DB접속정보를 담을 DbInfoVO.java
package pilot.simpletomcat; public class DbInfoVO { private String url; private String driverClass; private String user; private String password; ... }
■ config-properties.xml파일을 읽어서 DbInfoVO 객체에 정보를 담는 기능을 하는 DbInfoUtil.java
package pilot.simpletomcat; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DbInfoUtil { private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); private DocumentBuilder builder; private Document document; public DbInfoUtil() { try { StringBuffer sb = new StringBuffer(); sb.append(System.getProperty("user.dir")); sb.append(File.separator + "config"+File.separator +"config-properties.xml"); System.out.println( ">>> path : " + sb.toString() ); factory.setValidating(false); try { factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); builder = factory.newDocumentBuilder(); document = builder.parse(new File(sb.toString())); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } document.getDocumentElement().normalize(); } catch( Exception e ) { e.printStackTrace(); } } public DbInfoVO getDbInfo() { DbInfoVO dbInfo = new DbInfoVO(); Element element = null; Node node = null; NodeList nodeList = document.getElementsByTagName("entry"); for( int i=0; i<nodeList.getLength(); i++ ) { node = nodeList.item(i); element = (Element)node; System.out.println( "" + element.getAttribute("key") +"\t"+ element.getTextContent() ); if( element.getAttribute("key").equals("jdbc.driverClass.master") ) dbInfo.setDriverClass( element.getTextContent() ); if( element.getAttribute("key").equals("jdbc.url.master") ) dbInfo.setUrl( element.getTextContent() ); if( element.getAttribute("key").equals("jdbc.username.master") ) dbInfo.setUser( element.getTextContent() ); if( element.getAttribute("key").equals("jdbc.password.master") ) dbInfo.setPassword( element.getTextContent() ); } return dbInfo; } }
■ Tomcat서버가 시작될 때 실행될 StartupListener.java
package pilot.simpletomcat; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class StartupListener implements ServletContextListener { private Connection conn = null; public void contextInitialized(ServletContextEvent sce) { DbInfoVO dbInfo = (new DbInfoUtil()).getDbInfo(); openDB(dbInfo);// connection open doWork(); // db 작업 closeDB();// connection close } private void doWork() { if (conn != null) { PreparedStatement pstmt = null; try { pstmt = (PreparedStatement) conn.prepareStatement("DELETE FROM LOGIN_INFO WHERE ID = 'nextman'"); pstmt.executeUpdate(); pstmt = (PreparedStatement) conn.prepareStatement("INSERT INTO LOGIN_INFO (ID, COUNT) VALUES ( 'nextman',0)"); pstmt.executeUpdate(); pstmt = (PreparedStatement) conn.prepareStatement("UPDATE LOGIN_INFO SET COUNT = 1 WHERE ID = 'nextman'"); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } private void openDB(DbInfoVO dbInfo) { try { try { java.sql.Driver mySQLDriver = (Driver) Class.forName(dbInfo.getDriverClass()).newInstance(); DriverManager.registerDriver(mySQLDriver); } catch (Exception e) { e.printStackTrace(); } conn = DriverManager.getConnection(dbInfo.getUrl(), dbInfo.getUser(), dbInfo.getPassword()); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private void closeDB() { try { if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public void contextDestroyed(ServletContextEvent sce) {} }
■ Eclipse에서 본 파일 구성
- Java에서 properties파일 읽기 2019년 4월 3일
- weblogic.jdbc.wrapper.clob_oracle_sql_clob cannot be cast to oracle.sql.clob 2019년 3월 21일
- Java keytool 사용법 2019년 3월 14일
- Tomcat : consider increasing the maximum size of the cache 2019년 3월 4일
- Tomcat Startup timeout error in Eclipse 2018년 5월 24일
- Tomcat 시작시 ServletContextListener를 통한 mySQL 접속 2018년 5월 8일
- spring mybatis mysql 연결 예제 2018년 5월 4일
- apache commons StringUtils 예제 2018년 5월 3일
- 자바 래퍼 클래스(Java Wrapper Class)? 2018년 2월 13일
- java string 변수 switch, junit test 2018년 2월 12일
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 2017년 12월 4일
- java continue, break 키워드 2017년 3월 25일
- maven filtering – 메이븐 필터링 2016년 10월 24일
- maven profiles – 개발/운영 환경별 빌드 2016년 10월 24일
- maven pom.xml에서 로그 출력하기 2016년 5월 30일
- ServletContextLister를 구현한 웹 애플리케이션 시작 시 작업 2016년 5월 2일
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 2016년 5월 2일
- JUnit Controller Test java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 2016년 3월 22일
- Maven skipTests – Maven JUnit 테스트 안하기 2016년 3월 18일
- maven compile option -Xlint:deprecation 2016년 3월 17일
- Maven encoding 2016년 3월 17일
- TIBCO ActiveSpace Eclipse Maven 2016년 3월 16일
- Jetty Maven Plugin 2016년 3월 9일
- java.lang.UnsupportedClassVersionError: org/eclipse/jetty/maven/plugin/JettyRunMojo : Unsupported major.minor version 52.0 2016년 3월 9일
- Java String, 자바 문자열 2016년 3월 3일
- tomcat-maven-plugin을 통한 maven tomcat배포 2016년 2월 23일
- tomcat java.lang.OutOfMemoryError: PermGen space 2016년 2월 23일
- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet 2016년 2월 19일
- Tomcat https 서비스(SSL, keystore) 2015년 7월 26일
- eclipse maven 디버그(debug) 모드로 빌드 2014년 11월 21일
- No Spring WebApplicationInitializer types detected on classpath 에러 2014년 11월 21일
- org.apache.ibatis.binding.BindingException 디버깅 2014년 11월 11일
- maven-war-plugin webXml, 개발 환경별 web.xml 관리 2014년 11월 10일
- 명령행 모드 메이븐 프로젝트 생성하기 2014년 9월 17일
- eclipse maven tomcat 자동 재시작 2014년 9월 13일