แสดงบทความที่มีป้ายกำกับ tomcat แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ tomcat แสดงบทความทั้งหมด

วันจันทร์, พฤษภาคม 30, 2559

Fixed cannot access Tomcat port 8080 on AWS

Just a quick blog notes after try to deploy a demo spring-boot project on Tomcat 8 on EC2 instance (AWS)


Tomcat homepage inaccessible 


i.e. Host not found ... http://ec2-xx-yy-218-150.ap-southeast-1.compute.amazonaws.com:8080/

After execute Tomcat you might found that it cannot access via the browser; This is might because of your networks did not support IPv6 yet. So, We will do force the Tomcat to operate with IPv4 only.

Solution

- Shutdown the Tomcat

- Edit catalina.sh

Add java options JAVA_OPTS. See the code below:

...
#
#   LOGGING_MANAGER (Optional) Override Tomcat's logging manager
#                   Example (all one line)
#                   LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
# --------------------------------------------------------------------

JAVA_OPTS=" -Djava.net.preferIPv4Stack=true "

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false

darwin=false
...

- Startup Tomcat; It should be accessible now ;)


Command to check if Tomcat running(binding) with IPv6 

$ netstat -ant
...
tcp6       0      0 :::8080                   :::*                    LISTEN
...

After catalina.sh modification 

$ netstat -ant
...
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
...





วันพฤหัสบดี, สิงหาคม 06, 2558

แก้ปัญหา maven dependency: spring-boot 1.2.5 กับ JSTL และ Jasper Reports

แก้ปัญหา maven dependency: spring-boot 1.2.5 กับ jstl 1.2.1 และ jasperreports 6.1

เนื่องจากมีน้องเดินมาให้ช่วยดูให้หน่อยว่าทำไมเค้าใส่ dependency ใน pom ของเค้าแล้วมันมีปัญหา เค้าอยากจะใส่ jstl กับ jasper report เข้าไปใน spring-boot project ของเค้า

บางส่วนของ pom ที่เรียกใช้งาน spring-boot 1.2.5

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
...
<dependencies>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>   
...
</dependencies>
  • dependency ตัวนี้ org.springframework.boot.spring-boot-starter-web จะดึงเอา tomcat embeded version 7 มาให้อยู่แล้ว

JSTL dependency

เริ่มโดยน้องเค้าใส่

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency> 
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId> 
</dependency>
  • JSP 2.3 มากับ Tomcat 8 แต่ spring-boot 1.2.5 ใช้ embedded tomcat 7 classes มันน่าจะไม่ compatible กันละ
  • JSTL จาก group id javax.servlet เป็น version 2.0 ซึ่งหากดูใน web site เค้าก็บอกว่ามันย้าย group id ใหม่แล้ว javax.servlet.jsp.jstl
  • JSTL 2.0 มัน depend กับ jsp-api 2.0 ซึ่งมันก็ขัดกับ jsp-api ที่ระบุเอาไว้ก่อนหน้าอีก

ทีนี้ผมเลยพยายามจะใส่ JSTL 1.2.1 ใหม่ล่าสุดลงไป

ลบ dependency สองตัวข้างบนออกซะ แล้วใส่แบบนี้ลงไปใน pom

<dependencies>
...
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
...
</dependencies>
  • ใส่ org.apache.tomcat.embed.tomcat-embed-jasper เข้ามาด้วยเพราะว่าตัวนี้จะดึงเอา jsp-api ที่เหมาะสมมาให้เราด้วย, ถ้าจะใช่ jstl ต้องมี jsp-api (javax.servlet.jsp) ตัว spring-boot-starter-web เราได้มาแต่ servlet-api (javax.servlet)

พอทดสอบ $ mvn spring-boot:run กลับรันหน้า jsp ของเราไม่ได้ ลองมาเปิด jar ที่อยู่ .m2 ดู ปรากฎว่า jar ที่ได้จาก artifactId jstl มันเป็น jar เน่า มี classes ใน package name ด้วย :( ทำไง ... google ต่อ

คนนี้เค้าบอกวิธีที่ใช้ได้เอาไว้ที่ SO นี้ ขอบคุณครับ ;)

<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.1</version>
    <exclusions>
        <!-- jstl-api was adding selvlet-api 2.5 and jsp-api -->
        <exclusion>
            <artifactId>jstl-api</artifactId>
            <groupId>javax.servlet.jsp.jstl</groupId>
        </exclusion>
    </exclusions>
</dependency>

ต้องไปเอา jstl library จาก groupId ของ glassfish แทน

ต่อมาอยากได้

JasperReport 6.1

จะเอา jasper report ใส่ใน project maven ใส่แบบนี้

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.1.0</version>
</dependency>  

มีปัญหาว่าพอให้ maven update dependency มันหา olap4j library ไม่เจอ ใน ecilpse ฟ้องแบบนี้ Missing artifact org.olap4j:olap4j:jar:0.9.7.309-JS-3

ลองกดดูที่ maven central ตาม URL นี้ org.olap4j » olap4j ก็จะเจอ 404 file not found !! ห๊ะ

[404]

Not Found: /artifact/org.olap4j/olap4j/0.9.7.309-JS-3

งงอยู่ซักแพ๊บ google ต่อ มาเจอ คำตอบ ที่นี่เอง

ใน community ของ jaspersoft ว่าเราจะต้องเพิ่ม URL repository ตัวนี้เข้าไปใน maven's proxy ของเราด้วย http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/

เมื่อไปเพิ่ม URL ข้างต้นใน Artifactory virtual repository แล้วก็ update dependency ใหม่อีกครั้ง ที่นี้ก็ทำงานต่อได้แล้ว

เผอิญว่าที่ทำงาน ผมตั้ง Artifactory server เป็น maven proxy เอาไว้ ก็เลยเจอปัญหานี้ หาก maven ได้วิ่งออก internet ตรงๆ ก็คงไม่เป็นไร

วันจันทร์, กุมภาพันธ์ 14, 2548

HOW-TO configure Oracle Datasource with Tomcat 4.1.x

JNDI Datasource HOW-TO
Database Connection Pool (DBCP) Configurations
DBCP provides support for JDBC 2.0. On systems using a 1.4 JVM DBCP will support JDBC 3.0.
    DBCP uses the Jakarta-Commons Database Connection Pool. It relies on number of Jakarta-Commons componenets:
  • Jakarta-Commons DBCP 1.0
  • Jakarta-Commons Collections 2.0
  • Jakarta-Commons Pool 1.0

These jar files along with your the jar file for your JDBC driver should be installed in $CATALINA_HOME/common/lib.

NOTE:Third Party drivers should be in jarfiles, not zipfiles. Tomcat only adds $CATALINA_HOME/common/lib/*.jar to the classpath.

NOTE: Do not install these jarfiles in your /WEB-INF/lib, or $JAVA_HOME/jre/lib/ext, or anywhere else. You will experience problems if you install them anyplace other than $CATALINA_HOME/common/lib.

Firstly by default, Tomcat will only use *.jar files installed in $CATALINA_HOME/common/lib therefore classes111.zip or classes12.zip will need to be renamed with a .jar extension. Since jarfiles are zipfiles, there is no need to unzip and jar these files - a simple rename will suffice. Also, you should be aware that some (early) versions of Tomcat 4.0 when used with JDK 1.4 will not load classes12.zip unless you unzip the file, remove the javax.sql.* class hierarchy and rejar.


    1. server.xml configuration
    You will need to define your Datasource in your server.xml file. Here we define a Datasource called myoracle using the thin driver to connect as user scott, password tiger to the schema called myschema in the sid called mysid. (Note: with the thin driver this sid is not the same as the tnsname)

    <Resource name="jdbc/myoracle" auth="Container"
    type="javax.sql.DataSource"/>

    <ResourceParams name="jdbc/myoracle">
    <parameter>

    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter>
    <name>driverClassName</name>

    <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>
    <parameter>
    <name>url</name>
    <value>jdbc:oracle:thin:@[db's ip address]:1521:mysid</value>

    </parameter>
    <parameter>
    <name>username</name>
    <value>scott</value>
    </parameter>

    <parameter>
    <name>password</name>
    <value>tiger</value>
    </parameter>
    <parameter>

    <name>maxActive</name>
    <value>20</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>

    <value>10</value>
    </parameter>
    <parameter>
    <name>maxWait</name>
    <value>-1</value>

    </parameter>
    </ResourceParams>

2. web.xml configuration
You should ensure that you respect the element ordering defined by the DTD when you create you applications web.xml file.

<resource-ref>
<description>Oracle Datasource example</description>

<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


3.Code example
You can use the same example application as above (assuming you create the required DB instance, tables etc.) replacing the Datasource code with something like


Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();




..Happy Valentine's Day ..