#
第一部分:選擇題
QUESTION NO: 1
1、
public
?
class
?Test?{
????
public
?
static
?
void
?changeStr(String?str){
????????str
=
"
welcome
"
;
????}
????
public
?
static
?
void
?main(String[]?args)?{
????????String?str
=
"
1234
"
;
????????changeStr(str);
????????System.out.println(str);
????}
}
Please write the output result :
QUESTION NO:2
1.?public?class?Test?{
2.?static?boolean?foo(char?c)?{
3.?System.out.print(c);
4.?return?true;
5.?}
6.?public?static?void?main(?String[]?argv?)?{
7.?int?i?=0;
8.?for?(?foo('A');?foo('B')&&(i<2);?foo('C')){
9.?i++?;
10.?foo('D');
12.?}
13.?}
14.?}
What is the result?
A. ABDCBDCB
B. ABCDABCD
C. Compilation fails.
D. An exception is thrown at runtime.
QUESTION NO: 3
1.?class?A?{
2.?protected?int?method1(int?a,?int?b)?{?return?0;?}
3.?}
Which two are valid in a class that extends class A? (Choose two)
A. public int method1(int a, int b) { return 0; }
B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0; }
E. static protected int method1(int a, int b) { return 0; }
QUESTION NO: 4
1.?public?class?Outer{
2.?public?void?someOuterMethod()?{
3.?//?Line?3
4.?}
5.?public?class?Inner{}
6.?public?static?void?main(?String[]argv?)?{
7.?Outer?o?=?new?Outer();
8.?//?Line?8
9.?}
10.?}
Which instantiates an instance of Inner?
A. new Inner(); // At line 3
B. new Inner(); // At line 8
C. new o.Inner(); // At line 8
D. new Outer.Inner(); // At line 8//new Outer().new Inner()
QUESTION NO: 5
Which method is used by a servlet to place its session ID in a URL that is written to the servlet’s response output stream?
A. The encodeURL method of the HttpServletRequest interface.
B. The encodeURL method of the HttpServletResponse interface.
C. The rewriteURL method of the HttpServletRequest interface.
D. The rewriteURL method of the HttpServletResponse interface.
QUESTION NO: 6
Which two are equivalent? (Choose two)
A. <%= YoshiBean.size%>
B. <%= YoshiBean.getSize()%>
C. <%= YoshiBean.getProperty("size")%>
D. <jsp:getProperty id="YoshiBean" param="size"/>
E. <jsp:getProperty name="YoshiBean" param="size"/>
F. <jsp:getProperty id="YoshiBean" property="size"/>
G. <jsp:getProperty name="YoshiBean" property="size"/>
QUESTION NO: 7
Which of the following statements regarding the lifecycle of a session bean are correct?
1.?
java.lang.IllegalStateException is thrown if
SessionContext.getEJBObject() is invoked when a stateful session bean
instance is passivated.
2.? SessionContext.getRollbackOnly() does
not throw an exception when a session bean with bean-managed
transaction demarcation is activated.
3.? An exception is not
thrown when SessionContext.getUserTransaction() is called in the
afterBegin method of a bean with container-managed transactions.
4.?
JNDI access to java:comp/env is permitted in all the
SessionSynchronization methods of a stateful session bean with
container-managed transaction demarcation.
5.? Accessing resource
managers in the SessionSynchronization.afterBegin method of a stateful
session bean with bean-managed transaction does not throw an exception.
第二部分:概念題
1.??? 描述Struts體系結構?對應各個部分的開發工作主要包括哪些?
?
2.??? XML包括哪些解釋技術,區別是什么?
?
3.??? JSP有哪些內置對象和動作?它們的作用分別是什么?
?
4、SQL問答題
SELECT * FROM TABLE
和
SELECT * FROM TABLE
WHERE NAME LIKE '%%' AND ADDR LIKE '%%'
AND (1_ADDR LIKE '%%' OR 2_ADDR LIKE '%%'
OR 3_ADDR LIKE '%%' OR 4_ADDR LIKE '%%' )
的檢索結果為何不同?
?
5、SQL問答題
表結構:
1、??? 表名:g_cardapply
字段(字段名/類型/長度):
g_applyno??????? varchar?? 8;//申請單號(關鍵字)
g_applydate???? bigint???? 8;//申請日期
g_state??????? varchar???? 2;//申請狀態
2、??? 表名:g_cardapplydetail
字段(字段名/類型/長度):
g_applyno??????? varchar???? 8;//申請單號(關鍵字)
g_name??????? varchar???? 30;//申請人姓名
g_idcard??????? varchar???? 18;//申請人身份證號
g_state??????? varchar???? 2;//申請狀態
其中,兩個表的關聯字段為申請單號。
題目:
1、??? 查詢身份證號碼為440401430103082的申請日期
2、??? 查詢同一個身份證號碼有兩條以上記錄的身份證號碼及記錄個數
3、??? 將身份證號碼為440401430103082的記錄在兩個表中的申請狀態均改為07
4、??? 刪除g_cardapplydetail表中所有姓李的記錄
世界杯賽事時間表:
1、先自定義Comparator
//????對象的比較器,如1.1.1<1.2.1<1.11.1<1.1.1.1
????public?final?class?LcsjMapComparator?implements?Comparator
????{
????????
????????public?int?compare(Object?arg0,?Object?arg1)?{
????????????GdnkVO_Lcsj_Map?vo1=(GdnkVO_Lcsj_Map)arg0;
????????????GdnkVO_Lcsj_Map?vo2=(GdnkVO_Lcsj_Map)arg1;
????????????String[]?flowCode1=vo1.getFlowCode().trim().split("\\.");
????????????String[]?flowCode2=vo2.getFlowCode().trim().split("\\.");
????????????for(int?i=0;i<2;i++)
????????????{
????????????????if(Integer.parseInt(flowCode1[i])<Integer.parseInt(flowCode2[i]))
????????????????????return?-1;
????????????????else?if(Integer.parseInt(flowCode1[i])>Integer.parseInt(flowCode2[i]))
????????????????????return?1;
????????????}
????????????return?0;
????????}
????????
????}????
compare方法中的對象類型必須是集合中的元素的數據類型。
排在后面的返回1,排在前面的返回-1,相同順序的返回0。
2、使用
Comparator?lcsjComparator=new?LcsjMapComparator();
Collections.sort(lcsjList,lcsjComparator);
即可得到排好序的集合。
有一些網友問我字符串轉日期或日期轉字符串要如何做,本來已經在留言板回答了,但覺得似乎有滿多的網友有這種困擾,因此我把它整理整理貼出來。
在這篇文章中,我用的API是SimpleDateFormat,它是屬于java.text.SimpleDateFormat,所以請記得import進來!
用法:
SimpleDateFormat?sdf?
=
?
new
?SimpleDateFormat(
"
yyyy-MM-dd?HH:mm:ss
"
);?
這一行最重要,它確立了轉換的格式,yyyy是完整的公元年,MM是月份,dd是日期,至于HH:mm:ss就不需要我再解釋了吧!
ps:為什么有的格式大寫,有的格式小寫,那是怕避免混淆,例如MM是月份,mm是分;HH是24小時制,而hh是12小時制
1.字符串轉日期:
2002-10-8 15:30:22要把它轉成日期,可以用
Date?date
=
sdf.parse(
"
2002-10-8?15:30:22
"
);
2.日期轉字符串
假如把今天的日期轉成字符串可用
String?datestr
=
sdf.format(
new
?Date());?
這個字符串的內容便類似2002-10-08 14:55:38
透過這個API我們便可以隨心所欲的將日期轉成我們想要的字符串格式,例如希望將日期輸出成2002年10月08日,
我們可以這么寫:
SimpleDateFormat?sdf?
=
?
new
?SimpleDateFormat(
"
yyyy年MM月dd日
"
);
??? String?datestr
=
sdf.format(
new
?Date());?
datestr便會依照我們設定的格式輸出
以Oracle + Tomcat 5.0為例:
1.配置server.xml
<Resource?name="jdbc/myoracle"?auth="Container"
??????????????type="javax.sql.DataSource"/>?
<ResourceParams?name="jdbc/myoracle">
??<parameter>
????<name>factory</name>
????<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
??</parameter>
??<parameter>
????<name>driverClassName</name>
????<value>oracle.jdbc.driver.OracleDriver</value>
??</parameter>
??<parameter>
????<name>url</name>
????<value>jdbc:oracle:thin:myschema@127.0.0.1:1521:mysid</value>
??</parameter>
??<parameter>
????<name>username</name>
????<value>scott</value>
??</parameter>
??<parameter>
????<name>password</name>
????<value>tiger</value>
??</parameter>
??<parameter>
????<name>maxActive</name>
????<value>20</value>
??</parameter>
??<parameter>
????<name>maxIdle</name>
????<value>10</value>
??</parameter>
??<parameter>
????<name>maxWait</name>
????<value>-1</value>
??</parameter>
</ResourceParams>
2.配置web.xml
在</web-app>節點下加下面信息:
<resource-ref>
?<description>Oracle?Datasource?example</description>
?<res-ref-name>jdbc/myoracle</res-ref-name>
?<res-type>javax.sql.DataSource</res-type>
?<res-auth>Container</res-auth>
</resource-ref>
3.測試的代碼
Context?initContext?=?new?InitialContext();
Context?envContext??=?(Context)initContext.lookup("java:/comp/env");
DataSource?ds?=?(DataSource)envContext.lookup("jdbc/myoracle");
Connection?conn?=?ds.getConnection();
//etc.
如果想要取得系統的時間,可以使用System.currentTimeMillis()方法,例如:
DateDemo.java
1?public?class?DateDemo?{
2?????public?static?void?main(String[]args)?{
3???????? System.out.println(System.currentTimeMillis());
4???? }
5}
執行結果會顯示從
1970年1月1日開始到取得系統時間為止所經過的毫秒數,例如1115346430703這個數字,但這樣的數字沒有人確切了解它的意 義是什么,您可以使用Date類別來讓這個數字變的更有意義一些,例如:
DateDemo.java
import?java.util.Date;
public?class?DateDemo?{
????public?static?void?main(String[]args)?{
????????Date?date?=?new?Date();????????????????????
??????? System.out.println(date.toString());
????????System.out.println(date.getTime());
????}
}
執行的結果如下:
? Fri May 06 10:31:13 GMT+08:00 2005
??1115346673531
當您生成Date對象時,實際上它會使用System.currentTimeMillis()來取得系統時間,而您使用
toString()方法時,會將取得的
1970年1月1日至今的毫秒數轉為
dow mon dd hh:mm:ss zzz yyyy的格式,分別是:「星期 月 日 時:分:秒 公元」;使用Date的
getTime()方法則可以取得毫秒數。
如果您想要對日期時間作格式設定,則可以使用DateFormat來作格式化,先來看看它的子類SimpleDateFormat如何使用:
DateDemo.java
???
import?java.text.DateFormat;
import?java.text.SimpleDateFormat;
import?java.util.Date;
public?class?DateDemo?{
????public?static?void?main(String[]args)?{
????????Date?date?=?new?Date();
????????DateFormat?dateFormat?=?new??SimpleDateFormat("EE-MM-dd-yyyy");
??????? System.out.println(dateFormat.format(date));
????}
}
執行結果:
??
??星期
五-05-06-2005
DateFormat會依計算機上的區域設定顯示時間格式,EE表示星期,MM表示月份、dd表示日期,而yyyy是公元,每個字符的設定都各有其意義,您
可以參考
SimpleDateFormat
的API說明了解每個字符設定的意義。
您也可以直接從DateFormat指定格式生成DateFormat的實例,例如:
DateDemo.java
???
import?java.text.DateFormat;
import?java.util.Date;
public?class?DateDemo?{
????public?static?void?main(String[]?args)?{
????????Date?date?=?new?Date();
??? ??? DateFormat?shortFormat?=??DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT);
??????? DateFormat?mediumFormat?= DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
??? ??? DateFormat?longFormat?= DateFormat.getDateTimeInstance(DateFormat.LONG,?DateFormat.LONG);
??????? DateFormat?fullFormat?= DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
??????? System.out.println(shortFormat.format(date));
????????System.out.println(mediumFormat.format(date));
????????System.out.println(longFormat.format(date));
????????System.out.println(fullFormat.format(date));
????}
}
在使用
getDateTimeInstance()取得DateFormat實例時,可以指定的參數是日期格式與時間格式,以上所指定的格式依訊息詳細度 區分,執行結果如下:
?? 2005/5/6 上午 10:45 2005/5/6 上午 10:45:25 2005年5月6日 上午10時45分25秒 2005年5月6日 星期五 上午10時45分25秒 GMT+08:00
?? |
您也可以使用getDateInstance()取得DateFormat實 例,并同時指定日期的區域顯示方式,例如:
DateDemo.java
import?java.text.DateFormat;
import?java.util.Date;
import?java.util.Locale;
public?class?DateDemo?{
????public?static?void?main(String[]?args)?{
????????Date?date?=?new?Date();
????????Locale?locale?=?new?Locale("en",?"US");
????????DateFormat?shortFormat?=? DateFormat.getDateInstance(DateFormat.SHORT,locale);
????????DateFormat?mediumFormat?=?DateFormat.getDateInstance(????????????????DateFormat.MEDIUM,locale);
????????DateFormat?longFormat?= DateFormat.getDateInstance(DateFormat.LONG,?locale);
????????DateFormat?fullFormat?= DateFormat.getDateInstance(DateFormat.FULL,?locale);
??? ??? System.out.println(shortFormat.format(date));
??? ??? System.out.println(mediumFormat.format(date));
??? ??? System.out.println(longFormat.format(date));
??? ??? System.out.println(fullFormat.format(date));
????}
}
這邊指定了美國的時間顯示方式,執行結果如下:
?
5/6/05
??May 6, 2005
??May 6, 2005
??Friday, May 6, 2005
瀏
覽器為了加速使用者的瀏覽速度,常會將瀏覽過的網頁快取到硬盤,下次瀏覽同一頁時,便去硬盤里面去找,但現在的網頁常是動態的,為了避免使用者抓到硬盤內
過期的數據,JSP可用下面的方式來防止瀏覽器快取住網頁,此方法便可保證使用者到這個網頁時始終都可看到最新的資料。
1.JSP語法
<%
??? ??? response.setHeader(
"
Pragma
"
,
"
no-cache
"
);
??? ??? response.setHeader(
"
Cache-Control
"
,
"
no-cache
"
);
??? ??? response.setDateHeader(
"
Expires
"
,
0
);
??? %>
2.也可以用以下的HTML語法,可用在靜態網頁上
<
meta?http
-
equiv?
=
?
"
Pragma
"
?content
=
"
no-cache
"
>
??? <
meta?http
-
equiv
=
"
Cache-Control
"
?content
=
"
no-cache
"
>
??? <
meta?http
-
equiv?
=
?
"
Expires
"
?content
=
"
0
"
>
注意,這些HTML語法要下在<head></head>的標簽內。
AJAX 上手篇
第一步 – 說聲「請」 (又稱為「怎么發出 XMLHttpRequest」)
為了用 JavaScript 對服務器發送 HTTP 要求,你必須先以相關的類別(class)制出實體(instance)。Internet
Explorer 首先以 ActiveX 對象方式提供 XMLHTTP
類別,而 Mozilla、Safari及其它瀏覽器則隨后以 XMLHttpRequest
類別支持此 ActiveX 對象中的類別及屬性。
因此,如果想跨瀏覽器,那么可以這么寫:?
if
?(window.XMLHttpRequest)?{?
//
?Mozilla,?Safari,?
????http_request?
=
?
new
?XMLHttpRequest();
}?
else
?
if
?(window.ActiveXObject)?{?
//
?IE
????http_request?
=
?
new
?ActiveXObject(
"
Microsoft.XMLHTTP
"
);
}
(由于這段程序僅供說明,所以是采最簡方式寫出。本文第三步中有另一種我們比較常用的寫法。)
有些版本的 Mozilla 瀏覽器在服務器送回的數據未含 XML mime-type 文件頭(header)時會出錯。為了避免這個問題,你可以用下列方法覆寫服務器傳回的檔頭,以免傳回的不是text/xml
。
http_request?
=
?
new
?XMLHttpRequest();
http_request.overrideMimeType('text
/
xml');
接下來是要決定服務器傳回資料后的處理方式,此時你只要以 onreadystatechange
這個屬性指明要處理傳回值的
JavaScript 函式名稱即可,例如:
http_request.onreadystatechange?
=
?nameOfTheFunction;
注意,指定的函式名稱后不加括號也沒有參數。除了指定函式名稱外,你也能用 Javascript 實時定義函式的方法來定一個新的處理函式,如下:
http_request.onreadystatechange?
=
?
function
(){
????
//
?做些事
};
決定處理方式之后你得確實發出 request,此時需叫用 HTTP request 類別的 open()
及 send()
方法,如下:
http_request.open('GET',?'http:
//
www.example.org/some.file',?true);
http_request.send(
null
);
-
open()
的第一個參數是
???? HTTP request 的方法,也就是從
???? GET、POST、HEAD 中擇一使用,亦可用你主機上支持的方式。為遵循 HTTP 標準,請記得這些方法都是大寫,不然有的瀏覽器(如 Firefox)或許不會理你。其它 HTTP request 可以支持的方法列表請參考
???? W3C 規格書 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html)。
- 第二個參數是目標 URL。基于安全考慮,你不能叫用同網域以外的網頁。如果網域不同,則叫用
???? open()
時會出現「權限不足,拒絕存取」那類的錯誤。通常大伙會犯的錯誤多為在 domain.tld 網的網站下呼叫 www.domain.tld 中的網頁,僅是一點點差別都不行。
????
- 第三個參數決定此 request 是否不同步進行,如果設定為
???? TRUE
則即使服務器尚未傳回數據也會繼續執行其余的程序,這也就是 AJAX 中第一個 A 代表的意義。
send()
的參數在以 POST 發出 request 時可以是任何想傳給服務器的東西,而數據則以查詢字符串的方式列出,例如:
?
name
=
value
&
anothername
=
othervalue
&
so
=
on
不過如果你想要以 POST 方式傳送數據,則必須先將 MIME 型態改好,如下:
http_request.setRequestHeader('Content
-
Type',?'application
/
x
-
www
-
form
-
urlencoded');
否則服務器就不會理你傳過來的數據了。
第二步 – 「就上咩!」(又稱為「處理服務器傳回的數據」)
傳出 request 時必須提供處理傳回值的函式名稱。
http_request.onreadystatechange?
=
?nameOfTheFunction;
////////////////////////////////////////////////////////////////////
//但是,FireFox 對onreadyStateChange沒有反應,怎么辦,這個方法不能用在
//FireFox 中,有沒有其它的方法?
//??????Added by www.besook.com 2006-03-19
//////////////////////////////////////////////////////////////
那么來看看這個函式該做些什么。首先,它必須檢查 request 目前的狀態:如果狀態值為 4 代表服務器已經傳回所有信息了,便可以開始解析所得信息。
if
?(http_request.readyState?
==
?
4
)?{
????
//
?一切?ok,?繼續解析
}?
else
?{
????
//
?還沒完成
}
readyState
所有可能的值如下:
- 0 (還沒開始)
- 1 (讀取中)
- 2 (已讀取)
- 3 (信息交換中)
- 4 (一切完成)
(資料來源: MSDN (http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp))
接下來要檢查服務器傳回的 HTTP 狀態碼。所有狀態碼列表可于 W3C
網站 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)上查到,但我們要管的是200 OK
這種狀態。
if
?(http_request.status?
==
?
200
)?{
????
//
?萬事具備
}?
else
?{
????
//
?似乎有點問題,或許服務器傳回了?404?(查無此頁)?或者?500?(內部錯誤)?什么的
}
檢查傳回的 HTTP 狀態碼后,要怎么處理傳回的數據就由你決定了。有兩種存取數據的方式:
-
http_request.responseText
– 這樣會把傳回值當字符串用
????
-
http_request.responseXML
– 這樣會把傳回值視為
???? XMLDocument
對象,而后可用
???? JavaScript DOM 相關函式處理
第三步 - 萬事俱備 - 簡單范例
好,接著就做一次簡單的 HTTP 范例,演示方才的各項技巧。這段 JavaScript 會向服務器要一份里頭有「I'm
a test.」字樣的 HTML 文件(test.html
),而后以 alert()
將文件內容列出。
<
script?type
=
"
text/javascript
"
?language
=
"
javascript
"
>
????
var
?http_request?
=
?
false
;
????
function
?makeRequest(url)?{
????????http_request?
=
?
false
;
????????
if
?(window.XMLHttpRequest)?{?
//
?Mozilla,?Safari,
????????????http_request?
=
?
new
?XMLHttpRequest();
????????????
if
?(http_request.overrideMimeType)?{
????????????????http_request.overrideMimeType('text
/
xml');
????????????}
????????}?
else
?
if
?(window.ActiveXObject)?{?
//
?IE
????????????
try
?{
????????????????http_request?
=
?
new
?ActiveXObject(
"
Msxml2.XMLHTTP
"
);
????????????}?
catch
?(e)?{
????????????????
try
?{
????????????????????http_request?
=
?
new
?ActiveXObject(
"
Microsoft.XMLHTTP
"
);
????????????????}?
catch
?(e)?{}
????????????}
????????}
????????
if
?(
!
http_request)?{
????????????alert('Giving?up?:(?Cannot?create?an?XMLHTTP?instance');
????????????
return
?
false
;
????????}
????????http_request.onreadystatechange?
=
?alertContents;
????????http_request.open('GET',?url,?
true
);
????????http_request.send(
null
);
????}
????
function
?alertContents()?{
????????
if
?(http_request.readyState?
==
?
4
)?{
????????????
if
?(http_request.status?
==
?
200
)?{
????????????????alert(http_request.responseText);
????????????}?
else
?{
????????????????alert('There?was?a?problem?
with
?the?request.');
????????????}
????????}
????}
</
script
>
<
span
????style
=
"
cursor:?pointer;?text-decoration:?underline
"
????onclick
=
"
makeRequest('test.html')
"
>
????????Make?a?request
</
span
>
在此范例中:
- 首先使用者按下「Make a request」
- 這么一來就會呼叫
???? makeRequest()
函式,亦傳入參數值 test.html
(也就是那份 HTML 檔的名稱,放在同目錄下)
- 接著發出 request,而后會將主導權交給
???? onreadystatechange
指定的 alertContents()
函式
-
alertContents()
檢查響應是否正常,而后以 alert()
將 test.html
的內容列出
你可以由此測試本例 (http://www.w3clubs.com/mozdev/httprequest_test.html),也可以參考測試檔案 (http://www.w3clubs.com/mozdev/test.html)。
第四步 – 「X 檔案」(又稱為「處理 XML 響應值」)
前面的例子中,在收到 HTTP 傳回值后我們以對象的 reponseText
屬性使用 test.html
檔案的內容,接著來試試 responseXML
屬性的方法。
首先,我們得做個格式正確的 XML 文件,以便稍后取用。此檔名喚 test.xml
,內容如下:
<?
xml?version="1.0"?
?>
<
root
>
????I'm?a?test.
</
root
>
在程序中,我們叫用檔案的地方只須略事修改如下:
...
onclick
=
"
makeRequest('test.xml')
"
...
接著在 alertContents()
中,我們必須將 alert(http_request.responseText);
改成這樣:
var
?xmldoc?
=
?http_request.responseXML;
var
?root_node?
=
?xmldoc.getElementsByTagName('root').item(
0
);
alert(root_node.firstChild.data);
這樣一來我們便可取得
responseXML
所傳回的
XMLDocument
對象,而后以 DOM 相關的方法取用
XML 文件內容。你可以參考
test.xml
的原始碼 (
http://www.w3clubs.com/mozdev/test.xml)
以及修改過后的
測試程序 (
http://www.w3clubs.com/mozdev/httprequest_test_xml.html)。
例如數據庫中某一字段存了這樣一個數:3.1415926
但實際中只需要精確取到小數點后兩位的數字:3.14
那就可以這樣取出:
select cast(3.1415926 as decimal(9,2)) as value
如果要取精確到小數點后5位,只須改成:
select cast(3.1415926 as decimal(9,5)) as value。