首先,我們寫一個Servlet。要求就是簡單的列印一句話。
在MyServlet這個類別的上方使用 @WebServlet 註解來建立Servlet即可。
package com.songzihao.springboot.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * */ @WebServlet(urlPatterns = "/myservlet") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("My SpringBoot Servlet-1"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
之後在SpringBoot專案的入口類別上方使用註解 @ServletComponentScan 註解來掃描Servlet中的註解即可。
package com.songzihao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //开启spring配置 @ServletComponentScan(basePackages = "com.songzihao.springboot.servlet") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
最後啟動測試。
還是先寫一個 Servlet。這次不使用註解。
package com.songzihao.springboot.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * */ public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("My SpringBoot Servlet-2"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
然後再寫一個設定類別! ! !
這個類別的上方使用 @Configuration 註解,表名該類是一個設定類,相當於之前的各種xml設定檔。
在類別中的方法上方使用 @Bean 註解,ServletRegistrationBean 這相當於是一個Servlet註冊類,類似於先前的
package com.songzihao.springboot.config; import com.songzihao.springboot.servlet.MyServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * */ @Configuration //该注解将此类定义为一个配置类(相当于一个xml配置文件) public class ServletConfig { /** * @Bean 是一个方法级别上的注解,主要用在配置类里 * 相当于一个 <beans> * <bean id="..." class="..." /> * </beans> * @return */ @Bean public ServletRegistrationBean myServletRegistrationBean() { ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean( new MyServlet(),"/myservlet" ); return servletRegistrationBean; } }
最後啟動測試。
package com.songzihao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }#
以上是SpringBoot中如何使用Servlet的詳細內容。更多資訊請關注PHP中文網其他相關文章!