Servlet

第一章 Servlet快速入门

1.1 Servlet的概述

  • Servlet是“Server”和“applet”单词组成的合成词,称为服务端小程序。
  • Servlet就是一个接口,定义了java类被服务器解析的规则。

1.2 Servlet快速入门

  • ①创建JavaEE项目。
  • ②定义一个类,继承HttpServlet。
  • ③根据请求方式,实现对应的方法,如doGet()方法。
  • ④配置Servlet。

 

  • 示例:
  • 定义HelloServlet.java
package com.sunxiaping;

import java.io.IOException;

/**
 * @author 许威威
 */
public class HelloWorldServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("你好 Servlet");
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doPost(request, response);
    }
}
  • 配置web.xml
xml version="1.0" encoding="UTF-8"?>
<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>HelloWorldServletservlet-name>
        <servlet-class>com.sunxiaping.HelloWorldServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldServletservlet-name>
        <url-pattern>/hellourl-pattern>
    servlet-mapping>
web-app>

1.3 Servlet的执行原理

  • ①当服务器接受到客户端浏览器的请求后,会解析请求的URL地址,获取访问的Servlet的资源路径。
  • ②查找web.xml文件,是否有对应的标签标签体内容。
  • ③ 如果有,则再找到对应的全类名。
  • ④Tomcat会将字节码文件加载进内存,并且创建其对象。
  • ⑤调用其方法。

 

第二章 Servlet的生命周期

2.1 创建

  • 方法:
public void init() throws ServletException {

}

 

  • 默认情况下,第一次访问的时候,会创建Servlet对象,并调用init()方法,说明Servlet是单例的。
  • 可以配置Servlet的创建时机:

<load-on-startup>5load-on-startup>

2.2 提供服务

  • 执行service方法,执行多次。
public abstract void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException;

2.3 销毁

  • 执行destroy方法,只执行一次。
  • 服务器正常关闭的时候,Servlet被销毁。
    public void destroy() {
    }

 

第三章 Servlet3.0 注解配置

3.1 Servlet 3.0的好处

  • 可以支持注解配置。可以不需要web.xml文件了。

3.2 步骤

  • 创建JavaEE项目,选择Servlet3.0以上版本,可以不创建web.xml。
  • 定义一个类,继承HttpServlet。
  • 复写方法。
  • 在类上使用@WebServlet注解,进行配置。

3.3 应用

  • 示例:
package com.sunxiaping.servlet;

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(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {

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


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

 

第四章 url-pattern的规则

4.1 一个Servlet可以定义多个访问路径

  • 示例:
package com.sunxiaping.servlet;

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(urlPatterns = {"/hello", "world"})
public class HelloServlet extends HttpServlet {

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


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

4.2 路径定义规则

4.2.1 路径匹配

  • 规则:
/xxx

 

  • 示例:
package com.sunxiaping.servlet;

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(urlPatterns = {"/hello", "world"})
public class HelloServlet extends HttpServlet {

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


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

4.2.2 多层路径

  • 规则:
/xxx/xxx

 

  • 示例:
package com.sunxiaping.servlet;

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(urlPatterns = {"/hello/world"})
public class HelloServlet extends HttpServlet {

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


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

4.2.3 扩展名匹配

  • 规则:
*.do 
*.action

 

  • 示例:
package com.sunxiaping.servlet;

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(urlPatterns = {"hello.do"})
public class HelloServlet extends HttpServlet {

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


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

 

你可能感兴趣的:(Servlet)