• 0

HttpServletResponse is missing


Question

I have a Java Spring Boot project that uses React as a front end. In order to allow web crawlers to index the site I (with the help of examples found by googling) decided to implement a HandlerInterceptorAdapter where I would match the requested URI and redirect to my default controller if needed. I got stuck very quickly though. This is my entire interceptor as of now:

 

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.catalina.servlet4preview.http.HttpServletResponse;
import org.apache.catalina.servlet4preview.http.ServletMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class Interceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        ServletMapping mapping = request.getServletMapping();
        String uri = request.getRequestURI();
        System.out.println("uri:"+uri+"; servletName:"+mapping.getServletName()+"; class:"+mapping.getClass());
        return true;
    }
}

As you can see I am trying to make it compile at least. The second argument of preHandle() expects HttpServletResponse which needs to be imported from org.apache.catalina.servlet4preview.http. The one from javax.servlet.http doesn't count. Right now HttpServletResponse doesn't exist inside the catalina package.

To make things worse there is not logical connections between the import statements and the artifact ids. I can only guess. I have in project's pom.xml dependencies for tomcat-servlet-api and org.apache.catalina. Catalina cannot even be resolved (even though I have https://mvnrepository.com set as a secondary repository)

 

        <dependency>
            <groupId>org.apache.catalina</groupId>
            <artifactId>catalina</artifactId>
            <version>6.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>7.0.52</version>
            <scope>provided</scope>
        </dependency>

 

P.S. This project is to be deployed to a Tomcat server, so I need to use Catalina/Tomcat variants of the libraries

Link to comment
https://www.neowin.net/forum/topic/1393373-httpservletresponse-is-missing/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Ok, this is my mistake - even though the project goes to a standalone Tomcat server that doesn't mean that Tomcat/Catalina specific imports need to be used. javax.servlet.http package is to be used. Going like this the code compiles fine

 

 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class DefaultController extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        String uri = request.getRequestURI();
        System.out.println("uri:"+uri+"; servletPath:"+request.getServletPath()+"; class:"+request.getClass());
        return true;
    }
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now