Servlet

文章目录


文章目录

  • 文章目录
  • 一、Servlet快速入门
  • 二、Servlet的生命周期
  • 三、servlet方法
  • 四、servlet体系结构
  • 五、servlet urlPattern配置
  • 六、XML配置方式编写servlet



一、Servlet快速入门

Servlet_第1张图片
案例:

//servlet依赖坐标
<dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>


package com.study.web;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo1")
public class ServletDemo1 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("Hello!");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

在控制台会打印Hello!。

二、Servlet的生命周期

Servlet_第2张图片
案例:

package com.study.web;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo2")
public class ServletDemo2 implements Servlet {
    //第一次访问servlet的时候调用,该方法只调用一次。
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init.......");

    }
    //每次访问Servlet时都会被调用
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Hello Service");
    }
    //释放内存或容器关闭时调用,只调用一次
    @Override
    public void destroy() {
        System.out.println("destroy....");

    }
    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
}

三、servlet方法

Servlet_第3张图片

四、servlet体系结构

Servlet_第4张图片
案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/demo3" method="post">
    <input name="username" type="text"><input type="submit" value="提交">
</form>
</body>
</html>


package com.study.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost...");
    }
}

五、servlet urlPattern配置

Servlet_第5张图片
Servlet_第6张图片
Servlet_第7张图片

六、XML配置方式编写servlet

Servlet_第8张图片

你可能感兴趣的:(JavaEE,java,intellij-idea,java-ee)