构建一个普通的maven项目 删除src目录 在项目里面建module

构建

普通

module

关于maven 父子工程的理解

父工程多一个module

父工程

子工程多一个parent

parent

父项目的jar包 子工程可继承

maven 环境优化

根据tomcat 的 web.xml文件 修改项目中的web.xml为最新

优

将maven的结构优化

优化

结构

编写一个servlet 程序

package org.example.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;

/**
 * @ClassName HTTPServlet
 * @Description servlet
 * @Date 2022/11/24
 * @Author depressiom
 */
public class HTTPServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getInputStream();
        PrintWriter writer = resp.getWriter();
        writer.println("hello,servlet");
    }
}

编写一个servlet的映射

  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0"
         metadata-complete="true">
<!--  servlet映射  -->
    <!--  注册servlet  -->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.example.servlet.HTTPServlet</servlet-class>
    </servlet>
    <!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>


</web-app>

配置Tomcat

配置Tomcat
cat

servlet 原理

原理

mapping 问题

一个servlet 指定一个路径

<!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

一个servlet 指定多个路径

<!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello2</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello3</url-pattern>
    </servlet-mapping>

一个servlet 指定通配路径

<!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello/*</url-pattern>
    </servlet-mapping>

默认路径

<!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

指定一些默认后缀或者前缀

<!--  servlet的请求路径  -->
 <!--  可以自定义后缀实行请求映射
      注意点:*前面不添加项目映射的路径
      -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

优先级问题

指定了固有的映射路径优先级最高 如果找不到就会走默认的处理请求

 <!--  servlet的请求路径  -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <!--  404  -->
    <servlet>
        <servlet-name>error</servlet-name>
        <servlet-class>org.example.servlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>error</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

ErrorServlet

package org.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName ErrorServlet
 * @Description 错误页面
 * @Date 2022/11/24
 * @Author depressiom
 */
public class ErrorServlet extends HTTPServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html");
       resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<h1>404</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

posted on 2022-11-24 11:59  depressiom  阅读(13)  评论(0编辑  收藏  举报