平時經驗總結
1.設置一個按鈕為有用或不能用,<%
if(index.intValue()%2==1)
{
%>
<input type="button" name="Update" class="kuButton" value='Update' disabled onclick="submitUpdate();return false;">
<input type="button" name="Delete" class="kuButton" value='Delete' disabled onclick="submitDel();return false;"></td>
<%
}else{
%>
<input type="button" name="Update" class="kuButton" value='Update' disabled onclick="submitUpdate();return false;">
<input type="button" name="Delete" class="kuButton" value='Delete' disabled onclick="submitDel();return false;"></td>
<%
}
%>
2.讓你一個按鈕顯示不顯示
顯示:document.formname.id.style.display="none"
不顯示:document.formname.id.style.display="inline"
3.在一個鏈接中要用到動態的參數,要調用方法,但是傳參數時容易出現問題,最好的方式是:
action='<%="artMaintAction.do?method="+ session.getAttribute("method")+"&firstlevel=3&secondlevel=1"%>'
4,當頁面提交是為默為的utf-u編碼,我們要轉換成GB2312才能正確插入到數據庫中:
在SEVLETK加上:request.seCharacterEncoding("gb2312")
5.傳遞表單參數:
String name = new String(request.getParameter("name"));
7.文件操作
~~將一個字符串寫到一個指定的文件中,如果該文件不存在,則新建一個文件,并完成寫入;如果存在,則用此字符串覆蓋原文件的所有內容
import java.io.*;
String str = "print me";
//定義好打印的目標文件名
//取得當前主機存放WEB頁面的絕對路徑
String hostdir = System.getProperty("user.dir");
//取得當前主機所采用的路徑分隔符
String fileBar = System.getProperty("file.separator");
//書寫完整的目標文件存放路徑
String nameOfFile=hostdir+fileBar+"test.html";
try
//實例化一個文件輸出流對象
FileOutputStream afile = new FileOutputStream(nameOfFile);
//將文件輸出流,創建一個打印輸出流對象
PrintWriter pw = new PrintWriter(afile);
pw.println(str);
//clean up
pw.close();
{}
catch(IOException e)
out.println(e.getMessage());
{}
~~列出指定目錄下的文件列表
import java.io.*;
String cdur = System.getProperty("user.dir");
String fileBar = System.getProperty("file.separator");
String mydir =cdur+fileBar+"doc"+fileBar+"jspsky";
File my = new File(mydir);
String d[] = my.list();
int i;
int l=d.length;
for(i=0;i out.print(d[i]);
{}
8.計數器
Integer count = null;
synchronized (application)
count =(Integer) application.getAttribute("d");
if (count ==null)
count =new Integer("0");
count = new Integer(count.intValue()+1);
application.setAttribute("d",count);
{}
out.println(count);
// 首先定義一個整形對象,并初始化為:NULL,
// 取回APPLICATION對像的屬性D的值,并強制轉化為整形對象,賦給COUNT
// 判斷COUNT是否為空,為空時,將O賦給COUNT對象,
// 否則,通過COUNT。INTVALUE()方法,實現COUNT對象加1,并賦值給COUNT
// 最后,將COUNT對象保存在APPLICATION對象的D變量中。
9,在JAVA中
String a[] = { "a", "ab" };
String b[][] = { { "abc", "abcd" }, { "d" } };
10,不能把邏輯直接放在一個類里面方法外面,這是一個很低級的錯誤,而且會使你找不到錯誤在哪里。
11,在數組中
String a[] = { "a", "aa" ,"aaa"};
String c[] = { "c", "cc" };
如果a = c 即變成是a[]== { "c", "cc" };
c = a 即變成是c[]=={ "a", "aa" ,"aaa"};它們不是一個一個的賦值。
11,數組能存放原始對象(primitive)和Object對象,容器只能存放Object對象,不過可以使用類將其轉換,不過數組的效率比容器高很多。數組是不可變的
12,n = Math.abs(n)是返回N的決對值]
13,http://java.sun.com/docs/books/tutorial/index.html THE Java Tutorial
14,Array中Arrays.fill(a9, 1, 3, "World");是把a9中的第二個字符和第三個字符填上world
15,java.util.array類是一組用于數組操作的static方法它有四個基本方法:
Arrays.fill(object, values) , Arrays.sort(), Arrays.equals() , Arrays.binarySearch()[用天排列數組查找對象]
System.arraycopy(fromobject,startnum,toobject,startnum,length)用于數組之間的拷貝
Arrays.equals(objecta,objectb);返回一個boolen值,這里相等一定指長度相同,而且第一個元素相同
Arrays.sort(a); 對對象a進行排序,a是數字以小數字放前面,a是字母以A開頭a第二,以此類推
int location = Arrays.binarySearch(a, 20);查找a中為20(必須為20不能多不能少)的位置,在查找之前必須用Arrays.sort(a);排序
16.在JAVA數組中:
String[] s1 = { "Hi", "Hi", "Hi", "Hi", "Hi" };
String[] s2 = { "Hi", "Hi", "Hi", "Hi", "Hi" };
String[] s3 = { "Hi", "Hi", "Hi", "11", "" };
Arrays.equals(s1,s2)成立,Arrays.equals(s3,s2)不成立,它是基本內容的比較,相當與s1-s2(中間為減號),返回的是一個大于小于或等于O的數
s2.equals(s3),s2==s3它們是基于對象的比較(內存中比較)
17,返回一個1-100的整數:
static Random r = new Random();
Math.abs(r.nextInt()) % 100;
18,在其它類里面調用靜態方法:new testT.TConstraction1(5);只會調用靜態類的確定的構造函數,不會調用其它代碼:
class testT {
testT() {System.out.println("TConstraction(..)");}
public static class TConstraction1 {
public void atest() {
System.out.println(" 會執行所有的Constaction代代碼");
}
public TConstraction1(int i) {
System.out.println("-static void TConstraction1 i==" + i);
};
}
}
19,s.toLowerCase()將s中所有字母轉換成小寫字母
20,容器分為:collection map
collection分為set ,list.
set里面的值不能重復,ArrayList,hashSet 添加為a.add("test") set會進行排序
MAP是一個鏈接保存一對對象,也可以轉換成Collection 添加為:a.put("test","test2"):
21,list中也能用Collections.fill()靜態方法來填充字符,但是必須要在原來里面有值的時候才能填
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
list.add("");
}
Collections.fill(list, "Hello");
21,ArrayList其實就是一個可變長的數組,它方法也較簡單:用add("a")加,用get(i)得到第i個對象
22.collection(list and set)包含以下功能:
exp: collection c= new Arraylist(); collection b= new Arraylist(); String c ="1";
c.add(object c);
c.addAll(collection b);
c.clear();
c.contains(object b);
c.containsAll(collection b);
c.isEmpty();
Iterator iteratora = a.iterator(); while(iteratora.hasNest())
c.remove(object c);
c.removeAll(collection b);
c.size();容器大小
沒有get(i)隨機取值
LinkedList有addFirst() removeFirst() addLast() removeLast();
23.map有兩個方法可以分別打印key and values
System.out.println(mapt1.values());
System.out.println(mapt1.keySet());
也可以直接打印System.out.println(mapt1);
map使用Iterator 必須使用keySet 或values如:Iterator it =map1.keySet.iterator();
map1中可以使用map1.get(object key);
containsKey()包含一個key對象;
containsValues()包含一個值對象;
24,Math.random() * 20
返回一個1-20之間的一個應意隨機數,返回的是一個doubel型的,如果想產生在1-20整數使用:
Integer r = new Integer( (int) (Math.random() * 20));
25,SortedMap 比HASHMAP多了幾個屬性:
map1.firstKey();第一個
map1.lastKey();最后一個
map1.subMap(fromKey,toKey);返回值中包括前者不包括后者
map1.headMap(toKey);返回的值都小于fromKey
map1.tailMap(fromKey);返回的值都大于fromKey
26。HashMap 要用equals()不能判斷查詢的鍵是不是與表里面的其它鍵相等。
equals()具有五個屬性:反身,對稱,傳遞,一致,非空
27.取得當前主機的名稱:
//取得當前主機存放WEB頁面的絕對路徑
String hostdir = System.getProperty("user.dir");
//取得當前主機所采用的路徑分隔符
String fileBar = System.getProperty("file.separator");
//書寫完整的目標文件存放路徑
String nameOfFile=hostdir+fileBar+"test.html";