banner



How To Register A Filter For Dispatcher Servlet In Spring

There's no reasons, we cannot use Java Servlet components along with Spring MVC.

Spring MVC is itself based on Java Servlet (DispatcherServlet) and runs in a Servlet container.

We can annals other Java Servlet components via web.xml or past using annotations

Let's see an example how to use a Servlet and a Filter in Spring MVC.

Instance

A Spring Controller

@Controller @RequestMapping("/app") public class MyController {      @RequestMapping   @ResponseBody   public String handleRequest () {       System.out.println("-- treatment request in controller --");       return "dummy response from MyController";   } }

A Coffee Servlet

@WebServlet(name = "myServlet", urlPatterns = "/app2") public class MyServlet extends HttpServlet {    @Override   protected void doGet (HttpServletRequest req,                         HttpServletResponse resp)             throws ServletException, IOException {          System.out.println("-- In MyServlet --");       PrintWriter writer = resp.getWriter();       writer.println("dummy response from MyServlet");   } }

A Servlet Filter

@WebFilter(filterName = "myFilter", urlPatterns = "/*") public grade MyFilter implements Filter {      @Override   public void init (FilterConfig filterConfig) throws ServletException {   }      @Override   public void doFilter (ServletRequest request, ServletResponse response,                         FilterChain concatenation)             throws IOException, ServletException {       System.out.println("-- In MyFilter --");       HttpServletRequest req = (HttpServletRequest) request;       System.out.println("URI: " + req.getRequestURI());       chain.doFilter(request, response);   }      @Override   public void destroy () {   } }

Coffee Config form

@EnableWebMvc @Configuration @ComponentScan public grade MyWebConfig { }

DispatcherServlet initializer

public grade AppInitializer extends         AbstractAnnotationConfigDispatcherServletInitializer {      @Override   protected Form<?>[] getRootConfigClasses () {       return null;   }      @Override   protected Class<?>[] getServletConfigClasses () {       return new Class<?>[]{MyWebConfig.form};   }    @Override   protected String[] getServletMappings () {       return new Cord[]{"/"};   } }

To endeavor examples, run embedded tomcat (configured in pom.xml of example projection beneath):

mvn tomcat7:run-war

Output

Accessing Leap Controller at /app

Output on server console:

          -- In MyFilter --          URI: /app          -- handling request in controller --        

Notation that MyFilter is also working every bit expected.


Accessing Servlet at /app2

Output on server console:

          -- In MyFilter --          URI: /app2          -- In MyServlet --        

Other Options

Using HttpRequestHandler

An implementation of HttpRequestHandler tin be thought as a mini Servlet which runs within Spring MVC's DispatcherServlet. See an example here.

Using Filter as Spring bean with DelegatingFilterProxy

Run across an instance in the next tutorial.

Using Servlet components in Jump Kicking

Check this tutorial out.

Instance Project

Dependencies and Technologies Used:

  • spring-webmvc iv.iii.9.RELEASE: Spring Web MVC.
  • spring-exam 4.3.9.RELEASE: Spring TestContext Framework.
  • javax.servlet-api 3.one.0 Coffee Servlet API
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.three.9

How To Register A Filter For Dispatcher Servlet In Spring,

Source: https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/servlet-components.html

Posted by: monahanroperrin.blogspot.com

0 Response to "How To Register A Filter For Dispatcher Servlet In Spring"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel