Sobes.tech
Middle

Springda qanday servlet yaratish mumkin?

sobes.tech AI

AIdan javob

Spring'da, bir servlet'i birkaç yolla yaratmaq mümkündür:

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

    // `FrameworkServlet`-dən irəli gələn sadə servlet nümunəsi
    // Əsl tətbiqdə adətən daha çox konfiqurasiya tələb olunur
    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("MySpringServlet-dən salam!");
        }
    }
    
  2. Əl ilə qeydiyyat @BeanServletRegistrationBean istifadə edərək: Spring konfiqurasiyasında (adətən @Configuration ilə işarələnmiş sinifdə) servlet nümunəsi yarada və onu ServletRegistrationBean-ə sarıya bilərsiniz. Bu, URL xəritələşdirmə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("Bu, xüsusi servlet!");
            }
        }
    }
    
  3. Spring Boot-da standart servlet mexanizmlərindən istifadə (@ServletComponentScan): Spring Boot-da, əsas tətbiq sinifinə (@SpringBootApplication) və ya hər hansı konfiqurasiya sinifinə @ServletComponentScan əlavə etsəniz, Spring Boot avtomatik olaraq paketləri skan edir və @WebServlet, @WebFilter, @WebListener kimi standart servlet anotasiya ilə işarələnmiş sinifləri qeydiyyata alır.

    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 // Servlet anotasiya skanını aktivləşdirir
    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ə seçimi ehtiyaclarınıza bağlıdır. Spring kontekstinə və onun imkanlarına (məsələn, DI) inteqrasiya üçün çox vaxt FrameworkServlet və ya ServletRegistrationBean istifadə olunur. Sadə hallarda və ya mövcud servletlərin köçürülməsi üçün @ServletComponentScan kifayət edə bilər. Çox vaxt Spring MVC-də əsas servlet DispatcherServlet-dir və Spring Boot-da avtomatik konfiqurasiya olunur.