Detailed questions and knowledge, detailed question knowledge
I. Exceptions caused by cookie writing to Chinese Characters in Tomcat 7 and Solutions
Problem:
Cookie cookie = new Cookie ("cookieName", "Cookie value ");
Resp. addCookie (cookie );
When the target URL accesses this method, an exception is thrown:
Java. lang. IllegalArgumentException: Control character in cookie value or attribute
Description:
Environment: tomcat7 and Java7
Specific problem: An exception occurs when the cookie is written to Chinese. Therefore, the default Cookie encoding method is ASCII. Chinese characters cannot be encoded or decoded.
Solution:
When creating a Cookie:
Cookie cookie = new Cookie (name, URLEncoder. encode (value, "UTF-8 "));
When reading cookies:
String value = URLDecoder. decode (cookie. getValue (), "UTF-8 ");
Ii. Jump to the iframe parent window
In the iframe subform, jump to the parent window:
Window. parent. location. href = "parent. jsp ";
The parent attribute of window is used.
Iii. Effect of a tag disabled
Adding disabled = "disabled" to tag a does not work.
Use either of the following methods:
1.
<a href="link.html" class="not-active">Link</a>.not-active {pointer-events: none;cursor: default;}
2.
$('a.current-page').click(function() { return false; });
Iv. Form tag value passing
If the disabled attribute is added to a form tag, the value of the form tag is not uploaded to the background. You can use the readonly attribute instead.
5. button in form
In form, the default type of the button is the submit type, and the submit () method is the jQuery form submission method.
6. Database: set a field to null
1. The column does not have the not null constraint. You can directly set it to null.
2. This column has the not null constraint. When it is set to null, it is set to the default value of the corresponding type.
VII. css: z-index
. ZIndex (zIndex) =. css ("zIndex", zIndex)
8. An error occurred while introducing the script tag.
Error Tag:
<script src="${ctx}/static/js/module/project/personal_center.js" type="text/javascript" charset="utf-8"/>
The script tag is supplemented with the automatic prompt tool, but the script tag is incorrect.
Correct use:
<script src="${ctx}/static/js/module/project/personal_center.js" type="text/javascript" charset="utf-8"></script>
The script Tag ends with </script> as the closed tag.
9. Chinese garbled characters in get/post requests
Problems:
When a get request is sent to the server, if the request parameter is in Chinese, the parameters received by the server are garbled.
Cause Analysis and Solution:
1. About decoding and encoding
Encoding: '中' → 1110001
Decoding: 1110001 → Medium'
Garbled characters: different character sets are used during encoding and decoding.
2. character encoding and decoding in Web Applications
(1) Request Process:
<1> GET request
The browser side is coded in UTF-8, so the server side should also be decoded in UTF-8
Because the GET request parameters are attached to the URL address, the decoded Character Set of the URL address must be unified.
Solution:
In the tomcat configuration file server. xml find the first connector Add the following property URIEncoding = UTF-8
<2> POST request
Run: request. setCharacterEncoding ("UTF-8 ");
(2) Response Process
(1) encoding Character Set of response data
(2) Tell the browser what character set should be used for decoding the response data
<1> the Tomcat server automatically sets the encoding Character Set Based on the decoded character set"
<2> set the encoding character set to notify the browser of the content type of the current response data: response. setContentType ("text/html; charset = UTF-8"); note that the response. getWriter (); previously set; otherwise, it is invalid.
Suggestion:
All filters only apply to POST requests. Setting the character encoding method on the jsp page only applies to POST requests when the form is submitted, the GET request cannot be used to set its character encoding,
The default encoding method is used in GET requests. Therefore, for GET requests. set URIEncoding = "UTF-8" in the xml file and restart TOMCAT to solve the Chinese garbled problem,
HoweverWe do not recommend that you use Chinese parameters in href hyperlink requests or GET requests..
In addition, the unified encoding method for transmitting and receiving part of the passed Chinese characters is used to convey Parameters "reply. jsp? Title = "+ java.net. URLEncoder. encode (keywords)
Then, on the receiving parameters page, use keywords = new String (request. getParameter ("keywords"). getBytes ("8859_1"). Although this method solves Chinese garbled characters temporarily,
However, Chinese characters may still be garbled during later maintenance and deployment to other servers, so it is not recommended.
For Post requests, you only need to write the following code in Servlet or jsp to solve the problem of Chinese garbled characters passed in from the form.
Request. setCharacterEncoding ("UTF-8 ");
Understanding:
For Get requests, because the request parameters are appended to the URL in the address bar, the above processing method cannot be used. It should be like this:
String str = request. getQueryString (); // use URLDecoder to decode String str1 = java.net. URLDecoder. decode (str, "UTF-8"); String [] paraStrings = str1.split ("&"); // paraStrings [0] is the first parameter, and so on... for (String paraString: paraStrings ){
String [] nameValue = paraString. split ("="); // nameValue [0] indicates the name of the form, and nameValue [1] indicates the value corresponding to the form name}
Another way is to re-encode the request parameter value after obtaining the request parameter, that is, convert it into a byte array and then re-decode the byte array into a string.
String str=request.getParameter("name");byte[] bytes=str.getBytes("ISO-8859-1");String name=new String(bytes,"utf-8");
Conclusion: when passing Chinese parameters, it is best to use the POST Request for Submission. Different servers may set different encoding methods, and different operating systems may set different encoding methods.