测试Tomcat解析JSP
# 创建一个JSP的测试页面 [root@DaMoWang tomcat]# vim /data/tomcatweb/1.jsp <html><body><center> Now time is: <%=new java.util.Date()%> </center></body></html> # 查看运行结果 [root@DaMoWang tomcat]# curl -x127.0.0.1:8080 www.damowang.cn/1.jsp <html><body><center> Now time is: Fri Jul 27 18:40:46 CST 2018 </center></body></html> # 可以看到中间那段代码被解析成当前系统时间了 , 也可以在物理机上绑定hosts , 用浏览器来测试
Tomcat连接MySQL
Tomcat连接MySQL是通过JDBC驱动实现的
所以需要准备一个包
mysql-connector-java 可以去官网下载
首先配置mysql , 创建测试用的库、表以及用户
[root@DaMoWang ~]# mysql -uroot -p475541270 mysql> create database java_test; mysql> use java_test mysql> grant all on java_test.* to 'java'@'127.0.0.1' identified by 'damowang'; mysql> create table damowang (`id` int(4), `name` char(40)); mysql> insert into damowang values (1,'abc'); mysql> insert into damowang values (2,'aaa'); mysql> insert into damowang values (3,'ccc'); # 退出mysql 去验证java用户有没有问题 [root@DaMoWang ~]# mysql -ujava -pdamowang -h127.0.0.1 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.7.21 Source distribution Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
MySQL配置完后接着配置Tomcat相关的配置文件
[root@DaMoWang ~]# vim /usr/local/tomcat/conf/context.xml # 在</Context> 上面添加以下内容 <Resource name="jdbc/mytest" # 可以随便定义,要记住名字,之后会用到 auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="java" password="damowang" # 数据库的用户和密码 driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/java_test"> # 数据库的IP、端口以及库名 </Resource> # 编辑完之后还需要改另一个配置文件 [root@DaMoWang ~]# vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml # 在</web-app>上面添加 <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mytest</res-ref-name> # 和之前定义的Resource name保持一致 <res-auth>Container</res-auth> </resource-ref>









