动态web资源开发的技术统称为JavaWeb
web应用程序:可以提供浏览器访问的程序
web应用程序若想提供给外部访问:需要一个服务器统一管理Tomcat;
web应用程序组成:
动态web
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Df5ty0M6-1638892680163)(JavaWeb概念.assets/image-20211203235107788.png)]
ASP:微软,国内最早流行;
php:
JSP/Servlet:
服务器是一种被动的操作,用来处理用户的一些请求和给用户一些响应信息;
IIS:微软的,运行ASP,Windows自带;
tomcat:Apache软件基金,java编写的,轻量级,初学web最佳。运行JSP页面和Servlet。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v9x3Vbk5-1638892680165)(JavaWeb概念.assets/image-20211204000343316.png)]
启动访问:http://localhost:8080/
配置:server.xml文件
配置端口号
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
主机名称
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
输入一个域名;回车
检查本机的C:\Windows\System32\drivers\etc\hosts配置文件下有没有这个域名映射;
有:直接返回对应的ip地址,这个地址中,有我们需要访问的web程序,可以直接访问
127.0.0.1 localhost
没有:去DNS服务器找,找到的化就返回,找不到就返回找不到
localhot(local)是不经网卡传输!这点很重要,它不受网络防火墙和网卡相关的的限制。
127.0.0.1是通过网卡传输,依赖网卡,并受到网络防火墙和网卡相关的限制。
HTTP(超文本传输协议)一个简单的请求-响应协议,它通常运行在TCP上
Https:安全的
http1.0:HTTP/1.0客户端可以与web服务器连接后,只能获得一个web资源,断开连接;
http2.0:HTTP/1.1客户端可以与web服务器连接后,可以获取多个web资源;
1、请求行
2、消息头
百度例子:
General概况
Request URL: https://www.baidu.com/ 请求地址
Request Method: GET 请求方法get/post
Status Code: 200 OK 状态码
Remote Address: 14.215.177.39:443 远程地址
Referrer Policy: origin-when-cross-origin
Request Headers请求头
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 告诉浏览器,它所支持的数据类型
Accept-Encoding: gzip, deflate, br 告诉浏览器,它支持的编码格式:GBK utf-8 GB2312 io是859-1
Accept-Language: zh-CN,zh;q=0.9 告诉浏览器,它的语言环境
Cache-Control: max-age=0 缓存控制
Connection: keep-alive 告诉浏览器,请求完成是断开还是保持连接
Cookie: BAIDUID=50A354D020AE4E363F6B193397CE5D10:FG=1; __yjs_duid=1_8deec826354462b4964e8656959b22981636727703661; BIDUPSID=50A354D020AE4E363F6B193397CE5D10; PSTM=1636728447; channel=baidusearch; baikeVisitId=d7ef514f-7af5-4de7-9903-0d62a70de219; BAIDUID_BFESS=7D3EAC75153CE930B7A71A0EA68D5FB7:FG=1; BDUSS=XlzOHVsSWtiSFBvODg4RzlSRGpKSHlSRC1VZnRLYXZVQ2lKdEtndjZCUENYc2hoRVFBQUFBJCQAAAAAAAAAAAEAAABvpNwxcWNoNzM2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMLRoGHC0aBhY; BDUSS_BFESS=XlzOHVsSWtiSFBvODg4RzlSRGpKSHlSRC1VZnRLYXZVQ2lKdEtndjZCUENYc2hoRVFBQUFBJCQAAAAAAAAAAAEAAABvpNwxcWNoNzM2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMLRoGHC0aBhY; BD_UPN=12314753; ZD_ENTRY=bing; ab_sr=1.0.1_NjZmNjUxZjUyYjY0NjUzODU0NDhjM2I2ZTczMjNjODAxZWZmZjJkZGQ5ODM2NWIwZWZhMmUyYTA3OGQ4ODAzODVhYmRmYjkyMTFiY2Q1MjY0MDYzYjYyNDllMTcyNWY5YmJhMmE2MDhiNWVlNTk5NTYzNzNhNjc1M2ZhZmM2MmYyMzk5MzhmYTY3NDkwNmJlZGQwY2I1NDYwZGIyOTc2MTE2YjZkMzlkN2I3MjVhZGExMWY2MjNiZGFjNjY0YTZm; BD_HOME=1; H_PS_PSSID=35359_35105_31253_34584_34504_35245_34578_34606_35325_26350_35301; BA_HECTOR=8l258h2g85ak0501hs1gqkinp0q
Host: www.baidu.com
Referer: https://cn.bing.com/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
ResponseHeaders响应头
Cache-Control: private 缓存控制
Connection: keep-alive 连接
Content-Encoding: gzip 编码
Content-Type: text/html;charset=utf-8 类型
Date: Fri, 03 Dec 2021 16:51:09 GMT
Expires: Fri, 03 Dec 2021 16:51:09 GMT
Server: BWS/1.1
Set-Cookie: BDSVRTM=303; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=35359_35105_31253_34584_34504_35245_34578_34606_35325_26350_35301; path=/; domain=.baidu.com
Strict-Transport-Security: max-age=172800
Traceid: 1638550269025443994618078961669056469914
Transfer-Encoding: chunked
X-Frame-Options: sameorigin
X-Ua-Compatible: IE=Edge,chrome=1
响应体
Accept:text/html 告诉浏览器,它所支持的数据类型
Accept-Encoding: 告诉浏览器,它支持的编码格式:GBK utf-8 GB2312 io是859-1
Accept-Language: 告诉浏览器,它的语言环境
Cache-Control: 缓存控制
Connection: 告诉浏览器,请求完成是断开还是保持连接
Host: 主机
Refresh: 告诉客户端,多久刷新一次
Location: 让网页重新定位
状态码 | 含义 |
---|---|
200 | 请求响应成功 |
3xx | 请求重定向 |
4xx | 找不到资源 |
5xx | 服务器代码错误 |
servlet是sun公司开发动态web的一门技术。
sun在这些API中提供了一个Servlet接口,开发一个Servlet程序只需要两步:
编写一个普通类
实现Servlet接口,我们继承HttpServlet,重写get、post方法
package com.zui.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();// 响应流
writer.print("holle servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp); // 业务逻辑一样,请求方式不同直接调用;
}
}
编写Servlet的映射
为什么需要映射:我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要再web服务器中注册我们写的Servlet,还需要给一个浏览器能够访问的路径;
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>helloservlet-name>
<servlet-class>com.zui.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
web-app>
配置Tomcat
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mSd6w5Po-1638892680167)(JavaWeb概念.assets/image-20211205182311067.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mnzThbXs-1638892680168)(JavaWeb概念.assets/image-20211205201601654.png)]
启动
Servlet由web服务器调用的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W6HCjehM-1638892680168)(JavaWeb概念.assets/image-20211205221509188.png)]
优先级:固有的路径优先级最高,如果找不到走默认的请求
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>*.abcurl-pattern>
servlet-mapping>
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xtISOwc5-1638892680169)(JavaWeb概念.assets/image-20211207235518551.png)]
实现一个Servlet,放入数据
package com.zui.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
context.setAttribute("键","值"); // 将一个数据保存到ServletContexxt中
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
实现另一个Servlet去取第一个Servlet的数据
package com.zui.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String a = (String) context.getAttribute("键");
resp.setContentType("text/html"); // 设置响应基本信息
resp.setCharacterEncoding("utf-8");
resp.getWriter().print(a); // req请求,resp响应
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
编写Servlet的映射
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>helloservlet-name>
<servlet-class>com.zui.servlet.HelloServletContextservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
<servlet>
<servlet-name>getservlet-name>
<servlet-class>com.zui.servlet.GetServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>getservlet-name>
<url-pattern>/gethellourl-pattern>
servlet-mapping>
web-app>
创建一个类获取初始化
package com.zui.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class getServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
}
编写Servlet的映射,配置资源
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<context-param>
<param-name>urlparam-name>
<param-value>jdbc:mysql://localhost:3306/mybatisparam-value>
context-param>
<servlet>
<servlet-name>helloservlet-name>
<servlet-class>com.zui.servlet.HelloServletContextservlet-class>
servlet>
<servlet-mapping>
<servlet-name>helloservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
<servlet>
<servlet-name>getservlet-name>
<servlet-class>com.zui.servlet.GetServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>getservlet-name>
<url-pattern>/gethellourl-pattern>
servlet-mapping>
<servlet>
<servlet-name>mysqlservlet-name>
<servlet-class>com.zui.servlet.getServletContextservlet-class>
servlet>
<servlet-mapping>
<servlet-name>mysqlservlet-name>
<url-pattern>/getmysqlurlurl-pattern>
servlet-mapping>
web-app>
package com.zui.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletRequestDispatcher extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
context.getRequestDispatcher("/getmysqlurl").forward(req,resp); // 请求转发的路径,调用forward实现请求转发;
}
}