您好,欢迎访问一九零五行业门户网

SpringBoot中如何使用Servlet

1.方式一(使用注解)首先,我们写一个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); }}
最后启动测试。
2.方式二(定义配置类)仍然是先写一个 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注册类,类似于之前的 <servlet>、<servlet-mapping> 标签的作用。
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; @springbootapplicationpublic class application { public static void main(string[] args) { springapplication.run(application.class, args); }}
以上就是springboot中如何使用servlet的详细内容。
其它类似信息

推荐信息