Sun River
          Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
          posts - 78,comments - 0,trackbacks - 0
              只有注冊(cè)用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2009-03-10 11:42 Sun River| 編輯 收藏

           What are JavaScript types? - Number, String, Boolean, Function, Object, Null, Undefined.

          1. How do you convert numbers between different bases in JavaScript? - Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);
          2. What does isNaN function do? - Return true if the argument is not a number.
          3. What is negative infinity? - It’s a number in JavaScript, derived by dividing negative number by zero.

          4. What boolean operators does JavaScript support? - &&, || and !
          5. What does "1"+2+4 evaluate to? - Since 1 is a string, everything is a string, so the result is 124.
          6. How about 2+5+"8"? - Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.
          7. What looping structures are there in JavaScript? - for, while, do-while loops, but no foreach.
          8. How do you create a new object in JavaScript? - var obj = new Object(); or var obj = {};
          9. How do you assign object properties? - obj["age"] = 17 or obj.age = 17.
          10. What’s a way to append a value to an array? - arr[arr.length] = value;
          11. What is this keyword? - It refers to the current object.
          posted @ 2009-03-10 11:37 Sun River| 編輯 收藏
               摘要:   閱讀全文
          posted @ 2009-03-10 11:36 Sun River| 編輯 收藏

          (1)假設(shè)在helloapp應(yīng)用中有一個(gè)hello.jsp,它的文件路徑如下:
          %CATALINA_HOME%/webapps/helloapp/hello/hello.jsp????
          那么在瀏覽器端訪問hello.jsp的URL是什么? (單選)
          選項(xiàng):
          (A) http://localhost:8080/hello.jsp
          (B) http://localhost:8080/helloapp/hello.jsp
          (C) http://localhost:8080/helloapp/hello/hello.jsp

          (2)假設(shè)在helloapp應(yīng)用中有一個(gè)HelloServlet類,它位于org.javathinker包下,那么這個(gè)類的class文件應(yīng)該放

          在什么目錄下? (單選)
          選項(xiàng):
          (A) helloapp/HelloServlet.class
          (B) helloapp/WEB-INF/HelloServlet.class
          (C) helloapp/WEB-INF/classes/HelloServlet.class
          (D) helloapp/WEB-INF/classes/org/javathinker/HelloServlet.class

          (3)假設(shè)在helloapp應(yīng)用中有一個(gè)HelloServlet類,它在web.xml文件中的配置如下:
          <servlet>
          ??<servlet-name> HelloServlet </servlet-name>
          ??<servlet-class>org.javathinker.HelloServlet</servlet-class>
          </servlet>

          <servlet-mapping>
          ??<servlet-name> HelloServlet </servlet-name>
          ??<url-pattern>/hello</url-pattern>
          </servlet-mapping>
          那么在瀏覽器端訪問HelloServlet的URL是什么? (單選)
          選項(xiàng):
          (A) http://localhost:8080/HelloServlet
          (B) http://localhost:8080/helloapp/HelloServlet
          (C) http://localhost:8080/helloapp/org/javathinker/hello
          (D) http://localhost:8080/helloapp/hello


          (4)客戶請(qǐng)求訪問HTML頁(yè)面與訪問Servlet有什么異同?(多選)
          選項(xiàng):
          (A)相同:都使用HTTP協(xié)議
          (B)區(qū)別:前者Web服務(wù)器直接返回HTML頁(yè)面,后者Web服務(wù)器調(diào)用Servlet的方法,由Servlet動(dòng)態(tài)生成HTML頁(yè)面
          (C)相同:前者Web服務(wù)器直接返回HTML頁(yè)面,后者Web服務(wù)器直接返回Servlet的源代碼。
          (D)區(qū)別:后者需要在web.xml中配置URL路徑。
          (E)區(qū)別:前者使用HTTP協(xié)議,后者使用RMI協(xié)議。


          (5)HttpServletRequest對(duì)象是由誰(shuí)創(chuàng)建的?(單選)
          選項(xiàng):
          (A)由Servlet容器負(fù)責(zé)創(chuàng)建,對(duì)于每個(gè)HTTP請(qǐng)求, Servlet容器都會(huì)創(chuàng)建一個(gè)HttpServletRequest對(duì)象
          (B)由JavaWeb應(yīng)用的Servlet或JSP組件負(fù)責(zé)創(chuàng)建,當(dāng)Servlet或JSP組件響應(yīng)HTTP請(qǐng)求時(shí),先創(chuàng)建

          HttpServletRequest對(duì)象


          (6)從HTTP請(qǐng)求中,獲得請(qǐng)求參數(shù),應(yīng)該調(diào)用哪個(gè)方法? (單選)
          選項(xiàng):
          (A)調(diào)用HttpServletRequest對(duì)象的getAttribute()方法
          (B)調(diào)用ServletContext對(duì)象的getAttribute()方法
          (C)調(diào)用HttpServletRequest對(duì)象的getParameter()方法

          (7)ServletContext對(duì)象是由誰(shuí)創(chuàng)建的?(單選)
          選項(xiàng):
          (A)由Servlet容器負(fù)責(zé)創(chuàng)建,對(duì)于每個(gè)HTTP請(qǐng)求, Servlet容器都會(huì)創(chuàng)建一個(gè)ServletContext對(duì)象
          (B)由JavaWeb應(yīng)用本身負(fù)責(zé)為自己創(chuàng)建一個(gè)ServletContext對(duì)象
          (C)由Servlet容器負(fù)責(zé)創(chuàng)建,對(duì)于每個(gè)JavaWeb應(yīng)用,在啟動(dòng)時(shí),Servlet容器都會(huì)創(chuàng)建一個(gè)ServletContext對(duì)象


          (8)jspForward1.jsp要把請(qǐng)求轉(zhuǎn)發(fā)給jspForward2.jsp,應(yīng)該在jspForward1.jsp中如何實(shí)現(xiàn)? (單選)
          選項(xiàng):
          (A) <a href=“jspForward2.jsp”>jspForward2.jsp </a>
          (B) <jsp:forward page=“jspForward2.jsp”>

          (9)當(dāng)瀏覽器第二次訪問以下JSP網(wǎng)頁(yè)時(shí)的輸出結(jié)果是什么?(單選)

          <!% int a=0;???? %>
          <%
          ???? int b=0;
          ???? a++;
          ???? b++;
          %>

          a:<%= a %> <br>
          b:<%= b %>
          ??
          選項(xiàng):
          (A)??a=0??b=0
          (B) a=1??b=1
          (c) a=2??b=1

          (10)下面哪個(gè)說法是正確的? (單選)
          選項(xiàng):
          (A) 對(duì)于每個(gè)要求訪問maillogin.jsp的HTTP請(qǐng)求,Servlet容器都會(huì)創(chuàng)建一個(gè)HttpSession對(duì)象
          (B)每個(gè)HttpSession對(duì)象都有惟一的ID。
          (C)JavaWeb應(yīng)用程序必須負(fù)責(zé)為HttpSession分配惟一的ID

          (11)如果不希望JSP網(wǎng)頁(yè)支持Session,應(yīng)該如何辦? (單選)
          選項(xiàng):
          (A) 調(diào)用HttpSession的invalidate()方法
          (B) <%@ page session= “false\">


          (12)在標(biāo)簽處理類中,如何訪問session范圍內(nèi)的共享數(shù)據(jù)? (多選)
          選項(xiàng):
          (A)在TagSupport類中定義了session成員變量,直接調(diào)用它的getAttribute()方法即可。
          (B)在標(biāo)簽處理類TagSupport類中定義了pageContext成員變量,先通過它的getSession()方法獲得當(dāng)前的

          HttpSession對(duì)象,再調(diào)用HttpSession對(duì)象的getAttribute()方法。
          (C)pageContext.getAttribute(“attributename”,PageContext.SESSION_SCOPE)


          (13)在下面的選項(xiàng)中,哪些是TagSupport類的doStartTag()方法的有效返回值? (多選)
          選項(xiàng):
          (A) Tag.SKIP_BODY
          (B) Tag.SKIY_PAGE
          (C) Tag.EVAL_BODY_INCLUDE
          (D) Tag.EVAL_PAGE


          (14)以下代碼能否編譯通過,假如能編譯通過,運(yùn)行時(shí)得到什么打印結(jié)果?(單選)
          request.setAttribute(\"count\",new Integer(0));
          Integer count = request.getAttribute(\"count\");
          選項(xiàng):
          A)不能編譯通過??B)能編譯通過,并正常運(yùn)行??
          C) 編譯通過,但運(yùn)行時(shí)拋出ClassCastException

          答案:
          (1)C (2)D??(3)D??(4)A,B,D??(5)A??(6)C??(7)C??(8)B??(9)C?? (10)B
          (11)B??(12)B,C??(13)A,C??(14)A

          判斷題:(其它試題請(qǐng)下載)

          1 . Java 程序里 , 創(chuàng)建新的類對(duì)象用關(guān)鍵字 new ,回收無(wú)用的類對(duì)象使用關(guān)鍵字 free 。

          2 .對(duì)象可以賦值,只要使用賦值號(hào)(等號(hào))即可,相當(dāng)于生成了一個(gè)各屬性與賦值對(duì)象相同的新對(duì)象。

          3 .有的類定義時(shí)可以不定義構(gòu)造函數(shù),所以構(gòu)造函數(shù)不是必需的。

          4 .類及其屬性、方法可以同時(shí)有一個(gè)以上的修飾符來修飾。

          5 . Java 的屏幕坐標(biāo)是以像素為單位,容器的左下角被確定為坐標(biāo)的起點(diǎn)

          6 .抽象方法必須在抽象類中,所以抽象類中的方法都必須是抽象方法。

          7 . Final 類中的屬性和方法都必須被 final 修飾符修飾。

          8 .最終類不能派生子類,最終方法不能被覆蓋。

          9 .子類要調(diào)用父類的方法,必須使用 super 關(guān)鍵字。

          10 .一個(gè) Java 類可以有多個(gè)父類。

          11 .如果 p 是父類 Parent 的對(duì)象,而 c 是子類 Child 的對(duì)象,則語(yǔ)句 c = p 是正確的。

          12 .一個(gè)類如果實(shí)現(xiàn)了某個(gè)接口,那么它必須重載該接口中的所有方法。

          13 .當(dāng)一個(gè)方法在運(yùn)行過程中產(chǎn)生一個(gè)異常,則這個(gè)方法會(huì)終止,但是整個(gè)程序不一定終止運(yùn)行。

          14 .接口是特殊的類,所以接口也可以繼承,子接口將繼承父接口的所有常量和抽象方法。

          15 .用“ + ”可以實(shí)現(xiàn)字符串的拼接,用 - 可以從一個(gè)字符串中去除一個(gè)字符子串。

          16 .使用方法 length( ) 可以獲得字符串或數(shù)組的長(zhǎng)度。

          17 .設(shè) String 對(duì)象 s=”Hello ” ,運(yùn)行語(yǔ)句 System.out.println(s.concat(“World!”)); 后 String 對(duì)象 s 的內(nèi)容為 ”Hello world!” ,所以語(yǔ)句輸出為

          Hello world!

          18 .創(chuàng)建 Vector 對(duì)象時(shí)構(gòu)造函數(shù)給定的是其中可以包容的元素個(gè)數(shù),使用中應(yīng)注意不能超越這個(gè)數(shù)值。

          19 .所有的鼠標(biāo)事件都由 MouseListener 監(jiān)聽接口的監(jiān)聽者來處理。

          20 .一個(gè)容器中可以混合使用多種布局策略。

          21 . Java 中,并非每個(gè)事件類都只對(duì)應(yīng)一個(gè)事件。

          22 .一個(gè)線程對(duì)象的具體操作是由 run() 方法的內(nèi)容確定的,但是 Thread 類的 run() 方法是空的 , 其中沒有內(nèi)容 ; 所以用戶程序要么派生一個(gè) Thread 的子類并在子類里重新定義 run() 方法 , 要么使一個(gè)類實(shí)現(xiàn) Runnable 接口并書寫其中 run() 方法的方法體。

          23 . Java 的源代碼中定義幾個(gè)類,編譯結(jié)果就生成幾個(gè)以 .class 為后綴的字節(jié)碼文件。

          24 . Java Applet 是由獨(dú)立的解釋器程序來運(yùn)行的。

          25 . Java Applet 只能在圖形界面下工作。

          26 . Java 的字符類型采用的是 ASCII 編碼。

          27 . Java 的各種數(shù)據(jù)類型占用固定長(zhǎng)度,與具體的軟硬件平臺(tái)環(huán)境無(wú)關(guān)

          28 . Applet 是一種特殊的 Panel ,它是 Java Applet 程序的最外層容器。

          29 .子類的域和方法的數(shù)目一定大于等于父類的域和方法的數(shù)目。

          30 . System 類不能實(shí)例化,即不能創(chuàng)建 System 類的對(duì)象。

          31 .用戶自定義的圖形界面元素也可以響應(yīng)用戶的動(dòng)作,具有交互功能

          32 . Java 中數(shù)組的元素可以是簡(jiǎn)單數(shù)據(jù)類型的量,也可以是某一類的對(duì)象。

          33 . Vector 類中的對(duì)象不能是簡(jiǎn)單數(shù)據(jù)類型。

          34 . Java 中的 String 類的對(duì)象既可以是字符串常量,也可以是字符串變量。

          35 .容器是用來組織其他界面成分和元素的單元,它不能嵌套其他容器。

          posted @ 2007-02-22 11:59 Sun River| 編輯 收藏
               摘要: Part I. XSLT Programming ? 1)?? What is the exac...  閱讀全文
          posted @ 2006-11-27 08:00 Sun River| 編輯 收藏
               摘要:   閱讀全文
          posted @ 2006-10-28 15:15 Sun River| 編輯 收藏
               摘要:   閱讀全文
          posted @ 2006-09-18 11:51 Sun River| 編輯 收藏

          Question How do you delete a Cookie within a JSP? (JSP)

          Answer

          Cookie mycook = new Cookie("name","value");

          response.addCookie(mycook);

          Cookie killmycook = new Cookie("mycook","value");

          killmycook.setMaxAge(0);

          killmycook.setPath("/");

          killmycook.addCookie(killmycook);

          Question How many types of protocol implementations does RMI have? (RMI)

          Answer RMI has at least three protocol implementations: Java

          Remote Method Protocol(JRMP), Internet Inter ORB Protocol(IIOP),

          and Jini Extensible Remote Invocation(JERI). These are alternatives,

          not part of the same thing, All three are indeed layer 6 protocols for

          those who are still speaking OSI reference model.

          Question What are the different identifier states of a Thread?

          (Core Java)

          Answer The different identifiers of a Thread are:

          R - Running or runnable thread

          S - Suspended thread

          CW - Thread waiting on a condition variable

          MW - Thread waiting on a monitor lock

          MS - Thread suspended waiting on a monitor lock


          Question What is the fastest type of JDBC driver? (JDBC)

          Answer JDBC driver performance will depend on a number of

          issues:

          (a) the quality of the driver code,

          (b) the size of the driver code,

          (c) the database server and its load,

          (d) network topology,

          (e) the number of times your request is translated to a different API.

          In general, all things being equal, you can assume that the more your

          request and response change hands, the slower it will be. This

          means that Type 1 and Type 3 drivers will be slower than Type 2

          drivers (the database calls are make at least three translations versus

          two), and Type 4 drivers are the fastest (only one translation).

          Question Request parameter How to find whether a parameter

          exists in the request object? (Servlets)

          Answer 1.boolean hasFoo = !(request.getParameter("foo") ==

          null || request.getParameter("foo").equals(""));

          2. boolean hasParameter =

          request.getParameterMap().contains(theParameter);

          (which works in Servlet 2.3+)


          Question How can I send user authentication information while

          makingURLConnection? (Servlets)

          Answer You’ll want to use

          HttpURLConnection.setRequestProperty and set all the appropriate

          headers to HTTP authorization.

          Question How do I convert a numeric IP address like 192.18.97.39

          into a hostname like java.sun.com? (Networking)

          Answer

          Question How many methods do u implement if implement the

          Serializable Interface? (Core Java)

          Answer The Serializable interface is just a "marker" interface,

          with no methods of its own to implement. Other ’marker’ interfaces

          are

          java.rmi.Remote

          java.util.EventListener

          String hostname =InetAddress.getByName("192.18.97.39").getHostName();

          posted @ 2010-10-25 17:08 Sun River| 編輯 收藏
          1.

          Question What is the query used to display all tables names in

          SQL Server (Query analyzer)? (JDBC)

          Answer select * from information_schema.tables

          Question What is Externalizable? (Core Java)

          Answer Externalizable is an Interface that extends Serializable

          Interface. And sends data into Streams in Compressed Format. It has

          two methods, writeExternal(ObjectOuput out) and

          readExternal(ObjectInput in).

          Question What modifiers are allowed for methods in an Interface?

          Answer Only public and abstract modifiers are allowed for

          methods in interfaces.

          Question How many types of JDBC Drivers are present and what

          are they? (JDBC)

          Answer There are 4 types of JDBC Drivers

          Type 1: JDBC-ODBC Bridge Driver

          Type 2: Native API Partly Java Driver

          Type 3: Network protocol Driver

          Type 4: JDBC Net pure Java Driver

          Question What is the difference between ServletContext and

          PageContext? (JSP)

          Answer ServletContext: Gives the information about the container

          PageContext: Gives the information about the Request.

          Question How to pass information from JSP to included JSP?

          Answer Using <%jsp:param> tag.

          posted @ 2010-10-25 16:07 Sun River| 編輯 收藏

           

          tomcat6配置雙向認(rèn)證

          1
          、生成服務(wù)器端證書

          keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650


          2
          、生成客戶端證書

          keytool -genkey -keyalg RSA -dname "cn=sango,ou=sango,o=none,l=china,st=beijing,c=cn" -alias custom -storetype PKCS12 -keypass password -keystore custom.p12 -storepass password -validity 3650


          客戶端的CN可以是任意值。
          3
          、由于是雙向SSL認(rèn)證,服務(wù)器必須要信任客戶端證書,因此,必須把客戶端證書添加為服務(wù)器的信任認(rèn)證。由于不能直接將PKCS12格式的證書庫(kù)導(dǎo)入,我們必須先把客戶端證書導(dǎo)出為一個(gè)單獨(dú)的CER文件,使用如下命令,先把客戶端證書導(dǎo)出為一個(gè)單獨(dú)的cer文件:

          keytool -export -alias custom -file custom.cer -keystore custom.p12 -storepass password -storetype PKCS12 -rfc


          然后,添加客戶端證書到服務(wù)器中(將已簽名數(shù)字證書導(dǎo)入密鑰庫(kù))

          keytool -import -v -alias custom -file custom.cer -keystore server.jks -storepass password


          4
          、查看證書內(nèi)容

          keytool -list -v -keystore server.jks -storepass password


          5
          、配置tomcat service.xml文件

          <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
              maxThreads="150" scheme="https" secure="true"
              clientAuth="true" sslProtocol="TLS"
              keystoreFile="D:/server.jks" keystorePass="password"
              truststoreFile="D:/server.jks" truststorePass="password"
          />


          clientAuth="true"
          表示雙向認(rèn)證
          6
          、導(dǎo)入客戶端證書到瀏覽器
          雙向認(rèn)證需要強(qiáng)制驗(yàn)證客戶端證書。雙擊“custom.p12”即可將證書導(dǎo)入至IE

          tomcat6
          配置單向認(rèn)證

          1
          、生成服務(wù)器端證書

          keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650


          2
          、由于是單向認(rèn)證,沒有必要生成客戶端的證書,直接進(jìn)入配置tomcat service.xml文件

          <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
              maxThreads="150" scheme="https" secure="true"
              clientAuth="false" sslProtocol="TLS"
              keystoreFile="D:/server.jks" keystorePass="password"    
          />


          clientAuth="false"
          表示單向認(rèn)證,同時(shí)去掉truststoreFile="D:/server.jks" truststorePass="password"2

          posted @ 2010-05-11 12:12 Sun River| 編輯 收藏
           

          ---The key thing to know is that IDs identify a specific element and therefore must be unique on the page – you can only use a specific ID once per document. Many browsers do not enforce this rule but it is a basic rule of HTML/XHTML and should be observed. Classes mark elements as members of a group and can be used multiple times, so if you want to define a style which will be applied to multiple elements you should use a class instead.

           Notice that an ID's CSS is an HTML element, followed by a "#", and finally ID's name. The end result looks something like "element#idname". Also, be sure to absorb the fact that when an ID is used in HTML, we must use "id=name" instead of "class=name" to reference it!

          Why Did They Choose Those Names??

                 ID = A person's Identification (ID) is unique to one person.

                 Class = There are many people in a class.

          ID for Layout and Uniqueness

          Standards specify that any given ID name can only be referenced once within a page or document. From our experience, IDs are most commonly used correctly in CSS layouts. This makes sense because there are usually only one menu per page, one banner, and usually only one content pane.

          In Tizag.com CSS Layout Examples we have used IDs for the unique items mentioned above. View the CSS Code for our first layout example. Below are the unique IDs in our code.

          *       Menu - div#menuPane

          *       Content - div#content

          Answer: Classes vs IDs

          Use IDs when there is only one occurence per page. Use classes when there are one or more occurences per page.

          posted @ 2010-03-16 10:14 Sun River| 編輯 收藏
          --Spring的singleton是容器級(jí)的,我們一般說的singleton模式是JVM級(jí)的。所以singleton模式中,singleton的class在整個(gè)JVM中只有一個(gè)instance,Spring的Bean,你可以一個(gè)class配置多個(gè)Bean,這個(gè)class就有了多個(gè)instance。這個(gè)singleton是指在spring容器中,這個(gè)Bean是單實(shí)例的,是線程共享的。所以要求這些類都是線程安全的。也就是說,不能出現(xiàn)修改Bean屬性的方法,當(dāng)然除了設(shè)值得那些setter。只要滿足線程安全,這些bean都可以用singleton。而且我們?cè)诮^大多數(shù)使用上,也是這樣用的,包括dao,service。
          Beanfactory是Spring初始以靜態(tài)方式載入的,Spring的單例IOC是基于容器級(jí)的,所以這你都不用擔(dān)心與考慮.

          --應(yīng)用中對(duì)象有兩種,行為對(duì)象和數(shù)據(jù)對(duì)象,行為對(duì)象都要求是線程安全的!也就是允許單例的, 不管是dao 還是 service 對(duì)象,都是行為對(duì)象,行為對(duì)象不應(yīng)該引用非線程安全的對(duì)象做成員量,同時(shí)在應(yīng)用外部的資源(如文件,數(shù)據(jù)庫(kù)連接,session)時(shí),要先保證對(duì)這些東西的訪問是做了并發(fā)控制的!
            對(duì)于spring來講,<bean scope="singleton"/>或<bean singleton="true"/>都是保證對(duì)同一sesionfactory bean是單例的,也就是所謂 sessionfactory 范圍的.

          --這是一個(gè)真實(shí)的案例,我們?cè)陧?xiàng)目中使用Spring和ACEGI,我之所以選擇ACEGI,除了它對(duì)權(quán)限的良好控制外,
          我還看好它的SecurityContextHolder,通過代碼
          代碼
          1. Authentication auth = SecurityContextHolder.getContext().getAuthentication();   
          <script>render_code();</script>
          我可以很容易在系統(tǒng)任意一層得到用戶的信息,而不用把用戶信息在參數(shù)里傳來傳去,(這也是struts的缺點(diǎn)之一)
          但是我在每一次要得到用戶信息的時(shí)候都寫上面的一段代碼,未免有些麻煩,所以我在BaseService, BaseDao里都提供了如下方法:
          代碼
          1.  /**  
          2.  * get current login user info  
          3.  * @return UserInfo  
          4.  */  
          5. protected UserInfo getUserInfo()   
          6. {   
          7.     return getUserContext().getUserInfo();   
          8. }   
          9.   
          10. /**  
          11.  * get current login user context  
          12.  * @return UserContext  
          13.  */  
          14. protected UserContext getUserContext()   
          15. {   
          16.     Authentication auth = SecurityContextHolder.getContext().getAuthentication();   
          17.     return (UserContext) auth.getPrincipal();   
          18. }   
          <script>render_code();</script>
          這樣在其他的Service和Dao類里可以通過
          代碼
          1. super.getUserContext(), super.getUserInfo()   
          <script>render_code();</script>
          來得到用戶的信息,這也為問題的產(chǎn)生提供了溫床。請(qǐng)看如下代碼:
          代碼
          1. public class SomeServece extends BaseService implements SomeInterFace     
          2. {   
          3.     private UserInfo user = super.getUserInfo();   
          4.        
          5.     public someMethod()   
          6.     {   
          7.        int userID = this.user.getUserID();   
          8.        String userName = this.user.getUserName();   
          9.        //bla bla do something user userID and userNaem   
          10.     }   
          11. }       
          <script>render_code();</script>

           

          這段代碼在單元測(cè)試的時(shí)候不會(huì)用任何問題,但是在多用戶測(cè)試的情況下,你會(huì)發(fā)現(xiàn)任何調(diào)用SomeService里someMethod()方法
          的userID和userName都是同一個(gè)人,也就是第一個(gè)登陸的人的信息。Why?

          其根本原因是Spring的Bean在默認(rèn)情況下是Singleton的,Bean SomeServece的實(shí)例只會(huì)生成一份,也就是所SomeServece實(shí)例的user
          對(duì)象只會(huì)被初始化一次,就是第一次登陸人的信息,以后不會(huì)變了。所以BaseService想為開發(fā)提供方便,卻給開發(fā)帶來了風(fēng)險(xiǎn)

          正確的用法應(yīng)該是這樣的

          代碼
          1. public class SomeServece extends BaseService implements SomeInterFace     
          2. {   
          3.        
          4.        
          5.     public someMethod()   
          6.     {   
          7.        int userID = super.getUserInfo().getUserID();   
          8.        String userName = super.getUserInfo().getUserName();   
          9.        //bla bla do something user userID and userNaem   
          10.     }   
          posted @ 2009-04-08 12:12 Sun River| 編輯 收藏

          Architect (Java) Interview Questions

          General and general terms questions

          Architect interview is slightly different from all other interview types. Interviewer is looking for ability of the candidate to think independently on top of pure technical knowledge. Most of the questions are open-ended, prompting the interviewee to discussion about different aspects of Java development. Other side of the interview is general questions about position of the architect within the organization. Some questions do not have clear, direct or single answer and require discussion with the interviewer. On top of questions mentioned here you may be asked generic OO questions (what is class, what is polymorphism etc.)
          1. What distinguishes "good architecture" from "bad architecture"?
            This is an open-ended question. There are few aspects of "good" architecture:
            1. Shall address functional product requirements
            2. Shall address non-functional product requirements, such as performance, scalability, reliability, fault tolerance, availability, maintainability, extensibility
            3. Shall be simple and comprehendible (to support maintainability and extensibility)
            4. Shall be well structured (support multiple tiers, parallel development etc.)
            5. Shall be detailed enough to share with different levels of organizational structure (marketing, sales, development, management)
            "Bad" architecture is basically opposite to "good" architecture.
          2. How much experience do you have with Enterprise applications? Another variant of this questions is: "Tell me about projects where you worked with J2EE?" Yet another version: "What, when and how made using EJB?"
            Interviewer is looking for your experience with designing J2EE applications and your experience with J2EE technologies and general terms. This is often start of the discussion and bridge to the technical section of the questions.
          3. What is scalability?
          4. What is high-availability? How is it different from scalability?
          5. What is the fault tolerance?
          6. What resources are used to keep up to date with J2EE technology?
            You may mention design pattern books, such as "Core EJB Patterns" and web sites, such as http://www.theserverside.com

          Specific technical questions

          1. What modeling tools you are familiar with? What version of TogetherJ (Rational Rose etc.) have you used?
          2. If stateless session bean more scalable than stateful session beans?
            This is very popular questions that leads to some confusion. According to the second edition of "Core J2EE Patterns" and contrary to popular belief, stateful session beans are not less scalable than stateless session bean. The reason for that is life cycle of either type is controlled by Application Server and control of life cycle is what defines the scalability of the application
          3. What's the difference between EJB 1.1 and EJB 2.0?
            There are many differences. Some key points you want to mention are:
            1. New CMP model
            2. EJB Query Language
            3. Local interfaces
            4. EJBHome methods
            5. Message Driven Beans (MDB) support
          4. What transaction isolation levels do you know?
            none, repeatable read, read committed, read uncommitted, serializable
          5. What transaction attributes do you know?
            requires new, required, supports, not supported, mandatory, never
          6. What is the difference between optimistic lock and pessimistic lock?
            Optimistic lock is an implicit lock that tries to make best assumption about locking strategy and minimize time spent in lock of resource. Optimistic lock is usually implemented with some kind of timestamp strategy. Pessimistic lock is an explicit lock that set by client.
          7. What are entity beans. Are there any issues with them?
            Typical reaction to this question is very expressive answer that entity beans should not be used. There are many performancy implications with entity beans if used incorrectly. One of the famous problems are "n+1 call problem" Inter-entity bean call is very expensive operation and should be avoided.
          8. What core design patterns do you know?
            Architect must know at least some basic design patters used in J2EE development, e.g. Business Delegate, Session Facade, VO, List Handler, DTO, Composite Entity, etc.
          9. Where business logic should reside?
            Typical answer is "in business tier" This usually opens series of questions like: What is business logic, how to determine business logic, how business logic is different from persistent logic etc.
          10. What is JDO?
            JDO is Java Data Object - persistent framework that is alternative to idea of entity beans
          11. What is the difference between JSP and servlet? When to use what?
            JSP is compiled into servlet. JSP are better suit to view of information, while servlets are better for controlling stuff.
          12. Does the J2EE platform support nested transactions?
            No.
          13. Can you use synchronization primitives in my enterprise beans?
            No.
          14. Why all major application server vendors provide custom class loaders in addition to system jvm class loader?
            System one does not support hot deployment.

          Performance questions

          1. What are performance problems in J2EE and how to solve them?
          2. What are Entity beans performance pitfalls?
          3. What performance pattern do you know?

          Design Pattern questions

          1. Can you use singleton in EJB?
            Yes, but should not (explain why)
          2. What is MVC pattern and why M, V and C need to be separated?
          3. Describe Business Delegate pattern (or any other pattern)
          4. How to prevent double submission of the form from JSP page? (or describe Synchronizer Token pattern)
          posted @ 2009-03-17 11:51 Sun River| 編輯 收藏

          Interview Questions on UML and Design Patterns

          Why to use design patterns?
          Give examples of design patterns?
          What is UML?
          What are advantages of using UML?
          What is the need for modelling?
          Is it requiste to use UML in software projects?
          What are use cases? How did you capture use cases in your project?
          Explain the different types of UML diagrams ? sequence diagram , colloboration diagram etc
          What is the sequence of UML diagrams in project?
          What tools did you use for UML in your project?
          What is the difference between activity and sequence diagrams?
          What are deployment diagrams?
          What are the different object relationships ?
          What is the difference between composition and aggregation?
          Wheel acting as a part of car ? Is this composition or aggregation?

          posted @ 2009-03-17 11:43 Sun River| 編輯 收藏
           

          spring與自動(dòng)調(diào)度任務(wù)()

          面是自己自動(dòng)調(diào)度的一些學(xué)習(xí)。
          這里只采用jdk自帶的timer進(jìn)行的,準(zhǔn)備在下篇文章中用Quartz調(diào)度器。
          首先建立你自己要運(yùn)行的類。

          package com.duduli.li;

          public class Display {

              
          public void disp(){
                  System.out.println("
          自動(dòng)控制測(cè)試");
              }
          }

          一個(gè)簡(jiǎn)單的java bean,其中在這里你可以替換自己的任務(wù)。
          然后就是編寫調(diào)度程序,這里要繼承jdk中的TimerTask類,復(fù)寫他的run方法。

          package com.duduli.li;

          import java.util.TimerTask;

          public class AutoRan extends TimerTask {
              
          //set方法是springDI
              private Display display;
              
              
          public void setDisplay(Display display) {
                  
          this.display = display;
              }
              @Override
              
          public void run() {
                  display.disp();
              }
          }

          然后就是重要的一步,編寫applicationsContext.xml了。

          <?xml version="1.0" encoding="UTF-8"?>
          <beans
              
          xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
              
              
          <bean id="display"
                  class
          ="com.duduli.li.Display">
              
          </bean>
              
          <bean id="atuoRun"
                  class
          ="com.duduli.li.AutoRan">
                  
          <property name="display" ref="display"></property>
              
          </bean>
              
              
          <bean id="aR"
              class
          ="org.springframework.scheduling.timer.ScheduledTimerTask">
                  
          <property name="timerTask" ref="atuoRun"></property>
          <!--
          period
          多長(zhǎng)時(shí)間運(yùn)行一次,delay表示允許你當(dāng)任務(wù)第一次運(yùn)行前應(yīng)該等待多久
          -->

                  
          <property name="period" value="5000"></property>
                  
          <property name="delay" value="2000"></property>    
              
          </bean>
              
              
          <bean id="test"
              class
          ="org.springframework.scheduling.timer.TimerFactoryBean">
                  
          <property name="scheduledTimerTasks">
                      
          <list>
          <!--
          這里使用list,可以調(diào)度多個(gè)bean
          -->

                          
          <ref bean="aR"/>
                      
          </list>
                  
          </property>
              
          </bean>
          </beans>


          再來就是客戶端調(diào)度了。

          package com.duduli.li;

          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class Client {

              
          public static void main(String[] args) {
                  BeanFactory factory = 
          new ClassPathXmlApplicationContext("applicationContext.xml");
                  factory.getBean("test");
              }
          }

          spring與自動(dòng)調(diào)度任務(wù)()

          使用quartzspring自動(dòng)調(diào)度。
          具體實(shí)現(xiàn)bean

          package com.duduli.li.quartz;

          import java.util.Date;

          public class Display {

              @SuppressWarnings("deprecation")
              
          public void disp(){
                  System.out.println(
          new Date().getSeconds());
                  System.out.println("
          自動(dòng)控制測(cè)試");
              }
          }

          繼承quartzjobbean類:這個(gè)類和繼承Timer類類似

          package com.duduli.li.quartz;

          import org.quartz.JobExecutionContext;
          import org.quartz.JobExecutionException;
          import org.springframework.scheduling.quartz.QuartzJobBean;

          public class AutoRun extends QuartzJobBean{

              
          private Display  display;
              
              
          public void setDisplay(Display display) {
                  
          this.display = display;
              }

              @Override
              
          protected void executeInternal(JobExecutionContext arg0)
                      
          throws JobExecutionException {
                  display.disp();
              }
          }

          spring配置文件:

                              <!-- quartz進(jìn)行自動(dòng)調(diào)度 -->
          <!-- 具體實(shí)現(xiàn)類 -->
              
          <bean id="display2"    class="com.duduli.li.quartz.Display"></bean>
              
          <!-- spring對(duì)quartz的支持,Auto類實(shí)現(xiàn)quartzjob接口的類,jobDataAsMap是將實(shí)現(xiàn)類注入其中 -->
              
          <bean id="quartz" class="org.springframework.scheduling.quartz.JobDetailBean">
                  
          <property name="jobClass" value="com.duduli.li.quartz.AutoRun"/>
                  
          <property name="jobDataAsMap">
                      
          <map>
                          
          <entry key="display" value-ref="display2"></entry>
                      
          </map>
                  
          </property>
              
          </bean>
              
          <!-- spring對(duì)quartz的支持,對(duì)其值的設(shè)定 -->
              
          <bean id="simpleTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
                  
          <property name="jobDetail" ref="quartz"></property>
                  
          <property name="startDelay" value="2000"></property>
                  
          <property name="repeatInterval" value="2000"></property>
              
          </bean>
              
          <!-- 啟動(dòng)自動(dòng)調(diào)度 -->
              
          <bean id="quartzTest" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                  
          <property name="triggers">
                      
          <list>
                          
          <ref bean="simpleTask"/>
                      
          </list>
                  
          </property>
              
          </bean>

          client調(diào)用:

          package com.duduli.li.quartz;


          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class Client {

              
          public static void main(String[] args) {
                      BeanFactory factory = 
          new ClassPathXmlApplicationContext("applicationContext.xml");
                      factory.getBean("quartzTest");
                  }
          }

          posted @ 2009-03-12 12:42 Sun River| 編輯 收藏

          Java implements a very efficient interprocess communication which reduces the CPU’s idle time to a very great extent. It is been implemented through wait ( ), notify ( ) and notifyAll ( ) methods. Since these methods are implemented as final methods they are present in all the classes.

          The basic functionality of each one of them is as under:


          wait( ) acts as a intimation to the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).

          notify( ) is used as intimator to wake up the first thread that called wait( ) on the same object.

          notifyAll( ) as the term states wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.


          public class WaitNotifyAllExample {
           
          public static void main(String[] args) {
           
          try {
          Object o = new Object();
          Thread thread1 = new Thread(new MyOwnRunnable("A", o));
          Thread thread2 = new Thread(new MyOwnRunnable("B", o));
          Thread thread3 = new Thread(new MyOwnRunnable("C", o));
           
          // synchronized keyword acquires lock on the object.
          synchronized (o) {
          thread1.start();
          // wait till the first thread completes execution.
          // thread should acquire the lock on the object
          // before calling wait method on it. Otherwise it will
          // throw java.lang.IllegalMonitorStateException 
          o.wait();
          thread2.start();
          // wait till the second thread completes execution
          o.wait();
          thread3.start();
          }
           
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
           
          }
          }
           
          class MyOwnRunnable implements Runnable {
           
          private String threadName;
           
          private Object o;
           
          public MyOwnRunnable(String name, Object o) {
          threadName = name;
          this.o = o;
          }
           
          public void run() {
           
           
          synchronized (o) {
          for (int i = 0; i < 1000; i++) {
          System.out.println("Thread " + threadName + " Count : " + i);
          }
          // notify all threads waiting for the object o.
          // thread should acquire the lock on the object
          // before calling notify or notifyAll method on it. 
          // Otherwise it will throw java.lang.IllegalMonitorStateException 
          o.notifyAll();
          }
          }
          }
          posted @ 2009-03-12 12:09 Sun River| 編輯 收藏
              只有注冊(cè)用戶登錄后才能閱讀該文。閱讀全文
          posted @ 2009-03-10 11:42 Sun River| 編輯 收藏
          僅列出標(biāo)題  下一頁(yè)
          主站蜘蛛池模板: 大庆市| 雅安市| 左贡县| 安达市| 大安市| 夏河县| 金华市| 临沭县| 保山市| 昌平区| 静乐县| 都安| 扎鲁特旗| 江山市| 沁源县| 临泽县| 高州市| 开封市| 集安市| 博湖县| 安阳市| 柳林县| 博白县| 辽阳市| 翁源县| 新建县| 西充县| 固镇县| 平乐县| 宽城| 商河县| 乡城县| 兴海县| 额济纳旗| 德兴市| 永州市| 清涧县| 若羌县| 正安县| 汉寿县| 马山县|