JAVA WEB篇1——初识JAVAWEB
JavaWeb主要指使用Java语言进行动态Web资源开发技术的统称,是解决相关Web互联网领域的技术总和
1、Http协议
- HTTP协议(HyperText Transfer Protocol,超文本传输协议)是由W3C(万维网联盟)组织制定 的一种应用层协议,是用来规范浏览器与Web服务器之间如何通讯的数据格式,主要涉及浏览器 的发请求格式和服务器的响应格式
- HTTP协议通常承载于TCP协议之上,而承载于TLS或SSL协议层之上的协议就是常说的HTTPS协 议
- HTTP默认的端口号为80,HTTPS默认的端口号为443
1.1、Http请求格式
请求行请求类型 请求的路径 协议的版本(1.1)
请求头主机 请求长度 请求的浏览器相关信息
空白行(一个空行)
请求体也叫请求数据,可以添加任意的其他数据
POST /task01_demo01/demo1.html HTTP/1.1
Host: localhost:8088
Content-Length: 21
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64)
name=scott&pwd=123456
1.2、Http响应数据
响应行协议的版本(1.0 1.1) 状态码 (200 成功 404 路径错误 500 服务错误) 状态信息
响应头说明客户端要使用的一些附加信息,格式(key:value)
空白行(一个空行)
响应体服务器返回给客户端的文本信息
2、Tomcat
2.1、下载
Tomcat官网https://tomcat.apache.org/download-90.cgi
Tomcat 百度云:https://pan.baidu.com/s/1iEEptTiPXPLls3wBpSUg1g提取码:gv2h
解压到想安装的目录
添加环境变量
添加CATALINA_BASE变量
CATALINA_BASE
D:\apache-tomcat-9.0.31
添加CATALINA_HOME变量
CATALINA_HOME
D:\apache-tomcat-9.0.31
添加到PATH变量
%CATALINA_HOME%\bin;%CATALINA_HOME%\lib
2.2、卸载
删除目录,变量即可
2.3、启动
双击运行即可
2.4、问题
2.4.1、乱码
打开配置文件
apache-tomcat-9.0.31\conf\logging.properties
修改配置
# 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.handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-82localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-83manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-84host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = GBK############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandlerorg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandlerorg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE
2.4.2、黑窗一闪而过
没有正确配置JAVA_HOME环境变量
2.5、启动报错
2.5.1、暴力
找到占用的端口号,并且找到对应的进程,杀死该进程
netstat -ano
2.5.2、温柔:修改自身的端口号
进入conf/server.xml
<Connector port="8888" protocol="HTTP/1.1" onnectionTimeout="20000" redirectPort="8445" />
一般会将tomcat的默认端口号修改为80,80端口号是http协议的默认端口号,在访问时,就不用输入端口号
2.6、配置项目
2.6.1、直接将项目放到webapps目录下即可。
/hello:项目的访问路径–>虚拟目录
简化部署:将项目打成一个war包,再将war包放置到webapps目录下,war包会自动解压缩
2.6.2、配置conf/server.xml文件
在标签体中配置
```
docBase:项目存放的路径 path:虚拟目录 ```
2.6.3、在conf\Catalina\localhost创建任意名称的xml文件。在文件中编写
<Context docBase="D:\hello" />
虚拟目录:xml文件的名称