servlet

Posted mm163

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet相关的知识,希望对你有一定的参考价值。

1.实现httpservlet

@WebServlet("/some")
public class SomeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("some Servlet");
    }
}

2.

@SpringBootApplication
@ServletComponentScan("com.abc.servlet")  // 指定要扫描Servlet的包
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

----------------------------------------------------------------------------------------------------------------------------

          2.5

public class SomeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("some Servlet");
    }
}

 

在配置类中写

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    // 基于JavaConfig方式:适用于Servlet2.5+
    // 基于注解方式:适用于Servlet3.0+
    @Bean
    public ServletRegistrationBean<SomeServlet> getServlet() {
        return new ServletRegistrationBean<>(new SomeServlet(), "/some");
    }

}

 

以上是关于servlet的主要内容,如果未能解决你的问题,请参考以下文章

servlet和filter的区别

Java基础——JSP

java---servlet与filter的联系与区别

servlet,filter,listener,intercepter区别

Tomcat根据JSP生成Servlet机制解析

servlet,过滤器,监听器,拦截器的区别