tomcat服务器

Tomcat 目录结构bin 运行程序conf 配置文件lib tomcat需要类库logs tomcat日志webapps 网站部署目录work 存放JSP 编译后文件Tomcat 启动问题1、没有

Tomcat 目录结构

bin 运行程序

conf 配置文件

lib tomcat需要类库

logs tomcat日志

webapps 网站部署目录

work 存放JSP 编译后文件

Tomcat 启动问题

1、没有配置JAVA_HOME

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

At least one of these environment variable is needed to run this program

2、配置JAVA-HOME JAVAHOME 配置值时候多写;

3、将JAVA_HOME配置成为 JRE 路径

The JAVA_HOME environment variable is not defined correctly

This environment variable is needed to run this program

NB: JAVA_HOME should point to a JDK not a JRE

端口占用启动问题

发现tomcat 端口被占用(怎么发现的 logs ) ----- Fport 找到占用端口进程名称 ----- 任务管理器关闭进程

8080 是tomcat 默认端口 Oracle Listener

开发中经常将默认端口修改为80 ----- conf/server.xml

CATALINA_HOME环境变量: 用来指定默认运行tomcat 目录位置、不管运行哪个tomcat/bin/startup.bat 都启动 CATALINA_HOME配置tomcat

tomcat 中如何部署web 应用

1、tomcat/conf/context.xml 里面配置信息将会用于所有web 应用

2、tomcat/conf 为每个主机建立一个context.xml 用于所有该主机web 应用

部署web 应用三种方式

1、配置server.xml 修改元素内部添加

1

,

在D:apache-tomcat-7.0.69conf下的server.xml 下加入

//abc是访问路径,直接/abc/index.html

2、配置 $CATALINA_HOME/conf/[enginename]/[hostname]/ directory

在tomcat 安装目录下的conf 下新建Catalina 然后在Catalina 下新建 localhost 。在localhost 下建一个abc.xml 文件,那么abc 就是访问路径。在abc.xml 中加入启动tomcat 在访问时用/abc/index.html

3、将整个web 应用目录拷贝到 webapps

配置缺省web 应用

访问主机IP ,直接打开默认web 应用 ------------ localhost回车后即可访问web 应用的index 内容

1、webapps 下建立ROOT 目录就是缺省应用,tomcat 已经占据了

2、配置server.xml ----- path什么都不写就是缺省web 应用

3、在conf/Catalina/localhost 里面建 ROOT.xml 文件,在其中添加

war 文件是 zip 格式

静态资源直接存放网站根目录

动态java 网站会存在WEB-INF

web.xml 配置欢迎页面

在web.xm 中有这么一段代码,默认三个页面为欢迎界面。

index.html

index.htm

index.jsp

如何架设一个站点?

1、开发站点代码

2、一台服务器(1) 自己购买服务器,服务器托管 2) 购买服务器空间虚拟主机空间) -------- IP 地址

3、购买域名一级域名 news.itcast.cn 二级域名

配置虚似主机

2

,

1、在一个tomcat 服务器中可以放置多个网站,所谓配置虚似主机,就是在tomcat 服务器中配置一个网站。

2、如需在WEB 服务器中配置一个网站,需使用Host 元素进行配置,例:

unpackWARs="true" autoDeploy="true">

prefix="localhost_access_log." suffix=".txt"

pattern="h l u t "r" s b" />

unpackWARs="true" autoDeploy="true">

3、配置的主机(网站) 要想被外部访问,必须在DNS 服务器或windows 系统中注册

C:WINDOWSsystem32driversetchosts

2、配置hosts 文件将目标域名映射本机

3、提供默认缺省虚拟目录(网站) ---- ROOT

配置缺省虚拟主机有什么用? ---- 当服务器DNS 瘫痪时,只有缺省虚拟主机能被访问!

在server.xml 中有一句话就是定义缺省主机的。

登录服务器管理员的方法:

启动tomcat 主页,点击tomcat manager要求输入用户名密码。点击取消进入说明。

在conf 下的tomcat-users.xml 文件中加入提示的

而后登录即可

context 元素常用属性

3

,

WEB 应用的组成结构

开发web 应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web 应用无法访问,还会导致web 服务器启动报错。

4

,

在myeclipse 中新建一个web project工程

5

,

OSI 七层协议 TCP/IP分层

TCP 传输层协议 IP 网络层协议

三次握手

发送方:你能收到我的数据吗?

接收方:我能收到你的数据,你能收到我的数据吗

发送方:我也能收到

TCP 不丢包的数据传输,发送方如果一直收不到接收方回应,就会超时

HTTP 请求三部分:请求行、消息头、消息体

请求行三部分:请求方式(GET/Post)、请求资源路径、协议版本

消息头以键值对的方式传输

GET 和POST 区别

GET 数据格式请求数据位于请求行中

GET /day4/index.html?username=aaa&password=bbb HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*

Referer: http://localhost/day4/login.html

Accept-Language: zh-cn

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) Host: localhost

Connection: Keep-Alive

POST 数据格式请求数据位于请求体中

POST /day4/index.html HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*

Referer: http://localhost/day4/login.html

6

,

Accept-Language: zh-cn

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) Host: localhost

Content-Length: 25

Connection: Keep-Alive

Cache-Control: no-cache

username=aaa&password=bbb

请求的解析

Accept: text/html,image/* image/jpeg 通知服务器, 客户端可以识别文件格式 MIME-Type

Accept-Charset: ISO-8859-1 客户端能够接受编码

Accept-Encoding: gzip 客户端识别压缩格式

Accept-Language:zh-cn 客户端语言

Host: www.itcast.com:80 访问目标服务器

If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT

该资源客户端存在,告诉服务器该资源最后修改时间 ----- 如果客户端资源已经最新,服务器将通知客户端找寻本地缓存

Referer: http://www.itcast.com/index.jsp

请求来源

举例:

GET http://www.itcast.cn HTTP/1.1

Referer:www.baidu.com

访问itcast 请求来自百度

防止盗链

User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)

Cookie

Connection: close/Keep-Alive

7

,

Date: Tue, 11 Jul 2000 18:23:51 GMT

服务器响应的解析

HTTP 响应包括3个部分:响应行、头信息、响应体

响应行包括3个部分:协议版本、状态码、状态描述信息

状态码

1xx:请求收到,需要继续处理

2xx:请求处理完成

3xx: 重定向

4xx: 客户端出错,服务器无法处理 ---- 页面找不到

5xx: 服务器出错

200 请求处理成功

302 重定向 A 找B 借钱,B 告诉A 去找C 借去

304和307 If-modified-since last-modified 服务器通知客户端查看本地缓存 A 找B 借钱,B 说你兜里有

404 页面找不到错误 A 找B 借钱 B 说没钱

500 服务器内部异常

响应头含义

Location: http://www.it315.org/index.jsp

结合302状态码使用,服务器通知客户端重定向资源 --- 常见应用:未登陆,自动跳转登陆页面

Server:apache tomcat 服务器类型

Content-Encoding: gzip 通知客户端响应内容使用压缩格式

Content-Length: 80 响应长度

Content-Language: zh-cn 响应语言

Content-Type: text/html; charset=GB2312 响应数据类型、编码集

Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT

通知客户端,该资源在服务器上最后修改时间

8

,

Refresh: 1;url=http://www.it315.org ----- 表示1秒后跳转www.it315.org

自动刷新功能,案例:用户在登陆后,显示5秒后自动跳回主页

Content-Disposition: attachment; filename=aaa.zip

文件以附件格式下载

Expires: -1 过期时间

Cache-Control: no-cache 不缓存

Pragma: no-cache 不缓存

以上三个头信息资源,用于通知客户端响应数据不缓存,三个头一起使用是为了满足跨浏览器

Java 基础班 Socket 编程通过控制请求与响应实现部分功能

Serverlet 编程服务器响应请求的处理

publicclass extends HttpServlet {

// 手动压缩

protectedvoid doGet(HttpServletRequest req , HttpServletResponse response ) throws ServletException, IOException {

String data =

"sdgfasgfasdfgasdgasdjklfadshklfasdkfhadsjkfhasdhkfjadshfjkasdhfaskdjfhaskdjfha dsfjkhadkhadshfkaskdhlfaskldfhadshkfhadsklfhladshlfashdlf" ;

}

// 文件下载

protectedvoid demo4(HttpServletRequest request ,

9

byte [] b1 = data .getBytes(); // 原始数组 System. out .println(b1. length ); ByteArrayOutputStream out = new ByteArrayOutputStream(); // 字节数组 GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out ); gzipOutputStream .write(b1); byte [] b2 = out .toByteArray(); // 压缩后数组 System. out .println(b2. length );

,

HttpServletResponse response ) throws ServletException, IOException { response

.setHeader("Content-Disposition" , "attachment;

filename=mm.jpg");

}

// 防盗链程序

protectedvoid demo3(HttpServletRequest request ,

}

// 5秒后跳转到登陆页面

protectedvoid demo2(HttpServletRequest req , HttpServletResponse response )

}

10

File file = new File(getServletContext().getRealPath("/mm.jpg")); InputStream in = new FileInputStream(file ); OutputStream out = response .getOutputStream(); int temp ; while ((temp = in .read()) != -1) { } out .close(); in .close(); out .write(temp ); HttpServletResponse response ) throws ServletException, IOException { // 获得 String referer = request .getHeader("referer" ); if (referer == null || !referer .startsWith("http://localhost/day4")) { System. out .println(" 盗链" ); } else { } System. out .println(" 不是盗链" ); throws ServletException, IOException { response .setHeader("Refresh" , "5;url=login.html");

标签: