1. garbled characters are displayed when the JSP page is called.
When a JSP page is called through a browser, all Chinese content in the client browser is garbled.
Solution:
First, confirm that the encoding format of GBK is used when the JSP file is saved in the editor, add <% @ pageEncoding = "GBK" %> at the beginning of the JSP page to solve Chinese garbled characters.
2. garbled characters are displayed when the Servlet page is called.
When Servlet is called through a browser, the Servlet displays garbled content in the browser.
Solution:
Before using response in Servlet to output content, execute response. setContentType ("text/html; charset = GBK") to set the encoding of the output content to GBK.
3. garbled parameters passed in the Post form
Submit parameters to the corresponding JSP page or Servelt through the JSP page, HTML page, or form Element in the Servlet. The Chinese parameter values received by the JSP page or Servlet are garbled.
Solution:
Before receiving parameters submitted by POST, use request. setCharacterEncoding ("GBK") to set the content of the received parameters to be encoded by GBK.
A better solution is to use the filter technology.
- Package com. htt;
- 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 {
- Public void destroy (){}
- Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- Request. setCharacterEncoding ("GBK ");
- Chain. doFilter (request, response );
- }
- Public void init (FilterConfig filterConfig) throws ServletException {}
- }
- Settings in the Web. xml file
- <Filter>
- <Filter-name>Encoding</Filter-name>
- <Filter-class>Com. htt. Encoding</Filter-class>
- </Filter>
- <Filter-mapping>
- <Filter-name>Encoding</Filter-name>
- <Url-pattern>/ToCh_zn</Url-pattern>
- </Filter-mapping>
- Functions and principles of several encodings in JSP and Servlet
- Describes the usage of the request attribute in JSP
- Integration of Apache2.2 and Tomcat5.5 in JSP environment Configuration
- FAQs about Chinese JSP pages in Tomcat 4.0 and Tomcat 4.1
- Summary of redirection Technology in Servlet and JSP