?? aspcool_com--八、設置http應答頭.htm
字號:
<BR>
下面這個Servlet用來計算大素數。因為計算非常大的數字(例如500位)可能要花不少時間,所以Servlet將立即返回已經找到的結果,同時在后臺繼續計算。后臺計算使用一個優先級較低的線程以避免過多地影響Web服務器的性能。如果計算還沒有完成,Servlet通過發送Refresh頭指示瀏覽器在幾秒之后繼續請求新的內容。
<BR> <BR>
注意,本例除了說明HTTP應答頭的用處之外,還顯示了Servlet的另外兩個很有價值的功能。首先,它表明Servlet能夠處理多個并發的連接,每個都有自己的線程。Servlet維護了一份已有素數計算請求的Vector表,通過查找素數個數(素數列表的長度)和數字個數(每個素數的長度)將當前請求和已有請求相匹配,把所有這些請求同步到這個列表上。第二,本例證明,在Servlet中維持請求之間的狀態信息是非常容易的。維持狀態信息在傳統的CGI編程中是一件很麻煩的事情。由于維持了狀態信息,瀏覽器能夠在刷新頁面時訪問到正在進行的計算過程,同時也使得Servlet能夠保存一個有關最近請求結果的列表,當一個新的請求指定了和最近請求相同的參數時可以立即返回結果。
<BR> <BR> PrimeNumbers.java
<BR> <BR>
注意,該Servlet要用到前面給出的ServletUtilities.java。另外還要用到:PrimeList.java,用于在后臺線程中創建一個素數的Vector;Primes.java,用于隨機生成BigInteger類型的大數字,檢查它們是否是素數。(此處略去PrimeList.java和Primes.java的代碼。)
<BR> package hall; <BR>
<BR> import java.io.*; <BR> import
javax.servlet.*; <BR> import javax.servlet.http.*;
<BR> import java.util.*; <BR>
<BR> public class PrimeNumbers extends HttpServlet {
<BR> private static Vector primeListVector = new
Vector(); <BR> private static int maxPrimeLists = 30;
<BR> <BR> public void
doGet(HttpServletRequest request, <BR>
HttpServletResponse response) <BR> throws
ServletException, IOException { <BR> int numPrimes =
ServletUtilities.getIntParameter(request, "numPrimes", 50);
<BR> int numDigits =
ServletUtilities.getIntParameter(request, "numDigits", 120);
<BR> PrimeList primeList =
findPrimeList(primeListVector, numPrimes, numDigits);
<BR> if (primeList == null) { <BR> primeList
= new PrimeList(numPrimes, numDigits, true); <BR>
synchronized(primeListVector) { <BR> if
(primeListVector.size() >= maxPrimeLists) <BR>
primeListVector.removeElementAt(0); <BR>
primeListVector.addElement(primeList); <BR> }
<BR> } <BR> Vector currentPrimes =
primeList.getPrimes(); <BR> int numCurrentPrimes =
currentPrimes.size(); <BR> int numPrimesRemaining =
(numPrimes - numCurrentPrimes); <BR> boolean
isLastResult = (numPrimesRemaining == 0); <BR> if
(!isLastResult) { <BR> response.setHeader("Refresh",
"5"); <BR> } <BR>
response.setContentType("text/html"); <BR> PrintWriter
out = response.getWriter(); <BR> String title = "Some "
+ numDigits + "-Digit Prime Numbers"; <BR>
out.println(ServletUtilities.headWithTitle(title) + <BR>
"<BODY BGCOLOR=\"#FDF5E6\">\n" + <BR> "<H2
ALIGN=CENTER>" + title + "</H2>\n" + <BR> "<H3>Primes
found with " + numDigits + <BR> " or more digits: " +
numCurrentPrimes + ".</H3>"); <BR> if (isLastResult)
<BR> out.println("<B>Done searching.</B>");
<BR> else <BR> out.println("<B>Still looking
for " + numPrimesRemaining + <BR> "
more<BLINK>...</BLINK></B>"); <BR> out.println("<OL>");
<BR> for(int i=0; i<numCurrentPrimes; i++) {
<BR> out.println(" <LI>" + currentPrimes.elementAt(i));
<BR> } <BR> out.println("</OL>");
<BR> out.println("</BODY></HTML>"); <BR> }
<BR> <BR> public void
doPost(HttpServletRequest request, <BR>
HttpServletResponse response) <BR> throws
ServletException, IOException { <BR> doGet(request,
response); <BR> } <BR> <BR> //
檢查是否存在同類型請求(已經完成,或者正在計算)。 <BR> //
如存在,則返回現有結果而不是啟動新的后臺線程。 <BR> private PrimeList
findPrimeList(Vector primeListVector, <BR> int
numPrimes, <BR> int numDigits) { <BR>
synchronized(primeListVector) { <BR> for(int i=0;
i<primeListVector.size(); i++) { <BR> PrimeList primes =
(PrimeList)primeListVector.elementAt(i); <BR> if
((numPrimes == primes.numPrimes()) && <BR>
(numDigits == primes.numDigits())) <BR> return(primes);
<BR> } <BR> return(null); <BR> }
<BR> } <BR> } <BR>
<BR> <BR> <BR>
<BR> PrimeNumbers.html <BR> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<BR> <HTML> <BR> <HEAD> <BR>
<TITLE>大素數計算</TITLE> <BR> </HEAD>
<BR> <CENTER> <BR> <BODY BGCOLOR="#FDF5E6">
<BR> <FORM ACTION="/servlet/hall.PrimeNumbers">
<BR> <B>要計算幾個素數:</B> <BR> <INPUT TYPE="TEXT"
NAME="numPrimes" VALUE=25 SIZE=4><BR> <BR>
<B>每個素數的位數:</B> <BR> <INPUT TYPE="TEXT" NAME="numDigits"
VALUE=150 SIZE=3><BR> <BR> <INPUT TYPE="SUBMIT"
VALUE="開始計算"> <BR> </FORM> <BR> </CENTER>
<BR> </BODY> <BR> </HTML> <BR>
<BR> <BR> <BR>
</P></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" bgColor=#ffcc99
border=0><TBODY>
<TR>
<TD height=24>
<CENTER>相關文章</CENTER></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=0 width="100%" align=top bgColor=#ffffcc
border=0>
<TBODY>
<TR>
<TD vAlign=top width="50%">前1篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=337&bbsuser=jsp"
target=_blank>七、HTTP應答狀態</A> <BR>前2篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=336&bbsuser=jsp"
target=_blank>六、訪問CGI變量</A> <BR>前3篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=335&bbsuser=jsp"
target=_blank>五、讀取HTTP請求頭</A> <BR>前4篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=334&bbsuser=jsp"
target=_blank>四、處理表單數據</A> <BR></TD>
<TD vAlign=top width="50%">后1篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=339&bbsuser=jsp"
target=_blank>九、處理Cookie</A> <BR>后2篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=340&bbsuser=jsp"
target=_blank>十、會話狀態</A> <BR>后3篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=341&bbsuser=jsp"
target=_blank>十一、JSP及語法概要</A> <BR></TD></TR></TBODY></TABLE><BR>
<CENTER><A
href="http://www.aspcool.com/lanmu/republic.asp?ID=338&bbsuser=jsp&motive=八、設置HTTP應答頭">發表評論</A>
<A
href="http://www.aspcool.com/lanmu/register.asp">注冊新用戶</A></CENTER><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" bgColor=#ff8080
border=0><TBODY>
<TR>
<TD width="50%" height=24>
<CENTER>對該文的評論</CENTER></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<SCRIPT language=JavaScript
src="C:\Documents and Settings\selin\桌面\ASPcool_com--八、設置HTTP應答頭.files\ads(1).htm"
type=text/JavaScript> </SCRIPT>
<BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width=768 bgColor=#ffe7ce border=0>
<TBODY>
<TR>
<TD>
<P align=right><A href="http://www.aspcool.com/">返回首頁</A> <A
href="javascript:window.close()">關閉窗口</A></P></TD></TR></TBODY></TABLE>
<TABLE height=27 cellSpacing=0 cellPadding=0 width=768 border=0>
<TBODY>
<TR bgColor=#c4ecff>
<TD width="100%">
<P align=left><STRONG><FONT color=#6f6fb7> 導航</FONT></STRONG>
>> <A href="http://www.aspcool.com/">ASP酷首頁</A>-<A
href="http://www.chaxiu.com/">茶秀虛擬社區</A>-<A
href="http://www.aspcool.com/freebbs/apply.asp">申請BBS</A>-<A
href="http://www.aspcool.com/lanmu/mybook.htm">免費代碼教程</A>- <A
href="http://www.aspcool.com/guestbook/apply.asp">申請留言板</A> -<A
href="http://www.aspcool.com/lanmu/browse.asp?bbsuser=jsp"> JSP教程專欄</A>
</P></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=773 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD><A href="http://www.aspcool.com/"><IMG height=60
src="ASPcool_com--八、設置HTTP應答頭.files/top.gif" width=140 border=0></A></TD>
<TD width=10></TD>
<TD align=left>
<CENTER><IFRAME name=inpop marginWidth=0 marginHeight=0
src="ASPcool_com--八、設置HTTP應答頭.files/ad.htm" frameBorder=0 width=468
scrolling=no height=60 0?></IFRAME></CENTER></TD>
<TD width=10></TD>
<TD align=right width=150>
<DIV align=center><A href="http://www.aspcool.com/"><IMG height=60
src="ASPcool_com--八、設置HTTP應答頭.files/top.gif" width=140 border=0></A>
</DIV></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=773 align=center bgColor=#3366cc
border=0>
<TBODY>
<TR>
<TD> </TD></TR></TBODY></TABLE><BR>
<CENTER><A href="http://www.aspcool.com/about.asp">關于本站<A> - <A
href="http://www.aspcool.com/ads.asp">廣告聯系<A> - <A
href="http://bbs.aspcool.com/">技術論壇<A> - <A
href="http://www.aspcool.com/help.asp">使用說明<A> - <A
href="http://www.aspcool.com/copyright.asp">版權說明<A> - <A
href="http://www.aspcool.com/link.asp">聯系方式<A>
<P class=page align=center>© 2003 版權所有 ASP酷技術網(<A href="http://www.aspcool.com/"
target=_blank>http://www.aspcool.com/</A>) <BR>技術問題請到<A
href="http://bbs.aspcool.com/" target=_blank>技術論壇</A> <A
href="http://blog.joycode.com/tim/contact.aspx" target=_blank>給站長發信</A><BR></P>
<SCRIPT language=javascript>
scroll(0,110)
</SCRIPT>
<SCRIPT src="ASPcool_com--八、設置HTTP應答頭.files/adleft.js"></SCRIPT>
<SCRIPT src="ASPcool_com--八、設置HTTP應答頭.files/adright.js"></SCRIPT>
</CENTER></CENTER></CENTER></CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -