您现在的位置是:主页 > news > wordpress忘记密码ftp/中山口碑seo推广

wordpress忘记密码ftp/中山口碑seo推广

admin2025/5/2 5:08:22news

简介wordpress忘记密码ftp,中山口碑seo推广,建站必须要域名吗,企业查询系统官网河北Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。 当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSe…

wordpress忘记密码ftp,中山口碑seo推广,建站必须要域名吗,企业查询系统官网河北Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。 当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSe…

Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。

当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 
Spring boot 的主 Servlet 为DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。 
一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和ServletListenerRegistrationBean 获得控制。 
也可以通过实现 ServletContextInitializer 接口直接注册。

二、在SpringBootApplication 上使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

1.通过代码注册Servlet示例代码:

 

package me.shijunjie.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class MyServlet1 extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>HelloWorld</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>这是:MyServlet1</h1>"); out.println("</body>"); out.println("</html>");}
}
package me.shijunjie.testspringboot2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;import me.shijunjie.servlet.MyServlet1;

@SpringBootApplication
public class App 
{/*** 注册Servlet.不需要添加注解:@ServletComponentScan* @return*/@Beanpublic ServletRegistrationBean myServlet1(){return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");}public static void main( String[] args ){SpringApplication.run(App.class, args);}
}

测试

2.使用注解注册Servlet示例代码 添加@WebServlet注解

package me.shijunjie.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet(urlPatterns="/myServlet2/*",description="Servlet的说明")
public class MyServlet2 extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>HelloWorld</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>这是:MyServlet2</h1>"); out.println("</body>"); out.println("</html>");}
}

添加@ServletComponentScan注解

package me.shijunjie.testspringboot2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;/*** Hello world!**/
@SpringBootApplication
@ServletComponentScan("me.shijunjie.servlet")
public class App 
{/* *//*** 注册Servlet.不需要添加注解:@ServletComponentScan* @return*//*@Beanpublic ServletRegistrationBean myServlet1(){return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");}*/public static void main( String[] args ){SpringApplication.run(App.class, args);}
}

测试

 

转载于:https://www.cnblogs.com/s648667069/p/6489939.html