-1

I'm trying to convert a class written in Java 1.8 to Java 1.4. This is an older Java application but I've never coded in Java 1.4 before.

Here is the Java 1.8 code:

public class RequestContext implements Filter {

    private static final ThreadLocal<HttpServletRequest> REQUEST_HOLDER = new ThreadLocal<HttpServletRequest>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {

        if (servletRequest instanceof HttpServletRequest) {
            REQUEST_HOLDER.set((HttpServletRequest) servletRequest);
        }

        try {
            chain.doFilter(servletRequest, servletResponse);
        }
        finally {
            REQUEST_HOLDER.remove(); // Clean up the ThreadLocal
        }
    }

    /**
     * @return the HttpServletRequest for the current thread.
     */
    public static HttpServletRequest getRequest() {
        return REQUEST_HOLDER.get();
    }

    @Override
    public void destroy() {

    }

}

How do I convert the above to Java 1.4?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 1
    I'd recommend downloading and reviewing the [Java 1.4 SDK documentation](https://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-142docs-2045554.html) and going line by line, based on compiler errors. – Zephyr Jun 17 '19 at 15:18
  • 1
    What is the version of Java EE and Servlet spec that you are targeting with Java 1.4? – Karol Dowbecki Jun 17 '19 at 15:21
  • 4
    Why would you want to? Who exactly told you that investing time to backport to a java version that had end of life 2008? – GhostCat Jun 17 '19 at 15:31
  • 1
    Why do you care? This is an older application that runs on 1.4. – Jolly Khanna Jun 19 '19 at 19:07
  • I'm surprised that nobody has explicitly mentioned setting the **-source** level for compilation to 1.4 (`javac -source 1.4 etc...`). Any use of features implemented after 1.4 will then be flagged by the compiler. Which IDE are you using? – skomisa Jul 05 '19 at 19:42

1 Answers1

0

You most likely have to target Servlet 2.3 specification or newer as it introduced Filter. As per docs:

Filters are a new feature in the Java servlet API for version 2.3.

It looks like that you have to remove generics as they were introduced in Java 1.5. ThreadLocal is available from Java 1.2, although remove() method was introduced in 1.5. The code should probably look like:

public class RequestContext implements Filter {

    private static final ThreadLocal REQUEST_HOLDER = new ThreadLocal();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest) {
            REQUEST_HOLDER.set(servletRequest);
        }
        try {
            chain.doFilter(servletRequest, servletResponse);
        } finally {
            REQUEST_HOLDER.set(null);
        }
    }

    /**
     * @return the HttpServletRequest for the current thread.
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) REQUEST_HOLDER.get();
    }

    @Override
    public void destroy() {

    }

}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111