-------本文大纲
简介
Jsp环境部署
Tomcat目录结构
SHOP++网上商城系统安装
---------------
一、简介
JSP
JSP(Java Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件(*.jsp)。简单地说,jsp就是可能包含了java程序段的html文件(由jasper将嵌入在html文档的程序整个的转换为serlet代码),为了和普通的html区别,因此使用jsp后缀名。
Servlet
Servlet是一种独立于平台和协议的服务器端的Java应用程序,可以生成动态的Web页面。 它担当Web浏览器或其他HTTP客户程序发出请求,与HTTP服务器上的数据库或应用程序之间的中间层。
Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。
在通信量大的服务器上,Java servlet的优点在于它们的执行速度更快于CGI程序。各个用户请求被激活成单个程序中的一个线程,而创建单独的程序,这意味着各个请求的系统开销比较小。
简单地说,servlet就是在服务器端被执行的java程序,它可以处理用户的请求,并对这些请求做出响应。Servlet编程是纯粹的java编程,而jsp则是html和java编程的中庸形式,所有的jsp文件都将被最终转换成java servlet来执行。
从jsp到java到class,jsp在首次被请求时是要花费一定的服务器资源的。但庆幸的是,这种情况只发生一次,一旦这个jsp文件被翻译并编译成对应的servlet,在下次请求来临时,将直接由servlet来处理,除非这个jsp已经被修改。
JSP和Servlet的最大区别在于,Servlet通常需要事先编译好,而JSP则并非必须事先编译。这意味着Servlet通常放置于私有资源区域,而JSP则通常以嵌入代码的方式包含于HTML页面文件中,这些HTML文件通常放置在公开资源区域。
Tomcat
Tomcat是一个免费的开源的Serlvet容器,它是Apache基金会的Jakarta项目中的一个核心项目,由Apache,Sun和其它一些公司及个人共同开发而成。
Tomcat是稳固的独立的Web服务器与Servlet Container,不过,其Web服务器的功能则不如许多更健全的Web服务器完整,如Apache(Apache是一个强大的Web服务器 ,在处理静态页面、处理大量网络客户请求、支持服务的种类以及可配置方面都有优势,高速并且强壮,但是没有JSP/Servlet的解析能力)
将Apache和Tomcat整合起来可以看作是用Tomcat做Apache的jsp/servlet解析插件,不过Tomcat作为一个Web服务器,本身只具备了基本的Web服务功能。
二、Jsp环境部署
1、安装JDK(jdk-7u55-linux-x64)
[root@essun download]# ls apache-tomcat-7.0.53.tar.gz jdk-7u55-linux-x64.rpm [root@essun download]# rpm -ivh jdk-7u55-linux-x64.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... [root@essun download]#
安装后存放的路径
[root@essun java]# rpm -ql jdk | tail -10 /usr/java/jdk1.7.0_55/man/man1/schemagen.1 /usr/java/jdk1.7.0_55/man/man1/serialver.1 /usr/java/jdk1.7.0_55/man/man1/servertool.1 /usr/java/jdk1.7.0_55/man/man1/tnameserv.1 /usr/java/jdk1.7.0_55/man/man1/unpack200.1 /usr/java/jdk1.7.0_55/man/man1/wsgen.1 /usr/java/jdk1.7.0_55/man/man1/wsimport.1 /usr/java/jdk1.7.0_55/man/man1/xjc.1 /usr/java/jdk1.7.0_55/release /usr/java/jdk1.7.0_55/src.zip [root@essun java]# cd /usr/java/^C [root@essun java]# ll total 4 lrwxrwxrwx 1 root root 16 May 11 09:45 default -> /usr/java/latest drwxr-xr-x 8 root root 4096 May 11 09:44 jdk1.7.0_55 lrwxrwxrwx 1 root root 21 May 11 09:45 latest -> /usr/java/jdk1.7.0_55
添加环境变量
[root@essun bin]# cat /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH #注:$JAVA_HOME/bin一定要写在前面,否则将引用的是系统自带的java环境 [root@essun bin]# java -version java version "1.7.0_55" Java(TM) SE Runtime Environment (build 1.7.0_55-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
到此,jdk安装完成。
2、安装Tomcat (apache-tomcat-7.0.53.tar.gz)
[root@essun download]# tar xf apache-tomcat-7.0.53.tar.gz -C / usr/local/ [root@essun local]# ln -sv apache-tomcat-7.0.53 tomcat `tomcat‘ -> `apache-tomcat-7.0.53‘
添加环境变量
[root@essun tomcat]# cat /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$PATH:$CATALINA_HOME/bin [root@essun tomcat]# . /etc/profile.d/tomcat.sh [root@essun tomcat]# cat cat catalina.sh catchsegv
启动服务
[root@essun tomcat]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@essun tomcat]# ss -tnlp | grep 8080 LISTEN 0 100 :::8080 :::* users:(("java",28017,42)) [root@essun tomcat]# ss -tnlp | grep java LISTEN 0 100 :::8080 :::* users:(("java",28017,42)) LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",28017,45)) LISTEN 0 100 :::8009 :::* users:(("java",28017,43))
添加启动脚本
#!/bin/sh # Tomcat init script for Linux. # # chkconfig: 2345 92 34 # description: The Apache Tomcat servlet/JSP container. JAVA_HOME=/usr/java/latest CATALINA_HOME=/usr/local/tomcat export JAVA_HOME CATALINA_HOME case $1 in start) $CATALINA_HOME/bin/catalina.sh start;; stop) $CATALINA_HOME/bin/catalina.sh stop;; restart) $CATALINA_HOME/bin/catalina.sh stop sleep 2 $CATALINA_HOME/bin/catalina.sh start;; esac [root@essun tomcat]# chmod +x /etc/rc.d/init.d/tomcat [root@essun tomcat]# chkconfig --add tomcat [root@essun tomcat]# chkconfig tomcat on
访问测试
到此,tomcat安装完成
只要简单的设置就可以
修改/usr/local/tomcat/conf/tomcat-users.xml
<tomcat-users> <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui"/> <user username="tomcat" password="tomcat" roles="admin-gui"/> </tomcat-users>
重启tomcat
[root@essun tomcat]# service tomcat restart Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
web重新访问,在默认页面的右边有三个按钮
Server Status:服务器性能监控
Manager App :应用程序管理
输入登录密码
应用程序相关信息
应用程序部署
此外,context也可以让管理员基于每个app提供日志、appBase、DB连接等配置,这极大地增强配置的灵活性;
Tomcat7 deploy应用程序的方法:
Host Manager :布署虚拟主机
三、tomcat 目录结构
[root@essun tomcat]# ls -l | grep "^d" drwxr-xr-x 2 root root 4096 May 11 11:36 bin drwxr-xr-x 3 root root 4096 May 11 11:42 conf drwxr-xr-x 2 root root 4096 May 11 11:36 lib drwxr-xr-x 2 root root 4096 May 11 11:42 logs drwxr-xr-x 2 root root 4096 May 11 11:36 temp drwxr-xr-x 7 root root 4096 Mar 25 11:24 webapps drwxr-xr-x 3 root root 4096 May 11 11:42 work
1、各目录的作用
bin ——Tomcat执行脚本目录
conf ——Tomcat配置文件
lib ——Tomcat运行需要的库文件(JARS)
logs ——Tomcat执行时的LOG文件
temp ——Tomcat临时文件存放目录
webapps ——Tomcat的主要Web发布目录(存放我们自JSP,SERVLET,CLASSES)
work ——Tomcat的工作目录,Tomcat将翻译JSP文件到的Java文件和class文件放在这
2、各目录中的文件
conf/
[root@essun tomcat]# ls conf Catalina context.xml tomcat-users.xml catalina.policy logging.properties web.xml catalina.properties server.xml
最主要的配置文件有
server.xml Tomcat 的全局配置文件
web.xml 为不同的Tomcat配置的web应用设置缺省值的文件
tomcat-users.xml Tomcat用户认证的配置文件
context.xml 用于虚拟主机的配置及.war文件中的URL映射。使用context可以降低系统负载,因为,当某URL请求到达tomcat时,如果没有context,tomcat需要搜索server.xml或context.xml的配置以确定此URL,否则则需要搜索所有的war文件来查找用户的请求的资源
webapps
[root@essun webapps]# ls docs examples host-manager manager ROOT
含Web应用的程序 (JSP、Servlet等)
work
[root@essun work]# cd Catalina/localhost/ [root@essun localhost]# ls _ docs examples host-manager manager
由Tomcat自动生成,这是Tomcat放置它运行期间的中间文件(诸如编译的JSP文件)地方。 如果当Tomcat运行时,你删除了这个目录那么将不能够执行包含JSP的页面。
Tomcat的主配置文件,包含Service, Connector, Engine, Realm, Valve, Hosts主组件的相关配置信息;
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="8005" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the BIO implementation that requires the JSSE style configuration. When using the APR/native implementation, the OpenSSL style configuration is required as described in the APR/native documentation --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- Use the LockOutRealm to prevent attempts to guess user passwords via a brute-force attack --> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server>
<Server port="8005" shutdown="SHUTDOWN">
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps"> <Context path="" docBase="ROOT"/> <Context path="/bbs" docBase="/web/bss" reloadable="true" crossContext="true"/> </Host> <Host name="mail.essun.com" appBase="/web/mail"> <Context path="" docBase="ROOT"/> </Host> </Engine>
<Host name="www.essun.com" appBase="webapps" unpackWARs="true"> <Alias>essun.com</Alias> </Host>
Context : 一个 Context 对应于一个 web application,一个 web application 由一个或者多个Servlet 组成。每个 Context 在创建的时候都将根据配置文件conf/web.xml 和 webapps/${webapp.home}/WEB-INF/web.xml 载入 Servlet 类和创建映射表(mapping table)。当Context 获得请求时,将在自己的映射表中寻找相匹配的 Servlet 类。如果找到,则执行该类,获得结果并返回。
<!-- Tomcat Root Context --> <Context path="" docBase="/web/webapps"/> <!-- buzzin webapp --> <Context path="/bbs" docBase="/web/threads/bbs" reloadable="true"> </Context> <!-- chat server --> <Context path="/chat" docBase="/web/chat"/> <!-- darian web --> <Context path="/darian" docBase="darian"/>
Realm组件:一个Realm表示一个安全上下文,它是一个授权访问某个给定Context的用户列表和某用户所允许切换的角色相关定义的列表。因此,Realm就像是一个用户和组相关的数据库。定义Realm时惟一必须要提供的属性是classname,它是Realm的多个不同实现,用于表示此Realm认证的用户及角色等认证信息的存放位置。
下面是一个常见的使用UserDatabase的配置:
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99" driverName="org.gjt.mm.mysql.Driver" connectionURL="jdbc:mysql://localhost/authority" connectionName="test" connectionPassword="test" userTable="users" userNameCol="user_name" userCredCol="user_pass" userRoleTable="user_roles" roleNameCol="role_name" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
SemaphoreValve:用于控制Tomcat主机上任何容器上的并发访问数量;
JvmRouteBinderValve:在配置多个Tomcat为以Apache通过mod_proxy或mod_jk作为前端的集群架构中,当期望停止某节点时,可以通过此Valve将用记请求定向至备用节点;使用此Valve,必须使用JvmRouteSessionIDBinderListener;
<Context path="/probe" docBase="probe"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.0\.0\.1"/> </Context>
3) ResourceEnvRef;
<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources>
4)BackupManager:用于Tomcat集群的会话管理器,与DeltaManager不同的是,某节点会话的改变只会同步给集群中的另一个而非所有节点。
Stores:PersistentManager必须包含一个Store元素以指定将会话数据存储至何处。这通常有两种实现方式:FileStore和JDBCStore
Resources:经常用于实现在Context中指定需要装载的但不在Tomcat本地磁盘上的应用资源,如Java类,HTML页面,JSP文件等。
Cluster:专用于配置Tomcat集群的元素,可用于Engine和Host容器中。在用于Engine容器中时,Engine中的所有Host均支持集群功能。在Cluster元素中,需要直接定义一个Manager元素,这个Manager元素有一个其值为org.apache.catalina.ha.session.DeltaManager或org.apache.catalina.ha.session.BackupManager的className属性。同时,Cluster中还需要分别定义一个Channel和ClusterListener元素。
tomcat-user.xml
Realm认证时用到的相关角色、用户和密码等信息;Tomcat自带的manager默认情况下会用到此文件;在Tomcat中添加/删除用户,为用户指定角色等将通过编辑此文件实现;
catalina.policy
Java相关的安全策略配置文件,在系统资源级别上提供访问控制的能力;
catalina.properties
Tomcat内部package的定义及访问相关的控制,也包括对通过类装载器装载的内容的控制;Tomcat6在启动时会事先读取此文件的相关设置;
logging.properties:
Tomcat6通过自己内部实现的JAVA日志记录器来记录操作相关的日志,此文件即为日志记录器相关的配置信息,可以用来定义日志记录的组件级别以及日志文件的存在位置等;
context.xml
所有host的默认配置文件
四、SHOP++网上商城系统安装
1、环境部署
需要安装mysql
[root@essun conf]# yum install -y mysql-server mysql root@essun shop]# service mysqld start Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK
修改/usr/local/tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding = "UTF-8" />
在站点目录(/usr/local/tomcat/webapps)中,新建一目录名为shop
[root@essun webapps]# mkdir shop
将下载后网上商城解压到此下目录
[root@essun shop]# pwd /usr/local/tomcat/webapps/shop [root@essun shop]# ls admin changelog.txt index.html license.html product robots.txt sitemap WEB-INF article favicon.ico install META-INF resources shopxx.txt upload
重启tomcat服务
[root@essun shop]# service tomcat restart Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
使用浏览器访问安装
环境检测
系统配置
系统安装
访问首页
子页测试
登录后台
后台管理
2、请求解析
========================完===========================================
本文出自 “和风细雨” 博客,请务必保留此出处http://essun.blog.51cto.com/721033/1409616
Linux系统下Jsp环境部署,布布扣,bubuko.com
原文地址:http://essun.blog.51cto.com/721033/1409616