您的位置:首页 > 运维架构 > Tomcat

JavaWeb Tomcat 配置文件解析及JSP Servlet简介

2014-02-21 16:25 501 查看

1 Server

1.1 In the Tomcat world, a
Server represents thewhole container. Tomcat provides a default implementation of theServerinterface., and this is rarely customized
by users.
1.2 在tomcat的世界里,一个server代表整个容器.Tomcat提供了一个默认的org.apache.catalina接口的实现.用户很少修改这个默认的实现

2 Service

2.1 A
Service is anintermediate component which lives inside a Server and ties one or moreConnectors to exactly one Engine. The Service element is rarely customized byusers, as the default implementation is simple and sufficient:Serviceinterface.

2.2 一个Service是一个中间件,存在在一个Server的内部,将一个或者多个Connectors绑定到一个特定的Engine上.默认的实现已经足够用了.是org.apache.catalina.Service接口的一个实现.
A "Service" is a collection ofone or more "Connectors" that share a single "Container"

一个Service是一个或者多个Connectors的集合,这些个Connectors共享一个容器。

3 Engine

3.1 An
Engine representsrequest processing pipeline for a specific Service. As a Service may havemultiple Connectors, the Engine received and processes all requests from theseconnectors, handing the response back to the appropriate connector fortransmission to
the client. The
Engineinterface may be implemented to supply custom Engines, though this isuncommon.
3.2 一个Engine代表一个特定的Service的请求处理的管道.因为一个Service可以有多个Connectors,Engine接收并且处理从这些Connectors过来的所有的请求.并且将结果送回合适的connector并发送给客户端.
3.3 可以实现org.apache.catalina.Interface Engine接口来提供定制的Engines,虽然一般不需要这样做.
3.4 Note that the Engine may beused for Tomcat server clustering via the jvmRoute parameter. Read theClustering documentation for more information.

4 Host

4.1 A
Host is an associationof a network name, e.g. www.yourcompany.com, to the Tomcat server. An Enginemay contain multiple hosts, and the Host element also supports network aliasessuch as yourcompany.com and abc.yourcompany.com. Users rarely create customHostsbecause
the
StandardHostimplementation provides significant additional functionality.
4.2 一个Host将一个域名和tomcat联系起来.一个Engine可以包含多个hosts,并且一个Host还支持网络别名(例如yourcompany.com
或者 abc.yourcompany.com).用户很少去实现一个org.apache.catalina.InterfaceHost接口,因为org.apache.catalina.core.StandardHost这个默认的实现已经提供了丰富的扩展功能了

5 Connector

5.1 A Connector handlescommunications with the client. There are multiple connectors available withTomcat, all of which implement theConnectorinterface.
These include the
Coyote connector whichis used for most HTTP traffic, especially when running Tomcat as a standaloneserver, and theJK2connector which implements the AJP procotol used
when connecting Tomcat toan Apache HTTPD server. Creating a customized connector is a significanteffort.
5.2 一个Connector处理和客户端的通信.tomcat有多个connectors.这些个connectors都实现了Connector接口.
5.3 创建一个定制的connector是非常复杂的.
5.4 AJP是为Tomcat与HTTP服务器之间通信而定制的协议,能提供较高的通信速度和效率。在配置Tomcat与HTTP服务器集成中,读者可以不必关心AJP协议的细节。关于AJP的知识也可以参考网址:
5.5 Tomcat最主要的功能是提供Servlet/JSP容器,尽管它也可以作为独立的Java Web服务器,它在对静态资源(如HTML文件或图像文件)的处理速度,以及提供的Web服务器管理功能方面都不如其他专业的HTTP服务器,如IIS和Apache服务器。
5.6 因此在实际应用中,常常把Tomcat与其他HTTP服务器集成。对于不支持Servlet/JSP的HTTP服务器,可以通过Tomcat服务器来运行Servlet/JSP组件。
5.7 当Tomcat与其他HTTP服务器集成时,Tomcat服务器的工作模式通常为进程外的Servlet容器,Tomcat服务器与其他HTTP服务器之间通过专门的插件来通信。
5.8 Tomcat与HTTP服务器集成的原理:Tomcat服务器通过Connector连接器组件与客户程序建立连接,Connector组件负责接收客户的请求,以及把Tomcat服务器的响应结果发送给客户。默认情况下,Tomcat在server.xml中配置了两种连接器:
l <Connector port="8080"protocol="HTTP/1.1" connectionTimeout="20000"redirectPort="8443" />
l <Connector port="8009"protocol="AJP/1.3" redirectPort="8443" />
5.9 第一个连接器监听8080端口,负责建立HTTP连接。在通过浏览器访问Tomcat服务器的Web应用时,使用的就是这个连接器。
5.10 第二个连接器监听8009端口,负责和其他的HTTP服务器建立连接。在把Tomcat与其他HTTP服务器集成时,就需要用到这个连接器。
5.11 Tomcat提供了专门的JK插件来负责Tomcat和HTTP服务器的通信。应该把JK插件安置在对方的HTTP服务器上。
5.12 对于不同的HTTP服务器,Tomcat提供了不同的JK插件的实现模块
l 与Windows下的Apache HTTP服务器集成:mod_jk_2.0.46.dll
l 与Linux(RedHet)下的Apache HTTP服务器集成:mod_jk.so-ap2.0.46-rh72..46-rh72
l 与IIS服务器集成:isapi_redirect.dll
5.13 AJP是为Tomcat与HTTP服务器之间通信而定制的协议,能提供较高的通信速度和效率。
5.14 关于AJP的知识也可以参考网址:

http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk2/doc/common/AJPv13.html
5.15 如果两个Tomcat服务器都在同一台机器上运行,则至少应该对其中一个Tomcat服务器的以上3个端口号都进行修改。

6 Context

6.1 A
Context represents aweb application. A Host may contain multiple contexts, each with a unique path.TheContextinterface may be implemented
to create custom Contexts, but this is rarelythe case because theStandardContextprovides significant additional functionality.

6.2 一个Context代表一个web应用程序。一个Host可以包含多个contexts。每一个有不同的访问地址。
6.3 可以实现Context接口来创建自己的Contexts。但很少这样用,因为StandardContext已经提供了丰富的额外的功能

1. 符合j2ee标准的web-app的目录结构

n WEB-INF

u classes

u web.xml

u lib

2. servlet开发过程

n 从httpservlet继承, 重写doget / doPost方法

n 部署web.xml

3. servlet声明周期

n 只有一个对象

n 第一次请求的时候被初始化,只一遍

n 初始化后先调用init方法,只一遍

n 每个请求,调用一遍serviceàserviceàdoGet/doPost。以多线程的方式运行

u 不要在servlet中设计成员变量。

n 卸载前调用destroy方法

4. 为什么有2个init方法

5. /的含义

n form

u form提交中action的路径可以为虚拟路径,

u 也可以为绝对路径,/代表http://127.0.0.1/

n web.xml

u web.xml中url-pattern为webapplication根路径,/代表http://127.0.0.1/Test/

6. 乱码

场合:页面本身有中文的时候

解决办法:servlet:resp.setContentType("text/html;charset=gbk");

Jsp: <%@ page contentType="text/html;charset=gb2312"%>

注意:一定要写在PrintWriter out = resp.getWriter();之前

场合:解决get方式乱码问题:

解决办法:修改server.xmlàURIEncoding="GBK"

场合:解决post方式提交内容的乱码

解决办法:request.setCharacterEncoding("GBK");

注意:一定要写在存取第一个参数之前

不要调用response.setCharacterEncoding("GBK");

场合:<jsp:param name="user" value="<%=s%>"/>,url地址包含中文参数

解决办法:<%request.setCharacterEncoding("GBK");%>

注意:

7. Cookie
和session的区别

注意cookie有时候禁不掉

Cookie

session

存储在客户端

存储在服务器端

两种类型

l 有声明周期

l 无声明周期

两种实现方式

l 依赖于cookie

l url重写

父路径不能访问子路径的cookie

同一个session的窗口共享一个session

典型应用:

l 3个月不用再登陆
l 购物车(http://www.china-pub.com/)
典型应用:

l 用户登陆

l 购物车也可以用session实现。·

不可靠

可靠

8. application (ServletContext)的概念

9. jsp初学者容易犯的错误

n WEB-INF子目录下面不能放置html jsp 等文件

n 不能直接打开

10. jsp/servlet连接数据库时要注意的事项

拷贝数据库驱动的jar包到apache-tomcat-6.0.14\webapps\Test\WEB-INF\lib文件夹

确认数据库已经启动

用户名、密码应该正确

11. <%! <%区别

12. 如何解决ie缓存问题

13. TestErr.jsp / ErrPage.jsp

l 不太稳定,有的时候不好使:

n 先不写这句话:<%@pageerrorPage="ErrPage.jsp"%>

n Ie不行,firefox行

14. <%@include和jsp:include的区别

<%@ include file="date.jsp" %>

<jsp:include page="date.jsp" flush="true"/>

include编译指令是在JSP程序的转换时期就将file属性所指定的程序内容嵌入,然后再编译执行;

而include指令在转换时期是不会被编译的,只有在客户端请求时期如果被执行到才会被动态的编译载入

只生成一个class文件

多个

Include不能带参数

<jsp:include>可以

同一个request对象

不同的request对象,可以取得包含它的页面的参数,并添加了自己的参数

常用

不常用

15. forward和sendredirect的区别

Sendredirect

Forward

是不同的request

虽然是不同的对象,但是,可以取到上一个页面的内容

send后的语句会继续执行,除非return

forward后的语句不会继续发送给客户端

速度慢

速度快

需要到客户端的往返,可以转到任何页面,

服务器内部转换,

地址栏有变化

地址栏没有变化

可以传参数,直接写在url后面

可以传参数

/代表的是http://127.0.0.1/

<%response.sendRedirect("/test/ServletToJSP");%>

<form action="/test/ServletToJSP">

<a href="/test/ServletToJSP">test</a>

/代表的是http://127.0.0.1/test

this.getServletContext().getRequestDispatcher ("/servlet_jsp/ServletUseJsp.jsp")

.forward(req, resp);
并且只能以/开头
常用

常用

16. <jsp:useBean scope =page|request|session|application>

17. jsp内置对象: out request response sessionapplication exception

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: