- 基于PV作为交换目录将应用最终拷贝入/tomcat/webapps目录
- 进入Docker后,修改/bin/catalina.sh,加入jdbc的类
set CLASSPATH=E:apache-tomcat-9.0.1webappsmytestsqlWEB-INFlibmysql-connector-java-5.1.44-bin.jar
- 应用代码修改,将MYSQL_SERVICE_HOST和MYSQL_SERVICE_PORT环境变量获取
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page import="java.sql.*" %> <html> <head> <title>My JSP starting page</title> </head> <body> <h1> <% try { Class.forName("com.mysql.jdbc.Driver"); String mysql_host=System.getenv("MYSQL_SERVICE_HOST"); String mysql_port=System.getenv("MYSQL_SERVICE_PORT"); Connection con=DriverManager.getConnection("jdbc:mysql://"+mysql_host+":"+mysql_port+"/mydb","root","welcome1"); Statement state=con.createStatement(); String sql2="select * from student"; ResultSet rs=state.executeQuery(sql2); while(rs.next()){ String uID=rs.getString("StuID"); String uName=rs.getString("stuName"); out.println("stuID:"+uID+" "+"stuName:"+uName+"<br>"); } out.println("Operator success..."+"<br>"); state.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } %> </h1> </body> </html>
- 在创建时把环境变量带上
env: - name: MYSQL_SERVICE_HOST value: 'mysql' - name: MYSQL_SERVICE_PORT value: '3306'
验证时可在tomcat Pod创建后进入容器运行
printenv