程序员社区

【Servlet-2】自定义GenericServlet,HttpServlet

在这里插入图片描述

文章目录

      • 1.自定义GenericServlet类
      • 2.自定义HttpServlet类

1.自定义GenericServlet类

GenericServlet类实现Servlet方法:

package GenericServlet;

import javax.servlet.*;
import java.io.IOException;

//缺省适配器设计模式,Servlet里面的方法空实现
public class GenericServlet  implements Servlet {
    private ServletConfig servletConfig;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig=servletConfig;
        //想初始化的时候就执行代码
        init();
    }
    public void init(){
    }

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

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

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

    @Override
    public void destroy() {
    }
    public ServletContext getServletContext(){
        return servletConfig.getServletContext();
    }
}

GenericDemo1类继承GenericServlet类:这时不需要再重写Servlet类中的所有抽象方法了,只需要重写service方法就可以了。

package GenericServlet;

import javax.servlet.*;
import javax.servlet.GenericServlet;
import java.io.IOException;

public class GenericDemo1 extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        ServletContext servletContext = servletConfig.getServletContext();
    }
    @Override
    public void init(){
        System.out.println("------------");
    }
}

2.自定义HttpServlet类

package GenericServlet;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/HttpServlet")
public class HttpServlet extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        this.service(request,response);

    }
    public void service(HttpServletRequest request,HttpServletResponse response){
        //获取请求的提交方式
        String method = request.getMethod();
        System.out.println(method);
        if("POST".equals(method)){
            doPost(request,response);
        }else if("GET".equals(method)){
            doGet(request,response);
        }
    }

    private void doGet(HttpServletRequest request, HttpServletResponse response) {
    }

    private void doPost(HttpServletRequest request, HttpServletResponse response) {
    }
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【Servlet-2】自定义GenericServlet,HttpServlet

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区