Basic knowledge of JavaBean and common garbled code solutions

Basic knowledge of JavaBean and common garbled code solutions

The garbled code problem should be a problem encountered by java web developers. This problem also affected my idea of learning java at that time, and even wanted to give up. There was no way. I was young at that time, ha ha. In fact, there are many reasons for the problem of garbled code, and there are many ways to solve the problem of garbled code. Let's take a look at it now:

There are three types of garbled Codes:

Parameters transferred between JSP pages and data access in database

There are three solutions:

1 appears in the JSP page because the Chinese character code of the JSP page is not set. 2. The reason why the parameters are passed between JSP pages is that the correct character code is not set for the parameters. 3 if the above two problems are solved, there will be no garbled code when stored in the database. Unless you encode the data stored in the database again.

Specific solutions:

1. Set the character code as UTF-8 at the head of the form page

In addition, this sentence solves the display of Chinese garbled code in JSP pages. The HTML file output to the client after Tomcat compilation is not encoded in Chinese, so it will lead to garbled code.

2. Set the code of page request and response:

Add this sentence to solve the Chinese parameter transfer garbled code in the JSP page. Set the default encoding used by the browser to "UTF-8" to send request parameters.

3.String(request.getParameter("name"). getBytes("ISO8859_1"),"utf-8"); The meaning of this sentence is to encode and convert all the transmitted parameters into UTF-8. The disadvantage of this is that it is very troublesome to write one parameter every time.

You can also set server XML configuration file.

But this applies to the whole webapp.

4. You can also modify the web XML file to configure a filter. The principle is the same, just another way

1. Write filter class:

2. On the web XML configuration

5. Another common garbled problem is that the file name is garbled during downloading

The original code for processing the download is as follows:

response. setHeader("Content-Disposition","attachment; filename=" + java.NET.URLEncoder.encode(fileName,"UTF-8")); The urlencoder class contains static methods that convert strings to application / x-www-form-urlencoded mime format.

Urldecoder the urldecoder class corresponding to the urlencoder class has two static methods. They decode strings encoded in the form of x-www-form-url-encoded. That is, they convert all plus signs (+) into space characters and all% XX into corresponding characters:

JavaBean is a reusable component written in Java language. To be written as a JavaBean, a class must be concrete, public, and have a parameterless constructor. JavaBeans expose internal domains to member properties by providing public methods that conform to a consistent design pattern. As we all know, attribute names conform to this pattern, and other Java classes can discover and manipulate these JavaBean attributes through their own mechanism.

The task of JavaBean is "write once, run anywhere, reuse everywhere", that is, "write once, execute anywhere, reuse anywhere". This is actually to solve the increasing complexity perplexing the software industry and provide a simple, compact and excellent problem solution.

The scope of a JavaBean is a variable with a lifetime. The scope of JavaBean is < jsp: usebean scope = "... > It is indicated on the right side of the sign. A quick reference to the JavaBean will be generated. Note: the JSP server engine will split < JSP.... Mark. And the actual code cannot be displayed on the end user's browser.   

There are four ranges: page, request, conversation session and application.

Dialogue scope:

Conversation scoped JavaBeans are mainly used across multiple pages and time periods: for example, populating user information. Add information and receive feedback, and save the track of the user's recently executed page. The conversation scope JavaBean retains some information related to the user's conversation ID. This information comes from a temporary conversation cookie, which will be deleted from the client and server when the user closes the browser.

Page / request scope:

Page and request scoped JavaBeans are sometimes similar to form beans because they are mostly used to process forms. Forms take a long time to process user input, usually used for pages to accept http / post or get requests. In addition, page and request scope beans can be used to reduce the load on large site servers. If dialog beans are used, the delayed processing may consume a lot of resources.

Application:

The application scope is usually applied to the components of the server, such as JDBC connection pool, application monitoring, user counting and other classes involved in user behavior. Restrict the generation of HTML in beans: in theory, JavaBeans will not generate any HTML, because this is the responsibility of the JSP layer; However, it is useful to provide some pre prepared formats for dynamic messages. The generated HTML will be returned by the annotated JavaBean method.  

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>