Monday, October 29, 2007

今天在deploy一個ejb, 裡面因為用到spring所以把他的manifest.mf改了改, 想說可以把dependent的jar塞進去這個ejb的jar. 原本想說沒啥問題, 但今天弄了一整天, 才發現這句關鍵的句子害我感到都浪費了
Use the manifest file to specify that a JAR file can reference another JAR file.
Standalone EJBs cannot use the Manifest Class-Path. It is only supported for
components that are deployed within an EAR file
. The clients should reference
the client.jar in the classpath entry of the manifest file.

真讓我感到傷心, 原來是因為這樣, 不過超神奇的還是weblogic如果直接選原本的檔案, 卻又活的好好的, 讓我感到很迷惘阿.
雖然可以找到的解法很多, 像是直接把classpath塞到weblogic的classpath裡面, 但是想說不要動server的設定比較好, 結果就讓我耗了一天了!! 可惡~~

Friday, October 26, 2007

web.xml的內容

Web.xml是一在web application deploy的時候必要的file, 它是被放置於/WEB-INF/之下的. 由於它是個xml file, 所以檔案內的大小寫有別. 另外, 所有element排放的順序是規定的.

1. Head: 檔案的開頭必定為xml head, 大多為:
<?xml version="1.0" encoding="UTF-8" ?>
2. DOCTYPE: 告訴server servlet的版本(是2.2或是2.3)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" http://java.sun.com/dtd/web-app_2_3.dtd&gt>
3. Root: 必定為<web-app> 且一定為小寫. 如:
<web-app>
<!-- Other elements go here. All are optional. -->
</web-app>
4. Elements: 順序必定為以下順序:
  • icon
  • display-name
  • description
  • distributable
  • context-param
  • filter
  • filter-mapping
  • listener
  • servlet
  • servlet-mapping
  • session-config
  • mine-mapping
  • welcome-file-list
  • error-page
  • taglib
  • resource-env-ref
  • resource-ref
  • security-constraint
  • login-config
  • security-role
  • env-entry
  • ejb-ref
  • ejb-local-ref

詳細內容可以參考dtd的定義, 下面指列出一些我常用的elements以及一些注意事項.

1. servlet: 常用servlet-name以及servlet-class這兩個subelements. servlet-name一定要先出現, 才能出現servlet-class. 它的影響有兩個a). initialization parameters, custom URL patterns, and other customizations refer to the servlet by the registered name, not by the class name. b). the name can be used in the URL instead of the class name. 也就是我可以用http://host/webAppPrefix/servlet/xxx代替http://host/webAppPrefix/servlet/package.yyyServlet來連結, 其中xxx是設定的servlet-name, package.yyyServlet是servlet的class. 另外, 若是此servlet為一個jsp檔案的時候, 請改用jsp-file這個tag去代替servlet-class, 因為我們無法得知jsp被編譯之後的class名稱為何. 而一個container當中可以有很多個servlet, 所以在web.xml當中也可以同時定義很多個servlet.

2. servlet-mapping: 用於客制化URL, 一般結構為:

<servlet-mapping> <servlet-name>name</servlet-name> <url-pattern>url</url-pattern> </servlet-mapping>
其中name就是在servlet中所定義的servlet-name, 而url則為一個pattern,大致有下面幾種形式
a).路徑映射: 以/開頭或是以/*結尾
b).擴展映射: 以前綴*.開頭
c).default servlet映射: 使用/
d).詳細映射: 如ab/tt/cc.action
因此定義/*.action會出錯的原因,是由於同時有路徑mapping也有擴展的mapping, 所以container無法判斷

3. 使用servlet的initialization parameters. 一般型態為類似於下面的格式:

<servlet>
<servlet-name>InitTest</servlet-name>
<servlet-class>myPackage.InitServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>Value 1</param-value>
</init-param>
</servlet>
Initialization parameters are only available whenservlets are accessed by means of their registered names or throughcustom URL patterns associated with their registered names. Initialization parameters are not available in servlets that are accessedby their default URL. 而自己撰寫servlet的init()時可以利用getServletConfig().getInitParameter("...") 來取得type為String的parameter.

4.context-param可以設定提供給整個system, 裡面包含的subelement為context-name以及context-value.

5.若要將servlet在system啟動時就load進來, 可以使用load-on-starup這個tag

6.Welcome page的設定是透過welcome-file-list這個element來達到, 而放置於當中的welcome-file是有順序可言的, 若是第一個找不到, 則會找第二個, 以此類推, 直到沒有則使用server所設定的.

7.

當Spring碰上EJB

最近都在學一堆JAVA的framework, 對很多人來說, 這都不是新東西. 不過依舊多少會碰到一些問題. 所以就順手把一些常用的筆記寫下來了.

記得第一個碰到的是怎麼利用spring去config EJB. 方法有很多種, 在此簡單寫一下我的做法.

在Spring當中, 它提供三種abstract class, 形同EJB的bean class.








abstract class for bean in Spring
In Spring EJB
AbstractStatelessSessionBean Stateless Session Bean
AbstractStatefulSessionBean Stateful Session Bean
AbstractMessageDrivenBean Message Driven Bean

以上皆源自於AbstractEnterpriseBean

需要的步驟如下:

1. BeanFactoryLocator: 他是個interface, 必須有Function: BeanFactoryReference userBeanFactory (String factoryKey) throws BeansException;

2. ServiceInterface以及他的implementation

3. EJBObject的Interface要extends ServiceInterfaceEJBObject

4. EJBHome: 要extends EJBHome
,要記得function create()要return 3的type

5. Bean: 根據需求, extends上表當中的class, 最重要的事情是要有onEjbCreate() throws CreateException這個function.

6. Finally, 要在deployment descriptor當中(也就是ejb-jar.xml檔案)當中加入中的<env-entry-name>, <env-entry-type>,<env-entry-value>.


<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>spring-config.xml</env-entry-value>
</env-entry>

Tuesday, October 09, 2007

ClassNotFoundException: org.hibernate.hql.ast.HqlToken解决之道

From http://www.blogjava.net/SkyWinder/articles/40306.html
拥有Hibernate3.jar的应用,被部署到weblogic8.1上后,抛出异常 CharScanner; panic: ClassNotFoundException: org.hibernate.hql.ast.HqlToken。 解决方法:在hibernate.properties上,或是在spring的conext xml中,加上一个属性hibernate.query.factory_class,值为org.hibernate.hql.classic.ClassicQueryTranslatorFactory。
原因:从网上获知,weblogic.jar中已经有了一个antlr.jar的版本,导致应用中hibernate3.jar中用到的antlr.jar不能找到,导致异常。