Tag Archives: width

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");

Android gets the height and width of the screen

Android gets the height and width of the screen and uses the WindowManager class

There are two methods

1、WindowManager wm = (WindowManager) getContext()
                    .getSystemService(Context.WINDOW_SERVICE);

     int width = wm.getDefaultDisplay().getWidth();
     int height = wm.getDefaultDisplay().getHeight();

2、WindowManager wm = this.getWindowManager();

     int width = wm.getDefaultDisplay().getWidth();
     int height = wm.getDefaultDisplay().getHeight();

Format control of cout cout.width () and cout.fill ()

When I was doing C++ high-precision template output today, I found that the high-precision template output used cout.width() and cout.fill() in order to output the four-digit Numbers stored in each unit
We looked for information about cout.width() and Cout.fill ()
About cout. Width () :

a, control int width() will be used to adjust the width of the field. Since width is a member function, it will be called by an object. For example, cout.width() will display the current field width, which defaults to 0, and cout.width(3) will set the field width to 3. Note how C++ accommodates fields by assigning them just the right width to accommodate fields, so the default width in C++ is 0 to accommodate all fields.
b, width default alignment is right, that is, if cout.width(12) if the field is not so wide, the left side of the field will be filled with Spaces to reach the width of 12 fields.
c. Also note that width only affects the next output after it is set. After the output of the next field, subsequent fields are restored to their default values, such as cout.width(12); cout< < 2< < 3; Output 2 will be displayed as the width of 12 fields, but 3 will be displayed as the default.
d, int width() will reverse the previous field width value when it is called.
About cout. The fill () :
The member function fill() can be used to change the fill characters, such as cout.fill(‘ *’), which fills in the blank with *.
But be warned: the fill function will remain in effect after it is set, unless it is reset. This is quite different from width(). Width only affects the next output after it is set. After the next output, the following fields are restored to the default value of 0.
And cout. The fill (‘ * ‘) returns the set ‘*’ before the fill characters’ so if cout< < Fill (‘*’) will print this space
So the output of the following program is:
123456789
****** 123 // (note the space between asterisks and Numbers)

#include <iostream>
using namespace std;

void main()
{
cout<<"123456789"<<endl;
cout.width(8);
cout<<cout.fill('*')<<123<<endl;
} 

C++ also has a setw() function similar to cout.width()
These two functions do the same thing. Both control the width of the output character field and are not filled with Spaces. In C++, setw(int n) is used to control the output interval.
for example: cout< < ‘s'< < setw(8)< < ‘a'< < endl;
is displayed on the screen with 7 Spaces between
s a
s and a, and setw() only affects the output immediately following it. In the above example, means ‘a’ occupies 8 positions, and the insufficient space is filled.
If the input exceeds the length set by setw(), the actual length is output.
setw() defaults to Spaces, which can be filled with other characters using setfill().
Such as cout. The fill (‘ * ‘); After cout< < ‘s’ & lt; < setw(8)< < ‘a'< < endl; Output S ******a with seven asterisks between S and A
When setw(n) is used to set the output width, the default is right alignment. If you want it to be left aligned, you just insert STD ::left, and again, right aligned just insert STD ::right, but right aligned is the default and you don’t have to declare it explicitly.
In c++, the following functions are interusable:
setw or width()
setfill or fill()
setprecision or precision()
where the preceding set* requires header #include< iomanip>
 
 
There are some other format controls, just to mention:
Ios ::dec in decimal notation integer
ios::hex in hexadecimal notation
ios::hex in hexadecimal notation
ios::hex in hexadecimal notation
setfill(*) set padding characters *
setprecision(n) set display decimal precision n
setw(n) set field width n characters
setiosflags(ios::fixed) fixed floating point display
The index of setiosflags(ios::scientific) indicates that
setiosflags(ios::left) is left-align
setiosflags(ios::right) right-align
setiosflags(ios::skipws) ignores the leading blank
setiosflags(ios::uppercase) and outputs
Setiosflags (ios:: Lowercase) lowercase hexadecimal number output
Ios ::dec represents integers in decimal
ios::hex represents integers in hexadecimal
ios::oct represents integers in decimal
ios::showbase adds a prefix for integers in decimal
ios::internal inserts a number of padding characters in the middle of the symbol bit and value to align both ends of the string
ios::left inserts padding characters at the end of the string to align the string to the left
The padding characters are inserted before the string to cause the string to come right aligned
ios::boolalpha will represent values of the bool type as true or flase, Instead of 1 or 0
the ios: : fixed will be treated as character points according to the common fixed point format (not scientific notation)
the ios: : scientific will be treated as character points according to the scientific notation (with index domain)
the ios: : showpoint in decimal floating-point said forced insert decimal point (the default is don’t show the decimal floating-point representation of the integer)
the ios: : showpos forced to add in front of the positive number +
the ios: : skipws Empty the cache
in ios::unitbuf after an insert (each output) operation