Sobes.tech
Middle

Spring-də necə servlet yaratmaq olar?

sobes.tech Süni İntellekt

AI-dan cavab

Spring-də servlet yaratmaq üçün bir neçə üsul mövcuddur:

  1. HttpServletBean və ya FrameworkServlet istifadə edərək: Spring, HttpServlet-i genişləndirən və Spring kontekstinə inteqrasiyanı sadələşdirən abstrakt siniflər təqdim edir. FrameworkServlet daha ümumi olub, bir çox Spring veb framework-ü üçün istifadə olunur (məsələn, DispatcherServlet).

    // Sadə `FrameworkServlet`-i genişləndirən servlet nümunəsi
    // Əsl tətbiqdə daha çox konfiqurasiya tələb oluna bilər
    public class MySpringServlet extends org.springframework.web.servlet.FrameworkServlet {
    
        @Override
        protected void doService(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            // Sorğu emalı loqikası
            response.getWriter().write("Hello from MySpringServlet!");
        }
    }
    
  2. @BeanServletRegistrationBean istifadə edərək əl ilə qeydiyyat: Spring konfiqurasiyasında (adətən @Configuration ilə işarələnmiş sinifdə) servlet nümunəsi yarada və onu ServletRegistrationBean-ə bükə bilərsiniz. Bu, URL xəritələməsini və servlet parametrlərini elastik şəkildə tənzimləməyə imkan verir.

    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Configuration
    public class ServletConfig {
    
        @Bean
        public ServletRegistrationBean<HttpServlet> myCustomServletRegistration() {
            ServletRegistrationBean<HttpServlet> registration = new ServletRegistrationBean<>(new MyCustomServlet());
            registration.addUrlMappings("/custom-servlet/*"); // URL xəritəsi
            registration.setLoadOnStartup(1); // Başlanğıcda yükləmə
            return registration;
        }
    
        // Xüsusi servlet nümunəsi
        public static class MyCustomServlet extends HttpServlet {
            @Override
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
                response.getWriter().write("This is a custom servlet!");
            }
        }
    }
    
  3. Spring Boot-da standart servlet mexanizmlərindən istifadə (@ServletComponentScan): Spring Boot-da, əgər əsas tətbiq sinfinə və ya konfiqurasiya sinfinə @ServletComponentScan əlavə etsəniz, Spring Boot avtomatik olaraq @WebServlet, @WebFilter, @WebListener kimi standart servlet anotasiya ilə işarələnmiş sinifləri skan edir və qeydiyyatdan keçirir.

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @SpringBootApplication
    @ServletComponentScan // Standart servlet anotasiya skanını aktiv edir
    public class MySpringBootApp {
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApp.class, args);
        }
    
        // Avtomatik qeydiyyatdan keçəcək servlet nümunəsi
        @WebServlet("/auto-servlet")
        public static class AutoRegisteredServlet extends HttpServlet {
            @Override
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
                response.getWriter().write("Avtomatik qeydiyyatdan keçmiş servlet!");
            }
        }
    }
    

Yöntəm seçimi ehtiyaclarınızdan asılıdır. Spring kontekstinə inteqrasiya və DI kimi imkanlar üçün FrameworkServlet və ya ServletRegistrationBean vasitəsilə qeydiyyat istifadə olunur. Sadə hallarda və ya mövcud servletlərin köçürülməsi üçün @ServletComponentScan uyğundur. Əksər Spring MVC veb tətbiqlərində əsas servlet DispatcherServlet-dir və avtomatik konfiqurasiya olunur.