[Problem] common JSP garbled solutions and basic knowledge of JavaBean

Source: Internet
Author: User

http://bbs.csdn.net/topics/70409966

First declare the article from Java programmers from stupid birds to cainiao (23)
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. In fact, there are many causes of the garbled code problem, and there are also many problems to solve the garbled code problem. Now let's look at the following:
Garbled characters can be roughly divided into the following three types:
1. Parameters for passing parameters between two JSP pages on the JSP page 3. There are roughly three solutions for accessing data in the database:
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 data warehouse 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 transmitted parameters are encoded into UTF-8, the disadvantage of this is that it is very troublesome to write parameters every time a token is sent.
You can also set the server. xml configuration file.
<Connector Port = "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 filters. The principles are similar, 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 = 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. There is also a common garbled problem in listing, that is, file name garbled during download.
The original code for processing the download is as follows: Response. setheader ("content-disposition", "attachment; filename =" + java.net. url encoder. 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 a variety of Java languages. 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 disruptive 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, "secondary 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 in <JSP: Use bean scope = "...> To the right of the flag. A quick reference for generating ⼀. 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. The form takes a long time to process user input. It is usually used for the page to accept HTTP/pos t 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 Beans: In theory, JavaBean will not produce any HTML, because this is the work of the JSP layer; however, it is very useful to provide some pre-prepared lattice for Dynamic messages. The generated HTML returns the labeled JavaBean method.

Also share a forum post:

Http://bbs.csdn.net/topics/70409966

Related Article

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.