Servlet—————————————————————————————————————————————————
    06尚硅谷_Servlet王振国 - 课堂笔记.pdf

    image.png
    image.png
    image.png
    image.png
    image.png
    image.png

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <!-- servlet标签给tomcat配置servlet程序 -->
    7. <servlet>
    8. <!-- servlet-name标签 servlet程序起一个别名(一般是类名) -->
    9. <servlet-name>hello</servlet-name>
    10. <!-- servlet-class是servlet程序的全类名 -->
    11. <servlet-class>com.example.demo2.hello</servlet-class>
    12. </servlet>
    13. <!-- servlet-mapping标签给servlet程序配置访问地址 -->
    14. <servlet-mapping>
    15. <!-- servlet-name标签的作用是告诉服务器,我当前配置的地址给哪个servlet程序使用 -->
    16. <servlet-name>hello</servlet-name>
    17. <!-- url-pattern标签配置访问地址
    18. / 斜杠在服务器解析的时候表示地址为http://ip:port/工程路径
    19. /hello 表示地址http://ip:port/工程路径/hello
    20. -->
    21. <url-pattern>/hello</url-pattern>
    22. </servlet-mapping>

    运行结果
    image.png
    通过继承HttpServlet来实现servlet程序:

    1. package com.example.demo2;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. public class HelloServlet2 extends HelloServlet{
    7. @Override
    8. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    9. System.out.println("doGet方法");
    10. }
    11. @Override
    12. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    13. System.out.println("doPost方法");
    14. }
    15. }

    ②在web.xml中配置

    1. <servlet>
    2. <servlet-name>HelloServlet2</servlet-name>
    3. <servlet-class>com.example.demo2.HelloServlet2</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <servlet-name>HelloServlet2</servlet-name>
    7. <url-pattern>/hello2</url-pattern>
    8. </servlet-mapping>

    ③在网页中进行测试

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Servlet测试</title>
    6. </head>
    7. <body>
    8. <form action="http://localhost:8080/demo2/hello2" method="get">
    9. <input type="submit">
    10. </form>
    11. </body>
    12. </html>

    运行结果:
    image.png

    07尚硅谷_Servlet王振国 - 课堂笔记.pdf
    每次请求都会封装新的request

    常见请求和响应头-说明.pdf