Foreword
1. Why do you want to discuss?
Simply, it is the technology used to develop web applications. There are currently a lot of discussion for discussion, but I want to discuss the horizontal aspect, that is, how to put various kinds in web application development. The technique is combined to improve the development efficiency. If you use more popular words, don't talk about the most advanced, most useful.
2. What includes what?
Web development technology consists of three levels:
a) Display layer. Page production, this is usually done by art
b) Logic layer. The main discussion, including Struts, Hibernate, Mode, Code Generation Technology (XDoclet, MiddleGen), Automatic Build (Maven) Taglib
c) Resource layer. Discuss the design, MSSQL, Oracle, stored procedures, flip flops.
3. Purpose
One of the difficulties to do web development is to be more divided, not as compact as a GUI interface, such as writing a servlet, to configure web.xml, modify the field of the database, to modify various logic, and modify the database The situation is precisely, so the developer wants to revise, synchronize. In addition to these, the people needed by web development are different, there are art, database design, logic development, how to coordinate, how to synchronize, these There is a direct impact on improving development efficiency.
The technology I discussed here is not necessarily universal, these are just the experience I accumulated, and I am also inoperative.
First article: Chinese character encoding problem
Perhaps Chinese characters are the most common problems in Web development, so I will start our discussion!
1. The occasion of Chinese characters
a. Get parameters Request.getParameter ("param"); under Tomcat, the default is ISO8859-1, so if you want to get the parameters correctly, you need to convert to GB2132;
b. Chinese characters displayed on the page.
There are two situations here.
I) When JSP is to be parsed to servlet, the container reads <% @ page contentType = "text / html; charSet = GB2312" to determine the encoding of the file, so it is usually set.
When IE gets the page to be displayed, the displayed encoding is determined by .
II) If no code is set, the code is encoded is ISO8859-1, and if the Chinese character is to be displayed, you can use the Unicode encoding form to display Chinese characters. For example, direct.print ('/ uffff');
Converting Chinese characters into unicode encodings to use a tool under JDK 1.5. You can convert all the Chinese characters in a file into Unicode encoding. This method is not ahead of this convenience.
Code in C. Beans. The default code is encoded during the encoding in Java Beans. If GB2312 is encoded when the platform is encoded, no special conversion is required in Java Beans.
d. Coding of the database. The database also has its own default encoding. MySQL's default code is ISO8859, the default code of MSSQL / Orcale is a platform encoding, and it is GB2312 under Windows. Write the database to the database, the corresponding transformation should be made according to the coding of the database.
2. How to implement automatic conversion of Chinese character encoding
From the above-mentioned occasions, the most critical is the first, every time you turn to turn, it is very troublesome. If you are in WebLogic, you can set a parameter in web.xml, let WebLogic turnover, but this Can only be used in WebLogic.
Learn from WebLogic's ideas, you can write servlet filter to implement. Filter is the content of Servlet 2.3 specification. Now all popular servers should be supported.
code show as below:
/ **
* Filter Class
*
* @ Web.Filter Name = "locale.filter" Display-name = "locale filter" Description = "description for locale" * @ web.filter-maping url-pattern = "/ *"
*
* /
Public class localefilter imports filter {
Private log log = logfactory.getlog (localefilter.class);
/ **
*
* /
Public localfilter () {
Super ();
}
/ **
* @see javax.servlet.filter # init (javax.servlet.filterConfig)
* /
Public void init (filterconfig arg0) {
// Todo auto-generated method stub
}
/ **
* @see javax.servlet.filter # DOFILTER (javax.servlet.servletRequest, javax.servlet.servletResponse, javax.servlet.filterchain)
* /
Public Void Dofilter (ServletRequest Request, ServletResponse Response,
Filterchain chain) throws oException, servletexception {
// String Encoding = CommonUtil.getDefaultencoding ();
///log.debug("default characterencoding: " encoding);
// Encoding can be automatically acquired from the platform.
String encoding = "GB2312";
Request.setCharacterencoding (Encoding);
Response.setContentType ("Text / Html; Charset =" Encoding;
Response.setCharacterencoding (encoding);
Chain.dofilter (Request, Response);
}
/ **
* @see javax.servlet.filter # destroy ()
* /
Public void destroy () {
}
}
3. Effect
Based on the achievement of Filter, there are many benefits. We can write the Filter under the GB2312 platform, or write the filter in other platforms. The method of use is also very simple, and directly request.getParameter () is OK, no need special Conversion.
4. Summary
Filter-based implementation is a good choice. If you want to read the Chinese characters in a file in the framework similar to the Struts, and want to display on the page, if you are a common bean: Write label to read Properties,
The best way is to use Chinese characters in Properties to encode unicode, and efficiently.