linux配置Tomcat
Tomcat就是用來解析jsp程式的一個軟體,因為Tomcat技術先進、效能穩定,而且免費,因而深受Java愛好者的喜愛並得到了部分軟體開發商的認可,成為目前比較流行的Web 應用伺服器。
Tomcat是一個輕量級應用伺服器,在中小型系統和併發訪問使用者不是很多的場合下被普遍使用,是開發和除錯JSP程式的首選。對於一個初學者來說,可以這樣認為,當在一臺機器上配置好Apache伺服器,可利用它響應對HTML 頁面的訪問請求。
一、安裝tomcat
Tomcat的安裝分為兩個步驟:安裝JDK和安裝Tomcat.
JDK(Java Development Kit)是Sun Microsystems針對Java開發員的產品。自從Java推出以來,JDK已經成為使用最廣泛的Java SDK. JDK是整個Java的核心,包括了Java執行環境,Java工具和Java基礎的類庫。所以要想執行jsp的程式必須要有JDK的支援,理所當然安裝Tomcat的前提是安裝好JDK.
安裝JDK
下載jdk1.8安裝包上傳到/usr/local/src目錄
cd /usr/local/src/
tar zxvf jdk-8u191-linux-x64.tar.gz
mv jdk1.8.0_191/ /usr/local/jdk1.8
配置jdk
[[email protected] jdk1.8]# vi /etc/profile
JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
export JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH
檢測成功顯示
[[email protected] jdk1.8]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
安裝Tomcat
下載tomcat7安裝包放入/usr/local/src中
tar zxvf apache-tomcat-7.0.47.tar.gz
ll
mv apache-tomcat-7.0.47 /usr/local/tomcat
ll
/usr/local/tomcat/bin/startup.sh
ps aux |grep tomcat
測試
瀏覽器輸入ip:8080,顯示如下·
二、配置Tomcat
包括配置Tomcat的訪問埠,虛擬主機,解析jsp
配置Tomcat的訪問埠
tomcat預設啟動的是8080,如果你想修改為80,則需要修改server.xml檔案:
vim /usr/local/tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
重啟生效
[[email protected] src]# /usr/local/tomcat/bin/shutdown.sh
[[email protected] src]# /usr/local/tomcat/bin/startup.sh
配置Tomcat的虛擬主機
cd /usr/local/tomcat/conf/
vim server.xml
找到</Host>下一行插入新的<Host>內容如下:
<Host name="www.123.cn" appBase="/data/tomcatweb"
unpackWARs="false" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="./" debug="0" reloadable="true" crossContext="true"/>
</Host>
儲存後,重啟生效
測試Tomcat解析jsp
先建立tomcat的測試檔案:
vim /data/tomcatweb/111.jsp
加入如下內容:
<html><body><center>
Now time is: <%=new java.util.Date()%>
</center></body></html>
儲存後,使用curl測試:
[[email protected] ~]# curl -xlocalhost:80 www.123.cn/111.jsp
看看執行結果是否是:
<html><body><center>
Now time is: Thu Jun 13 15:26:03 CST 2013
</center></body></html>
三、Tomcat連線MySql
建立測試的資料庫和使用者
[[email protected] logs]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> create database java_test;
Query OK, 1 row affected (0.00 sec)
mysql> use java_test
Database changed
mysql> grant all on java_test.* to 'tom'@'127.0.0.1' identified by 'cat';
Query OK, 0 rows affected (0.00 sec)
mysql> create table user (`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into user value (1,'mingming');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user value (2,'huahua');
Query OK, 1 row affected (0.00 sec)
下載 mysql-connector-java
將mysql-connector-java-5.1.25.tar.gz解壓並放在/usr/local/下: cd mysql-connector-java-5.1.25
cp mysql-connector-java-5.1.25-bin.jar /usr/local/tomcat/lib
重啟tomcat cd /usr/local/tomcat/bin ./shutdown.sh ./startup.sh
配置Tomcat相關檔案
cd /usr/local/tomcat/webappsmkdir test touch index.jsp vim index.jsp編輯如下內容:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.naming.*" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java_test","root","123456");
Statement state=con.createStatement();
String sql="select * from user";
ResultSet rs=state.executeQuery(sql);
while(rs.next()){
String id=rs.getString("id");
String name=rs.getString("name");
out.println("id:"+id+" "+"name:"+name+"<br>");
}
out.println("success..."+"<br>");
state.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>