Java programmers from stupid birds to cainiao (23) Common garbled solutions and basic knowledge of JavaBean

Source: Internet
Author: User

The garbled problem should have been encountered by Java Web developers. At that time, it also affected my idea of learning Java and even gave up some ideas. At that time, I was young, haha. As a matter of fact, there are many causes of Garbled text, and there are also many problems to solve Garbled text. Now let's take a look at them one by one:

Garbled characters can be roughly divided into the following three types:

1. jsp page
2. Parameters for passing parameters between JSP pages
3. Access to data in the database
There are roughly three solutions:

1 appears on the JSP page because the Chinese character encoding of the JSP page is not set.
2. parameters are transmitted between JSP pages because the correct character encoding is not set for the parameters.
3. If the above two problems are solved, there will naturally be no garbled characters when they are stored in the database. Unless you encode the data stored in the database again.

Specific solution:

1. Set the character encoding to UTF-8 in the header of the form page.
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>

After Tomcat is compiled, the HTML file output to the client is not encoded in Chinese, so garbled characters are generated.


2. Set the encoding of the page request and response:

<% Request. setcharacterencoding ("UTF-8 ");

Response. setcharacterencoding ("UTF-8"); %>

Add this sentence to solve the Chinese parameter passing garbled characters on 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 ");This sentence means that all the transmitted parameters are encoded into UTF-8. The disadvantage of this statement is that every time a parameter is sent, It is very troublesome.

You can also set the server. xml configuration file.

< Connectorport ="8080" maxHttpHeaderSize ="8192" maxThreads ="150"  minSpareThreads ="25"  maxSpareThreads ="75" enableLookups ="false"  redirectPort ="8443"  acceptCount ="100" connectionTimeout ="20000"  disableUploadTimeout ="true"   URIEncoding ="utf-8"/> 

In this way, the application is applied to the entire webapp.

4. You can also modify the Web. xml file and configure a filter. The principles are the same, just in a different way.

1. Compile the filter class:

package org.RN.util;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class Encoding implements Filter {   @SuppressWarnings("unused")   private FilterConfig config=null;   String encoding=null; public void destroy() {  this.encoding=null;  this.config=null; } public void doFilter(ServletRequest request, ServletResponse response,   FilterChain chain) throws IOException, ServletException {     if(encoding!=null)      request.setCharacterEncoding(encoding);     chain.doFilter(request, response);      } public void init(FilterConfig arg0) throws ServletException {  this.config=arg0;  this.encoding=arg0.getInitParameter("encoding"); }}


2. Configure in Web. xml

<Filter> <description> cache filtering </description> <filter-Name> encoding </filter-Name> <filter-class> filter. encoding </filter-class> </filter> <filter-mapping> <filter-Name> encoding </filter-Name> <URL-pattern> * </url-pattern> </filter-mapping>
</filter>


5. Another common garbled problem is the file name garbled during download.

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 a static method to convert a string to the application/X-WWW-form-urlencoded MIME format.

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 to say, they convert all the plus signs (+) into space characters, and convert all % XX into corresponding characters respectively:

Javabean is a reusable component written in Java. The class must be specific and public and have a non-parameter constructor. JavaBean exposes the member attributes of an internal domain by providing a public method that complies with the consistent design pattern. As we all know, attribute names comply with this pattern. Other Java classes can discover and operate these an attributes through their own mechanisms.

The JavaBean task is: "write once, run anywhere, reuse everywhere", that is, "one-time writing, execution anywhere, reuse anywhere ". This is actually to solve the increasing complexity that troubles the software industry and provide a simple, compact, and excellent problem solution.

The scope of Javabean is a variable with a life time. The scope of Javabean is <JSP: usebean scope = "...> To the right of the flag. A quick reference for Javabean is generated. Note: The JSP server engine is stripped away from <JSP .... Mark. The actual Code cannot be displayed on the end user's browser.

There are four scopes: Page, request, dialog session, and application.

Dialog scope:

The JavaBean of the dialog scope is mainly used across multiple pages and time periods: for example, filling user information. Add information and receive feedback to save the trajectory of the user's recent execution page. The dialog scope JavaBean retains information related to the user dialog ID. This information comes from a temporary dialog cookie, which will be deleted from the client and server when the user closes the browser.

Page/request range:

Page and request range JavaBean are sometimes similar to form beans because most of them are used to process forms. A form takes a long time to process user input. It is usually used for pages to accept HTTP/post or GET requests. In addition, the page and request range beans can be used to reduce the load on large site servers. If you use a conversation bean, delayed processing may consume a lot of resources.

Application:

Application Scope is usually applied to server components, such as JDBC connection pool, application monitoring, user count, and other classes that participate in user behavior. Limit the generation of HTML in Bean: In theory, JavaBean will not generate any HTML, because this is the work of the JSP layer; however, it is very useful to provide some pre-prepared formats for Dynamic messages. The generated HTML returns the labeled JavaBean method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.