頁面都用UTF-8了,而且數據庫鏈接池也做了編碼控制,還是出現了亂碼。不過剛開始用那個數據庫,還沒有用FILTER都沒有亂碼。于是我想到是否用個FILTER試試。
過程:
1
package org.cotel.Evote.Util;
2
3
import java.io.IOException;
4
import javax.servlet.*;
5
6
public class EncodingFilter implements Filter{
7
private String encoding;
8
public EncodingFilter(){
9
}
10
public void init(FilterConfig filterconfig)
11
throws ServletException{
12
encoding = filterconfig.getInitParameter("encoding");
13
}
14
15
public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
16
throws IOException, ServletException{
17
if(encoding != null && servletrequest.getCharacterEncoding() == null)
18
servletrequest.setCharacterEncoding(encoding);
19
filterchain.doFilter(servletrequest, servletresponse);
20
}
21
public void destroy(){
22
}
23
}
24
25

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

然后將過濾器添加到web.xml


1

2

3

4

5

6

7

8

9

10

11

12

注意在所有servlet 之前。
遺憾的測試發現還是有亂碼。不過這個配置是正確的。所以最后數據庫師父說,數據庫問題。他說換個版本就行了。
不過想想今天做的事情吧,學會了連接池配置,filter配置,還能熟練用一個開源的js TREE組件。
不錯吧!