Springboot--web application development-servlets, Filters, listeners

Source: Internet
Author: User

I. Web development using a Controller can basically accomplish most of the requirements, but we may also use Servlets, Filter, listener, and so on.

Two. Three ways to implement in spring boot

Method one: Gain control by registering Servletregistrationbean, Filterregistrationbean, and Servletlistenerregistrationbean

Servlet class:

package Com.demo.example.util.servlet;import Java.io.IOException;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpservletrequest;public class CustomServlet Extends HttpServlet {/** * */private static final long serialversionuid = 1L; @Overrideprotected void Doget (httpservletreq Uest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method StubSystem.out.println ("servlet Get Method");d Opost (request, response);} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method StubSystem.out.println ("Servlet get Method"); Response.getwriter (). Write (" Hello world ");}} 

Filter Class:
Package Com.demo.example.util.filter;import Java.io.ioexception;import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;public class Custonfilter implements Filter {@ Overridepublic void Destroy () {//TODO auto-generated method StubSystem.out.println ("Do Destroy");} @Overridepublic void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {System.out.println ("do Filter"); Chain.dofilter (request, response);} @Overridepublic void init (Filterconfig arg0) throws Servletexception {//TODO auto-generated method StubSystem.out.println ("do init");}}

Listener class:

Package Com.demo.example.util.listener;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;public class Customlistener implements Servletcontextlistener {@Overridepublic void contextdestroyed (Servletcontextevent servletcontextevent) {//TODO auto-generated method StubSystem.out.println ( "Servletcontextevent");} @Overridepublic void contextinitialized (Servletcontextevent servletcontextevent) {//TODO auto-generated method StubSystem.out.println ("Servletcontextevent");}}

 

Registering beans (First way)

Code registration: (in the Springbootdemofilerservletlisapplication Class)

@Beanpublic servletregistrationbean<customservlet> Servletregistrationbean () {return new Servletregistrationbean<customservlet> (New Customservlet (), "/customservlet");} @Beanpublic filterregistrationbean<custonfilter> Filterregistrationbean () {return new Filterregistrationbean <CustonFilter> (New Custonfilter (), Servletregistrationbean ());} @Beanpublic servletlistenerregistrationbean<customlistener> Servletlistenerregistrationbean () {return new Servletlistenerregistrationbean<customlistener> (New Customlistener ());}

Registration of the second code: (similar to one)

@SpringBootApplicationpublic class Springbootdemofilerservletlisapplication implements Servletcontainerinitializer {@Overridepublic void Onstartup (set<class<?>> arg0, ServletContext ServletContext) throws servletexception {//TODO auto-generated method Stubservletcontext.addservlet ("Customservlet", New Customservlet ()). Addmapping ("/customservlet"); Servletcontext.addfilter ("Custonfilter", New Custonfilter ()). Addmappingforservletnames (Enumset.of (Dispatchertype.request), True, "Customservlet"); Servletcontext.addlistener ( New Customlistener ());}

The third type is based on comments:

First: Add in the Springbootdemofilerservletlisapplication class

In listener

In the servlet

On filter ("/*" for filtering global)



Springboot--web application development-servlets, Filters, listeners

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.