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:
- Caused by: java.net.SocketException: Software caused connection abort: socket write error
- How to Solve Show() error caused by empty data
- [Solved] sqoop Error: jSQLException in nextKeyValue Caused by: ORA-24920:column size too large for client
- Kettle Error Caused by: java.lang.ArrayIndexOutOfBoundsException
- [Solved] Android mediaplayer.prepare() Error: Caused by: java.lang.IllegalStateException
- ValueError: Negative dimension size caused by subtracting 2 from 1 for…
- [Solved] hive Caused by: MetaException(message:Version information not found in metastore. )
- [Solved] Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
- [Solved] Caused by: java.lang.ClassNotFoundException: org.apache.flink.api.common.typeinfo.TypeInformation
- The problem of error reporting caused by fluent swift
- [Solved] Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no furthe
- [Solved] Redisson distributed lock error: attempt to unlock lock, not locked by current thread by node id
- [Solved] thrift Service Error: socket write error
- How to Solve Docker Run Error: standard_init_linux.go:219: exec user process caused: exec format error
- [Solved] standard_init_linux.go:190: exec user process caused “exec format error“
- Docker Create tomcat Error standard_init_linux.go:211: exec user process caused “no such file or directory”
- [Solved] ZYNQ Programs Error: memory write error at 0x100000.APB AP transaction error,DAP status f0000021
- [Go] Solve the garbled content sent by go-smtp and the unparsed html mail sent
- Git Pull error: Your local changes to the following files would be overwritten by merge:
- About completionfailedexception: error reported by compilation failed to complete