• Servlet创建的三种方式


    关于servlet的创建,我们有三种方式。

    • 实现Servlet接口

    • 继承GenericServlet

    • 继承HttpServlet

    1 实现Servlet接口

    实现五个抽象方法

    package com.zjw;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class MyServlet implements Servlet {
    
        public MyServlet() {
            System.out.println("MyServlet构造方法....");
        }
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            System.out.println("init....");
        }
    
        @Override
        public ServletConfig getServletConfig() {
            System.out.println("getServletConfig....");
            return null;
        }
    
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            System.out.println("service....");
        }
    
        @Override
        public String getServletInfo() {
            System.out.println("getServletInfo....");
            return null;
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy....");
        }
    }
    

    2 继承GenericServlet

    实现service抽象方法

    package com.zjw;
    
    import javax.servlet.GenericServlet;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import java.io.IOException;
    
    public class GenServlet extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            System.out.println("哈哈哈");
        }
    }
    
    

    3 继承HttpServlet

    需要重写doGet、doPost方法,否则请求不被允许。

    package com.zjw;
    
    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 HttpsServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("这个是get请求");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("这个是post请求");
        }
    }
    
    

    4 web.xml配置

    <?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">
        <servlet>
            <servlet-name>my</servlet-name>
            <servlet-class>com.zjw.MyServlet</servlet-class>
        </servlet>
        <servlet>
            <servlet-name>gs</servlet-name>
            <servlet-class>com.zjw.GenServlet</servlet-class>
        </servlet>
        <servlet>
            <servlet-name>hs</servlet-name>
            <servlet-class>com.zjw.HttpsServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>my</servlet-name>
            <url-pattern>/myservlet</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>gs</servlet-name>
            <url-pattern>/gs</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>hs</servlet-name>
            <url-pattern>/hs</url-pattern>
        </servlet-mapping>
    </web-app>
    
    --------------- 我每一次回头,都感觉自己不够努力,所以我不再回头。 ---------------
  • 相关阅读:
    面试汇总-待整理
    javaWeb2之Jsp
    Spring常用注解
    消费者、生产者模型
    网络知识之http请求
    设置Centos时间
    安装hive
    在Ubuntu上安装mysql(5.17.19)
    rabbitmq学习——主题路由
    rabbitmq学习——routingkey
  • 原文地址:https://www.cnblogs.com/zjw-blog/p/13890534.html
Copyright © 2020-2023  润新知