8-19HTTP协议+servlet

本文深入讲解JavaWeb的基本组件,通过创建小型项目理解Request与Response的工作原理,演示Servlet的使用,包括HTTP请求处理、转发和重定向等核心概念。

接下来几天我们将根据老师的讲解做出一个小型的例子,来深入了解JAVAWEB的相关内容,最后的完整项目会上传。

Request+Response
  1. 请求行
  2. 请求头部
  3. 空行
  4. 请求数据
Servlet
  1. DNS域名解析
  2. 建立连接(TCP/IP三次握手)
    在这里插入图片描述
    在这里插入图片描述
  3. demo
package com.pactrea.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class IndexServlet extends HttpServlet {
    @Override
    public void init(){

    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        try {
            response.sendRedirect("jsp/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        doGet(request,response);
    }
    @Override
    public void destroy(){

    }

}

JavaWebDemo
在这里插入图片描述

package com.pactrea.servlet;

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 IndexServlet extends HttpServlet {
    @Override
    public void init() {

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        request.setAttribute("name", "张三");

        // 重定向 不带参数
//        try {
//            response.sendRedirect("jsp/index.jsp");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        // 转发 带着信息转发
        try {
            request.getRequestDispatcher("jsp/index.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        doGet(request, response);
    }

    @Override
    public void destroy() {

    }

}

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <welcome-file-list>
        <welcome-file>/jsp/index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>haha</servlet-name>
        <servlet-class>com.pactrea.servlet.IndexServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>haha</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/19
  Time: 13:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>jsp</title>
</head>
<body>
jsp包的index
<%=request.getAttribute("name")%>
${name}
</body>
</html>

F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111\bin\catalina.bat run [2025-10-19 04:23:27,224] Artifact untitled3:war: Waiting for server connection to start artifact deployment... Using CATALINA_BASE: "C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3" Using CATALINA_HOME: "F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111" Using CATALINA_TMPDIR: "F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111" Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_202" Using CLASSPATH: "F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111\bin\bootstrap.jar;F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111\bin\tomcat-juli.jar" Using CATALINA_OPTS: "" Connected to the target VM, address: '127.0.0.1:50342', transport: 'socket' 19-Oct-2025 16:23:28.281 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.服务器版本: Apache Tomcat/9.0.111 19-Oct-2025 16:23:28.283 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器构建: Oct 10 2025 14:13:20 UTC 19-Oct-2025 16:23:28.283 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器版本号: 9.0.111.0 19-Oct-2025 16:23:28.283 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 操作系统名称: Windows 10 19-Oct-2025 16:23:28.283 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS.版本: 10.0 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 架构: amd64 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java 环境变量: C:\Program Files\Java\jdk1.8.0_202\jre 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java虚拟机版本: 1.8.0_202-b08 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM.供应商: Oracle Corporation 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djava.util.logging.config.file=C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3\conf\logging.properties 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50342,suspend=y,server=n 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -javaagent:C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcom.sun.management.jmxremote= 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcom.sun.management.jmxremote.port=1099 19-Oct-2025 16:23:28.284 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcom.sun.management.jmxremote.ssl=false 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcom.sun.management.jmxremote.password.file=C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3\jmxremote.password 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcom.sun.management.jmxremote.access.file=C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3\jmxremote.access 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djava.rmi.server.hostname=127.0.0.1 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djdk.tls.ephemeralDHKeySize=2048 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dsun.io.useCanonCaches=false 19-Oct-2025 16:23:28.285 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dignore.endorsed.dirs= 19-Oct-2025 16:23:28.289 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcatalina.base=C:\Users\ZhuanZ1\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_untitled3 19-Oct-2025 16:23:28.289 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Dcatalina.home=F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111 19-Oct-2025 16:23:28.289 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -Djava.io.tmpdir=F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111 19-Oct-2025 16:23:28.291 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent 使用APR版本[1.7.4]加载了基于APR的Apache Tomcat本机库[1.3.1]。 19-Oct-2025 16:23:28.291 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR功能:IPv6[true]、sendfile[true]、accept filters[false]、random[true]、UDS [true]。 19-Oct-2025 16:23:28.292 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL配置:useAprConnector[false],useOpenSSL[true] 19-Oct-2025 16:23:28.296 信息 [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL成功初始化 [OpenSSL 3.0.14 4 Jun 2024] 19-Oct-2025 16:23:28.516 信息 [main] org.apache.coyote.AbstractProtocol.init 初始化协议处理器 ["http-nio-8080"] 19-Oct-2025 16:23:28.531 信息 [main] org.apache.catalina.startup.Catalina.load 服务器在[461]毫秒内初始化 19-Oct-2025 16:23:28.561 信息 [main] org.apache.catalina.core.StandardService.startInternal 正在启动服务[Catalina] 19-Oct-2025 16:23:28.561 信息 [main] org.apache.catalina.core.StandardEngine.startInternal 正在启动 Servlet 引擎:[Apache Tomcat/9.0.111] 19-Oct-2025 16:23:28.572 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"] 19-Oct-2025 16:23:28.588 信息 [main] org.apache.catalina.startup.Catalina.start [55]毫秒后服务器启动 Connected to server [2025-10-19 04:23:28,797] Artifact untitled3:war: Artifact is being deployed, please wait... 19-Oct-2025 16:23:29.768 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。 19-Oct-2025 16:23:29.829 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean FrameworkServlet 'spring': initialization started 19-Oct-2025 16:23:29.938 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.context.support.AbstractApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Sun Oct 19 16:23:29 CST 2025]; root of context hierarchy 19-Oct-2025 16:23:29.983 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml] 19-Oct-2025 16:23:30.585 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register Mapped "{[/hello]}" onto public org.springframework.web.servlet.ModelAndView com.springmvc.hello(java.lang.String) 19-Oct-2025 16:23:30.679 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'spring-servlet': startup date [Sun Oct 19 16:23:29 CST 2025]; root of context hierarchy 19-Oct-2025 16:23:30.721 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'spring-servlet': startup date [Sun Oct 19 16:23:29 CST 2025]; root of context hierarchy 19-Oct-2025 16:23:30.848 信息 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean FrameworkServlet 'spring': initialization completed in 1015 ms [2025-10-19 04:23:30,871] Artifact untitled3:war: Artifact is deployed successfully [2025-10-19 04:23:30,871] Artifact untitled3:war: Deploy took 2,074 milliseconds 19-Oct-2025 16:23:31.403 警告 [http-nio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:31.468 警告 [http-nio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:38.584 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111\webapps\manager] 19-Oct-2025 16:23:38.647 信息 [Catalina-utility-2] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。 19-Oct-2025 16:23:38.657 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web应用程序目录[F:\apache-tomcat-9.0.111-windows-x64\apache-tomcat-9.0.111\webapps\manager]的部署已在[73]毫秒内完成 19-Oct-2025 16:23:50.660 警告 [http-nio-8080-exec-4] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:54.976 警告 [http-nio-8080-exec-6] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:55.741 警告 [http-nio-8080-exec-8] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:56.028 警告 [http-nio-8080-exec-9] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:56.263 警告 [http-nio-8080-exec-10] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:56.487 警告 [http-nio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:56.740 警告 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring' 19-Oct-2025 16:23:56.985 警告 [http-nio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/untitled3/index.html] in DispatcherServlet with name 'spring'
最新发布
10-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值