Solution of socket write error caused by pressing F5 to refresh page by ehcache user

Method 1

When using ehcache as the page cache of a website, you may encounter a problem: when the user presses F5 to refresh the page continuously, the following error will be generated in the output log of Tomcat:

ClientAbortException:  java.net.SocketException: Software caused connection abort: socket write error
......
Caused by: java.net.SocketException: Software caused connection abort: socket write error

In a word, socket write error error

In fact, this is because the user keeps making requests when pressing F5, but each request is not complete. The user makes a request and immediately interrupts it. On the server side, because ehcache caches the page, ehcache is responsible for outputting the cached content to the user. Let’s look at the source code of cachingfilter of ehcache, in which the writecontent method is responsible for outputting content to users (of course, generally we choose to configure net.sf.ehcache.structures.web.filter.simplepagecachingfilter , simplepagecachingfilter inherits the cachingfilter class)

protected void writeContent(final HttpServletRequest request,  final HttpServletResponse response, final PageInfo pageInfo)
    throws IOException, ResponseHeadersNotModifiableException {
    byte[] body;
    boolean shouldBodyBeZero = ResponseUtil.shouldBodyBeZero(request, pageInfo.getStatusCode());
    if (shouldBodyBeZero) {
        body = new byte[0];
    } else if (acceptsGzipEncoding(request)) {
        body = pageInfo.getGzippedBody();
        if (ResponseUtil.shouldGzippedBodyBeZero(body, request)) {
            body = new byte[0];
        } else {
            ResponseUtil.addGzipHeader(response);
        }
    } else {
        body = pageInfo.getUngzippedBody();
    }
    response.setContentLength(body.length);
    OutputStream out = new BufferedOutputStream(response.getOutputStream());
    out.write(body);
    out.flush(); 
}

Here, the out. Flush () method is responsible for the final refresh of the output content. Obviously, if the user interrupts the request when the server sends data to the user, the server will generate an error of clientabortexception: java.net.socketexception: software caused connection above: socket write error .

Of course, this is a reasonable mechanism. However, if the background has been outputting a lot of error log information, it is not what we want. So, how to solve this problem?

Here, we can implement our own filter class by inheriting net.sf.ehcache.structures.web.filter.simplepagecacheingfilter :

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.constructs.web.PageInfo;
import net.sf.ehcache.constructs.web.ResponseHeadersNotModifiableException;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;

public class MySimplePageCachingFilter extends SimplePageCachingFilter {
	protected void writeContent(final HttpServletRequest request, final HttpServletResponse response, final PageInfo pageInfo) throws IOException, ResponseHeadersNotModifiableException {
		try {
			super.writeContent(request, response, pageInfo);
		} catch (IOException e) {
			System.out.println("Visits are too frequent!");
		}
	}
}

Here, we rewrite the writecontent method and use try catch to process this method. At this time, we can ensure that a large amount of socket write error log information does not appear in Tomcat log records.

Method 2

httpServletResponse.setHeader("Content-Type","text/html;charset=UTF-8");

Read More: