Using pure servlets (Java Servlet, no framework) in a project using the Spring framework?

懵懂的女人 提交于 2020-01-15 23:34:13

问题


We've got some Pure Servlets (pure Java classes following the Servlet API, no framework) that we'd like to include in a project that relies heavily on the Spring Framework.

What's the best way of including these servlets in the project, when all the new code we're writing is making heavy use of Spring 3 features?


回答1:


your servlet container can run multiple servlets, spring is just one of them. why not just include your servlets in the web.xml and see if it works? it should work. spring is not that intrusive, yet (but obviously it already intruded the minds of many developers)




回答2:


If you declare servlets in the web.xml, alongside the Spring front controller, it most certainly will work.

You just have to be careful when you declare which URLs map to the servlets. If you send "/*" to the Spring front controller, none of your requests will reach your other servlets. Be specific about what you need to send to each one.




回答3:


As you might know, servlets cannot be configured as Spring beans. If your question is about colloborating with spring beans from a servlet, do refer this thread and also this




回答4:


Spring provides a couple of classes to make this bridging easier.

ServletForwardingController

Spring Controller implementation that forwards to a named servlet, i.e. the "servlet-name" in web.xml rather than a URL path mapping. A target servlet doesn't even need a "servlet-mapping" in web.xml in the first place: A "servlet" declaration is sufficient.

Useful to invoke an existing servlet via Spring's dispatching infrastructure, for example to apply Spring HandlerInterceptors to its requests.

ServletWrappingController

Spring Controller implementation that wraps a servlet instance which it manages internally. Such a wrapped servlet is not known outside of this controller; its entire lifecycle is covered here (in contrast to ServletForwardingController).



来源:https://stackoverflow.com/questions/3258456/using-pure-servlets-java-servlet-no-framework-in-a-project-using-the-spring-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!