<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.form.TestForm"%>
<%@page import="com.form.Person"%>
<%@page import="com.form.Go"%>
<%@page import="java.net.URLEncoder"%>
<%@ taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html"%>
<%
request.setAttribute("name", "曹歡操");
Date date = new Date();
request.setAttribute("date", date);
Person person1 = new Person();
person1.setId(1);
person1.setName("a");
Person person2 = new Person();
person2.setId(1);
person2.setName("a");
Person person3 = new Person();
person3.setId(1);
person3.setName("a");
Go go = new Go();
Map map = go.getMap();
map.put("x",person1);
map.put("y",person2);
map.put("z",person3);
request.setAttribute("go", go);
%>
<!-- 這是將保存在request中的name的值賦值給paramId中的userName -->
<html:link action="/testGoGo1.htm?p=go" paramId="userName" paramName="name">點擊</html:link>很好 可以自動攔截以任何東西結尾的
<!-- 這是將保存在request中的name對象的time屬性賦值給paramId中的now -->
<html:link action="/testGoGo1.htm?p=go" paramId="now" paramName="date" paramProperty="time">點擊</html:link>
<!--這是將go對象的map屬性(臉面包含喝多對象) -->
<html:link action="/testGoGo1.htm?p=go" name="go" property="map">點擊</html:link>
<a href="${pageContext.request.contextPath}/testGoGo1.htm?p=go&userName=${name}">電機及</a>
<%
//如果不進行編碼的的在後臺得到的id的值就是asd,但是如果通過編碼的話就是asd&go
String s = URLEncoder.encode("asd&go","utf-8");
request.setAttribute("s",s);
%>
<a href="${pageContext.request.contextPath}/testGoGo1.htm?p=go&id=${s}">點擊</a>;
//name相當於select的name屬性
<html:select property="name">
//list是保存在request的一個屬性(裏面保存的一個一個的對象),id是list中的對象的id屬性,name同id一樣
<html:optionsCollection name="list" value="id" label="name"/>
1、URLEncoder是對字符編碼,
URLDecoder是對字符進行解碼:
1.大寫字母A-Z
2.小寫字母a-z
3.數字 0-9
4.標點符 "." "-" "*" and "_"
不會被編碼,是安全的,
我就搞不懂,它們所指別的字符的都是不安全的,究竟是哪不安全,能不能舉個例子??
另外,這種編碼解碼在WEB開發中,有什麼意義,能不能舉個例子?
2、舉個簡單例子:
text = "abcd";url = "a.jsp?text="+text;
這樣沒有問題
但是當text = "abcd&edf"; 這樣url就是"a.jsp?text=abcd&edf"
request.getParameter就獲得abcd,剩餘的就解析爲另外一個參數edf
所以需要對符號"&"編碼
3、舉個簡單例子:
String value1="123&中文";
String enc="UTF-8";
String url="Http://localhost:8080/forum.jsp?id="+URLEncoder.encoder(value1,enc);
如果沒有進行URL編碼,getParameter("id")的值爲 "123";
另外,假設服務器的Http URL encoding 是"UTF-8", 而value1中含有中文。
那麼,enc 也要是UTF-8。才能正確顯示中文。
4、原理解釋:
URLEncoder類:
用於HTML的form中數據編碼的類。
這個類包含將字符串轉換爲application/x-www-form-urlencoded MIME 格式的靜態方法.
如果想了解HTML的編碼細則,請參考HTML規範。
編碼規則如下:
字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不被編碼,維持原值,
空格" "被轉換爲加號"+"。
所有其他的字符都被認爲是不安全的,首先都根據指定的編碼scheme被轉換爲1個或者多個字節。[憑什麼認爲其他的字符都是不安全的?看來這些規範的制訂者中沒有中國人呀!]
然後每個字節都被表示成"%xy"格式的由3個字符組成的字符串,xy是字節的2位16進制的表達(xy is the two-digit hexadecimal representation of the byte),推薦的編碼scheme爲UTF-8,然而,出於兼容性的考慮,如果沒有制定編碼的scheme,那麼將使用當前操作系統的編碼的scheme。
如:如果編碼scheme是UTF-8,
"The string ü@foo-bar"將被轉換爲"The+string+%C3%BC%40foo-bar" 。
因爲載UTF-8中字符ü被編碼成2個字節C3 (十六進制) 和BC (十六進制), 字符@被編碼成一個字節40 (十六進制)。
起始於:JDK1.0
這個類共有2個重載方法:
public static String encode(String s, String enc) throws UnsupportedEncodingException。起始於:JDK1.4
和即將被廢棄的方法:public static String encode(String s)。(因爲這個方法的編碼的字符集依賴於程序運行的系統的默認的字符集)。
第一個方法的作用是:根據指定的encode scheme 將一個字符串翻譯成application/x-www-form-urlencoded格式。
注意: W3C推薦UTF-8。
參數:
s - 將要被翻譯的字符串。
enc - 編碼用的character。
返回:翻譯後的字符串。
拋出異常: UnsupportedEncodingException - 如果不支持制定的編碼
起始於:1.4
另請參考:URLDecoder.decode(java.lang.String, java.lang.String)
類URLDecoder的作用和URLEncoder的作用相反,方法類似,這裏就不再贅述了。
如果你想知道你的字符串被編碼後的值是什麼樣,你可以打開www.baidu.com,然後輸入你要編碼後的數值,然後提交,你可以在地址欄看到你被編碼後的字符串
</html:select>