首页 Linux正文

搭建Tomcat环境实现jsp通过Tomcat连接MySQL

王昊 Linux 2019-08-01 238 0

实验所需软件:

cd /usr/local/

Jdk:  http://www.oracle.com/technetwork/java/javase/downloads/index.html

Tomcat:
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.43/bin/apache-tomcat-8.5.43.tar.gz                

MySQL-Connector-Java:
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz

MySQL:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz

实验思路:

要在apache环境下运行jsp就需要一个解释器来执行jsp网页,而这个jsp解释器就是Tomcat,那为什么还要JDK呢?因为jsp要连接数据库的话,就要jdk来提供数据库的驱动程序,所以要运行jsp的web服务器平台就要Apache+Tomcat+JDK

环境准备

#设置或停止防火墙:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
#关闭selinux:
临时关闭:
[root@localhost ~]# setenforce 0
永久关闭(修改配置文件):
[root@localhost ~]# vim /etc/selinux/config
改成SELINUX=disabled

安装apache服务请参考前面的文章:

编译安装Apache 2.4

安装JDK

注意:安装之前需要查看下系统是否安装了openjdk,如果安装了openjdk,请先卸载,否则安装不了oracle官方的jdk

rpm -qa | grep openjdk

卸载方法:

yum remove java-* -y

解压

tar xf jdk-8u91-linux-x64.tar.gz

mv jdk1.8.0_91/ /usr/local/jdk

配置JDK需要的环境变量

vim /etc/profile

JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

使配置立即生效

source /etc/profile

查看版本

java -version

java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Tomcat

tar xf apache-tomcat-8.5.43.tar.gz

mv apache-tomcat-8.5.43/ /usr/local/tomcat

cd tomcat/bin/

tar xf commons-daemon-native.tar.gz

cd commons-daemon-1.1.0-native-src/unix/

./configure –with-java=/usr/local/jdk

make

编译完成之后会生成一个jsvc文件

将jsvc文件拷贝至/tomcat/bin目录下

cp jsvc /usr/local/tomcat/bin/

配置daemon.sh

cd /usr/local/tomcat/bin/

vim daemon.sh

# chkconfig:2345 10 90

JAVA_HOME=/usr/local/jdk
CATALINA_HOME=/usr/local/tomcat

配置用户与权限

useradd tomcat -M -s /sbin/nologin

chown -R tomcat /usr/local/tomcat/

chmod +x /usr/local/tomcat/bin/*.sh

启动服务

/usr/local/tomcat/bin/daemon.sh start

浏览器访问:服务器IP:8080

优化执行路径

ln -s /usr/local/tomcat/bin/daemon.sh /etc/init.d/tomcat

chmod +x /etc/init.d/tomcat

可以用以下命令进行服务的停止与启动了

/etc/init.d/tomcat stop/start

将Tomcat 加入到service管理器

chkconfig –add tomcat

设置tomcat开机启动

chkconfig tomcat on

安装MySQL

这里安装过程就不做了,直接看我另一篇文章就可以了:

CMake方式安装MySQL 5.7

创建测试数据

mysql> create database tomcat;

mysql> use tomcat

创建测试表

mysql> create table tt(id int,name varchar(128));   

创建测试数据

mysql> insert into tt values(1,"hey man"),(2,"hey woman");

授权用户

mysql> grant all on tomcat.* to tomcat@localhost identified by 'tomcat';

测试tomcat是否可以登陆

mysql -u tomcat -ptomcat

安装mysql-connector

wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz

tar xf mysql-connector-java-5.1.48.tar.gz

只需要复制到 tomcat 的 lib 目录下,重启 tomcat 就可以生效

cd /usr/local/mysql-connector-java-5.1.48

cp mysql-connector-java-5.1.48-bin.jar /usr/local/tomcat/lib/

重启 tomcat

service tomcat stop; service tomcat start

测试jsp链接mysql测试

建立测试界面

vim /usr/local/tomcat/webapps/ROOT/mysql.jsp

<%@ page import="java.sql.*"%>
<html>
<body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/tomcat?user=tomcat&password=tomcat&useUnicode=true&characterEncoding=utf-8";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
);
String sql="select * from tt";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){%>
step:<%=rs.getString(1)%>
context:<%=rs.getString(2)%><br><br>
<%}%>
<%out.print("Congratulations!!! JSP connect MYSQL IS OK!!");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

浏览器访问:服务器IP:8080/mysql.jsp

页面出现以下内容表明测试成功

step:1 context:hey man

step:2 context:hey woman

Congratulations!!! JSP connect MYSQL IS OK!!  
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论