1 借助spring
web.xml中增加
1
<filter>
2
<filter-name>Set Character Encoding</filter-name>
3
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
4
<init-param>
5
<param-name>encoding</param-name>
6
<param-value>utf8</param-value>
7
</init-param>
8
</filter>
9
<filter-mapping>
10
<filter-name>Set Character Encoding</filter-name>
11
<url-pattern>/*</url-pattern>
12
</filter-mapping>
13
14

2

3

4

5

6

7

8

9

10

11

12

13

14

配置文件即可,如果不用spring怎么辦呢?有辦法啊
1
<filter>
2
<filter-name>encodingFilter</filter-name>
3
<filter-class>com.rda.commons.filter.ChineseFilter</filter-class>
4
<init-param>
5
<param-name>encoding</param-name>
6
<param-value>UTF-8</param-value>
7
</init-param>
8
<init-param>
9
<param-name>forceEncoding</param-name>
10
<param-value>true</param-value>
11
</init-param>
12
</filter>
13
<filter-mapping>
14
<filter-name>encodingFilter</filter-name>
15
<url-pattern>/*</url-pattern>
16
</filter-mapping>
17
18

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

配置文件,然后呢,轉碼的過濾器就要自己寫咯
1
package com.filter;
2
3
import java.io.IOException;
4
import javax.servlet.Filter;
5
import javax.servlet.FilterChain;
6
import javax.servlet.FilterConfig;
7
import javax.servlet.ServletException;
8
import javax.servlet.ServletRequest;
9
import javax.servlet.ServletResponse;
10
import javax.servlet.http.HttpServlet;
11
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletResponse;
13
14
/**
15
*
16
* 創建人:金鑫
17
* 創建時間:2008-9-21 下午03:27:03
18
* 類作用:中文轉碼過濾器
19
*
20
*/
21
@SuppressWarnings("serial")
22
public class ChineseFilter extends HttpServlet implements Filter
23
{
24
private FilterConfig filterConfig;
25
26
public void init(FilterConfig filterConfig) throws ServletException
27
{
28
this.filterConfig = filterConfig;
29
}
30
31
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
32
{
33
try
34
{
35
String encoding = filterConfig.getInitParameter("encoding");
36
//從WEB.xml配置文件中取出參數,這樣我們可以通過配置修改編碼格式.
37
request.setCharacterEncoding(encoding);//設置請求的編碼格式
38
39
filterChain.doFilter(request, response);
40
}
41
catch (ServletException sx)
42
{
43
filterConfig.getServletContext().log(sx.getMessage());
44
}
45
catch (IOException iox)
46
{
47
filterConfig.getServletContext().log(iox.getMessage());
48
}
49
}
50
51
public void destroy()
52
{
53
}
54
55
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
56
{
57
super.doGet(arg0, arg1);
58
}
59
60
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
61
{
62
super.doPost(arg0, arg1);
63
}
64
65
/**
66
* @function 驗證數據是否為空,如果為空則轉換
67
* @param param
68
* @return String
69
*/
70
public String checkNull(String param)
71
{
72
if (param == null || param.equals(""))
73
{
74
return "";
75
}
76
else
77
{
78
return param;
79
}
80
}
81
}
82
83

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

OK,完美解決掉啦。
其實解決亂碼很簡單的,就是通過過濾器來實現的,呵呵很簡單吧?
本文來自“青島金鑫-java技術攻略”url:http://hi.baidu.com/%C7%E0%B5%BA%BD%F0%F6%CE/blog/item/a726f523915fe4559922ed57.html
I have encounted a error when I use a report system underlying JSF.
But any way , this is definitely a good and generic solution for J2EE solutions, thanks for you sharing.
請問下你說遇到的錯誤,是什么錯誤。我在轉這篇文章時,是在一個qq技術群中的一個大大的博客上看到的,我用了下感覺不錯。想向你請教學習。
可以的話,也請加入這個qq群:28251818。同時感謝你的評論!