org.apache.jasper.JasperException: /admins/userManagement.jsp(1,1)
Page directive: illegal to have multiple occurrences of
contentType with different values (old: text/html;charset=utf8, new: text/html;charset=gbk) javascript
jsp的這個裏面的 contentType 屬性可能與相應的 html 的 contentType 有關聯
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf8" pageEncoding="utf8"%> css
html的以下
<html xmlns="http://www.w3.org/1999/xhtml">
< head>
<title>後臺管理系統</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<link href="backSystem.css" rel="stylesheet" type="text/css" />
<script src="js/backSystem.js" type="text/javascript" ></script>
< /head>... html
二者必須相一致!!不能一個爲 utf8,一個爲 gbk,否則話就會報出上面那個錯誤~ java
②在安裝mysql數據庫的時候若是不當心選擇了默認的 latin1,再去修改 my.ini 文件的時候
可能出現有些東西改不過來的狀況 mysql
通常狀況下,若是安裝的時候選擇 gbk的話,則將 myeclipse 項目,各個jsp頁面,過濾器都設置成 gbk
這種狀況下出現亂碼的概率不會太大,這是通過我實踐檢驗的
之前在使用gbk編碼的mysql數據庫時,基本上不多遇到如今這般的 亂碼問題~~ sql
若是將在安裝mysql數據庫的時候選擇 utf8 的話,則後面有不少注意的地方,否則話將致使不少麻煩的問題
諸如中文插不進mysql數據庫,或者中文插入mysql數據庫再取出來的時候變成了亂碼~ 數據庫
若是在安裝時選擇了 utf8,則在黑屏下執行「 SHOW VARIABLES LIKE 'character_set%'; 」
命令,通常會獲得如下結果:
+--------------------------+---------------------------------------------------------+
| Variable_name | Value
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8
|
| character_set_connection | utf8
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | utf8
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec) apache
然而這個狀況下,插入中文是插入不了的!!
mysql> show tables;
+------------------+
| Tables_in_estore |
+------------------+
| books |
| test |
| user |
+------------------+
3 rows in set (0.00 sec) eclipse
mysql> insert into test values(2,'養腸胃');
ERROR 1366 (HY000): Incorrect string value: '/xD1/xF8/xB3/xA6/xCE/xB8' for colum
n 'name' at row 1 jsp
怎麼辦呢??看下面的解決方案~
mysql> set names 'gbk';
Query OK, 0 rows affected (0.00 sec)
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into test values(3,'朱昱');
Query OK, 1 row affected (0.00 sec)
繼續往下走
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value
|
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)
能夠看到這時候 character_set_client 、character_set_connection 、character_set_results
的值都變成 gbk 了,也就是說 set names 'gbk' 修改了這3個變量的值!!!
而後:
mysql> set character_set_results='utf8';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 鎵暱浼? |
| 3 | 鏈辨槺 |
+----+-----------+
2 rows in set (0.00 sec)
還原:
mysql> set character_set_results='gbk';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | 揚長偉 |
| 3 | 朱昱 |
+----+--------+
2 rows in set (0.00 sec)
繼續試驗!!
mysql> set names 'latin1';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | ??? |
| 3 | ?? |
+----+------+
2 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value
+--------------------------+---------------------------------------------------------+
| character_set_client | latin1
|
| character_set_connection | latin1
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | latin1
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)
mysql> insert into test values (4,'打發');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | ??? |
| 3 | ?? |
| 4 | 打發 |
+----+------+
3 rows in set (0.01 sec)
③我曾經在這個項目上遇到過這樣一個問題!!
整個 E-StorePro 項目採用的編碼格式爲 UTF-8 ,該項目裏面的每一個 JSP 頁面涉及到編碼格式的設置都爲 UTF-8
該項目裏面的每一個 html 頁面涉及到編碼格式的也都設置爲 UTF-8 !!
有一天我不死心(後臺JSP頁面採用 了嵌套JSp頁面的模式,如今看來感受真是亂糟糟的,當初還爲此自鳴得意節省代碼),
把某個JSP頁面的編碼格式改爲了 gbk,結果就致使有時候進入到後臺的時候報錯,而有時候卻正常地不得了~
通常狀況下,設置爲以下這樣就差很少了~
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | utf8
|
| character_set_system | utf8
整個項目的編碼格式建議爲utf-8,整個項目的全部jsp,html頁面編碼選項都設置成utf-8
過濾器filter貌似無所謂,我filter是像下面這麼寫得,感受沒什麼用~
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
④數據庫鏈接字符串帶仍是不帶 「&useUnicode=true&characterEncoding=UTF-8」 呢? 若是採用3所述的設置的話,帶上則會出現亂碼,不帶居然也出現了亂碼!!!蛋疼啊!! 回想究竟是什麼使我可以在mysql數據庫中插入正常地中文?? 對了,難道是由於添加書籍數據的那幾個頁面將全部與編碼相關的變量都設置成了「gbk」??? 我初步猜想應該是這樣的,有待我進一步實驗!!!! 通過個人反覆測試,媽的果真不出我所料!!!!!! 通過進一步實驗,發現不管是 set names 'gbk'、 set names 'utf8' 仍是 set names 'latin1' 都能正常地插入和查詢出中文,最最關鍵的其實不在於數據庫 而在於 jsp、html 頁面的編碼是否爲 GBK !!!這是最終的結論!!