【1、最基本的彈出窗口代碼】
<SCRIPT LANGUAGE="javascript">
<!--
window.open ('page.html')
-->
</SCRIPT>
因為這是一段javascripts代碼,所以它們應該放在<SCRIPT LANGUAGE="javascript">標簽和</script>之間。<!-- 和 -->是對一些版本低的瀏覽器起作用,在這些老瀏覽器中不會將標簽中的代碼作為文本顯示出來。要養成這個好習慣啊。window.open ('page.html') 用于控制彈出新的窗口page.html,如果page.html不與主窗口在同一路徑下,前面應寫明路徑,絕對路徑(
http://)和相對路徑(../)均可。用單引號和雙引號都可以,只是不要混用。這一段代碼可以加入HTML的任意位置,<head>和</head>之間可以,<body>間</body>也可以,越前越早執行,尤其是頁面代碼長,又想使頁面早點彈出就盡量往前放。
【2、經過設置后的彈出窗口】
下面再說一說彈出窗口的設置。只要再往上面的代碼中加一點東西就可以了。我們來定制這個彈出的窗口的外觀,尺寸大小,彈出的位置以適應該頁面的具體情況。
<SCRIPT LANGUAGE="javascript">
<!--
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no') //這句要寫成一行
-->
</SCRIPT>
參數解釋:
<SCRIPT LANGUAGE="javascript"> js腳本開始;
window.open 彈出新窗口的命令;
'page.html' 彈出窗口的文件名;
'newwindow' 彈出窗口的名字(不是文件名),非必須,可用空''代替;
height=100 窗口高度;
width=400 窗口寬度;
top=0 窗口距離屏幕上方的象素值;
left=0 窗口距離屏幕左側的象素值;
toolbar=no 是否顯示工具欄,yes為顯示;
menubar,scrollbars 表示菜單欄和滾動欄。
resizable=no 是否允許改變窗口大小,yes為允許;
location=no 是否顯示地址欄,yes為允許;
status=no 是否顯示狀態欄內的信息(通常是文件已經打開),yes為允許;
</SCRIPT> js腳本結束
【3、用函數控制彈出窗口】
下面是一個完整的代碼。
<html>
<head>
<script LANGUAGE="JavaScript">
<!--
function openwin() {
window.open ("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //寫成一行
}
//-->
</script>
</head>
<body onload="openwin()">
任意的頁面內容...
</body>
</html>
這里定義了一個函數openwin(),函數內容就是打開一個窗口。在調用它之前沒有任何用途。怎么調用呢?
方法一:<body onload="openwin()"> 瀏覽器讀頁面時彈出窗口;
方法二:<body onunload="openwin()"> 瀏覽器離開頁面時彈出窗口;
方法三:用一個連接調用:
<a href="#" onclick="openwin()">打開一個窗口</a>
注意:使用的“#”是虛連接。
方法四:用一個按鈕調用:
<input type="button" onclick="openwin()" value="打開窗口">
【4、同時彈出2個窗口】
對源代碼稍微改動一下:
<script LANGUAGE="JavaScript">
<!--
function openwin() {
window.open ("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no")//寫成一行
window.open ("page2.html", "newwindow2", "height=100, width=100, top=1 00, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca tion=no, status=no")//寫成一行
}
//-->
</script>
為避免彈出的2個窗口覆蓋,用top和left控制一下彈出的位置不要相互覆蓋即可 。最后用上面說過的四種方法調用即可。
注意:2個窗口的name(newwindows和newwindow2)不要相同,或者干脆全部為空。
【5、主窗口打開文件1.htm,同時彈出小窗口page.html】
如下代碼加入主窗口<head>區:
<script language="javascript">
<!--
function openwin() {
window.open("page.html","","width=200,height=200")
}
//-->
</script>
加入<body>區:
<a href="1.htm" onclick="openwin()">open</a>即可。
【6、彈出的窗口之定時關閉控制】
下面我們再對彈出的窗口進行一些控制,效果就更好了。如果我們再將一小段 代碼加入彈出的頁面(注意是加入page.html的HTML中,不是主頁面中),讓它10秒后自動關閉是不是更酷了?
首先,將如下代碼加入page.html文件的<head>區:
<script language="JavaScript">
function closeit()
{
setTimeout("self.close()",10000) //毫秒
}
</script>
然后 儆?lt;body onload="closeit()"> 這一句話代替page.html中原有的<BODY>這一句就可以了。(這一句話千萬不要忘記寫啊!這一句的作用是調用關閉窗口的代碼,10秒鐘后就自行關閉該窗口。)
【7、在彈出窗口中加上一個關閉按鈕】
<FORM>
<INPUT TYPE='BUTTON' VALUE='關閉' onClick='window.close()'>
</FORM>
呵呵,現在更加完美了!
【8、內包含的彈出窗口-一個頁面兩個窗口】
上面的例子都包含兩個窗口,一個是主窗口,另一個是彈出的小窗口。通過下面的例子,你可以在一個頁面內完成上面的效果。
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function openwin()
{
OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars="+scroll+",menubar=no");
//寫成一行
OpenWindow.document.write("<TITLE>例子</TITLE>")
OpenWindow.document.write("<BODY BGCOLOR=#ffffff>")
OpenWindow.document.write("<h1>Hello!</h1>")
OpenWindow.document.write("New window opened!")
OpenWindow.document.write("</BODY>")
OpenWindow.document.write("</HTML>")
OpenWindow.document.close()
}
</SCRIPT>
</head>
<body>
<a href="#" onclick="openwin()">打開一個窗口</a>
<input type="button" onclick="openwin()" value="打開窗口">
</body>
</html>
看看OpenWindow.document.write()里面的代碼不就是標準的HTML嗎?只要按照格式寫更多的行即可。千萬注意多一個標簽或少一個標簽就會出現錯誤。記得用 OpenWindow.document.close()結束啊。
【9、終極應用--彈出的窗口之Cookie控制】
回想一下,上面的彈出窗口雖然酷,但是有一點小毛病,比如你將上面的腳本放在一個需要頻繁經過的頁面里(例如首頁),那么每次刷新這個頁面,窗口都會彈出一次,我們使用cookie來控制一下就可以了。
首先,將如下代碼加入主頁面HTML的<HEAD>區:
<script>
function openwin(){
window.open("page.html","","width=200,height=200")
}
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function loadpopup(){
if (get_cookie('popped')==''){
openwin()
document.cookie="popped=yes"
}
}
</script>
然后,用<body onload="loadpopup()">(注意不是openwin而是loadpop啊!)替換主頁面中原有的<BODY>這一句即可。你可以試著刷新一下這個頁面或重新進入該頁面,窗口再也不會彈出了。真正的Pop-Only-Once!
對于Java開發人員,多線程應該是必須熟練應用的知識點,特別是開發基于Java語言的產品。本文將深入淺出的表述Java多線程的知識點,在后續的系列里將側重于Java5由Doug Lea教授提供的Concurrent并行包的設計思想以及具體實現與應用。
如何才能深入淺出呢,我的理解是帶著問題,而不是泛泛的看。所以該系列基本以解決問題為主,當然我也非常希望讀者能夠提出更好的解決問題的方案以及提出更多的問題。由于水平有限,如果有什么錯誤之處,請大家提出,共同討論,總之,我希望通過該系列我們能夠深入理解Java多線程來解決我們實際開發的問題。
作為開發人員,我想沒有必要討論多線程的基礎知識,比如什么是線程? 如何創建等 ,這些知識點是可以通過書本和Google獲得的。本系列主要是如何理深入解多線程來幫助我們平時的開發,比如線程池如何實現? 如何應用鎖等。
(1)方法Join是干啥用的? 簡單回答,同步,如何同步? 怎么實現的? 下面將逐個回答。
自從接觸Java多線程,一直對Join理解不了。JDK是這樣說的:
join
public final void
join(long millis)throws
InterruptedException
Waits at most
millis
milliseconds for this thread to die. A timeout of
0
means to wait forever.
大家能理解嗎? 字面意思是等待一段時間直到這個線程死亡,我的疑問是那個線程,是它本身的線程還是調用它的線程的,上代碼:
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread(new RunnableImpl());
t.start();
try {
t.join(1000);
System.out.println("joinFinish");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(1000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
結果是:
Begin sleep
End sleep
joinFinish
明白了吧,當main線程調用t.join時,main線程等待t線程,等待時間是1000,如果t線程Sleep 2000呢
public void run() {
try {
System.out.println("Begin sleep");
// Thread.sleep(1000);
Thread.sleep(2000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
結果是:
Begin sleep
joinFinish
End sleep
也就是說main線程只等1000毫秒,不管T什么時候結束,如果是t.join()呢, 看代碼:
public final void join() throws InterruptedException {
join(0);
}
就是說如果是t.join() = t.join(0) 0 JDK這樣說的 A timeout of
0
means to wait forever 字面意思是永遠等待,是這樣嗎?
其實是等到t結束后。
這個是怎么實現的嗎? 看JDK代碼:
/**
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @param millis the time to wait in milliseconds.
* @exception InterruptedException if any thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
其實
Join方法實現是通過wait(小提示:Object 提供的方法)。 當main線程調用t.join時候,main線程會獲得線程對象t的鎖(wait 意味著拿到該對象的鎖),調用該對象的wait(等待時間),直到該對象喚醒main線程,比如退出后。
這就意味著main 線程調用t.join時,必須能夠拿到線程t對象的鎖,如果拿不到它是無法wait的,剛開的例子t.join(1000)不是說明了main線程等待1秒,如果在它等待之前,其他線程獲取了t對象的鎖,它等待時間可不就是1毫秒了。上代碼介紹:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread(new RunnableImpl());
new ThreadTest(t).start();
t.start();
try {
t.join();
System.out.println("joinFinish");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ThreadTest extends Thread {
Thread thread;
public ThreadTest(Thread thread) {
this.thread = thread;
}
@Override
public void run() {
holdThreadLock();
}
public void holdThreadLock() {
synchronized (thread) {
System.out.println("getObjectLock");
try {
Thread.sleep(9000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("ReleaseObjectLock");
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(2000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在main方法中 通過new ThreadTest(t).start();實例化ThreadTest 線程對象, 它在holdThreadLock()方法中,通過 synchronized (thread),獲取線程對象t的鎖,并Sleep(9000)后釋放,這就意味著,即使
main方法t.join(1000),等待一秒鐘,它必須等待ThreadTest 線程釋放t鎖后才能進入wait方法中,它實際等待時間是9000+1000 MS
運行結果是:
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish
小結:
本節主要深入淺出join及JDK中的實現。
在下一節中,我們將要討論SWing 中的事件方法線程來解決一個網友問到的問題:
如何控制Swing程序在單機只有一個實例,也就是不能運行第二個Main方法。
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1494)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at Test.TestDao.main(TestDao.java:21)
Caused by: org.dom4j.DocumentException: c:\ajar\hibernate-configuration-3.0.dtd (系統找不到指定的路徑。) Nested exception: c:\ajar\hibernate-configuration-3.0.dtd (系統找不到指定的路徑。)
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484)
... 3 more
這里是在咱們的hibernate.cfg.xml中寫在文件頭的 DOCTYPE中 這里說明當hibernate運行時候 即new Configuration的時候 hibernate這個框架會 進行XML文檔的校驗 其實 STRUTS也一樣