Netbeans project with UTF-8

基本上,NetBeans預設的file encoding是系統語系,所以在繁中版Windows是MS950,遇到”堃”、”綉”等字時會發生錯誤。鳥毅這幾年都用UTF-8編碼,必須手動改 Tools->Options->Advanced Options->Editing->Java Sources->Default EncodingUTF-8。但這樣編譯時會錯,因此還得改Project的Properties->Build->Compiling->Additional Compiler Options加上 -encoding UTF-8

來源鳥毅的Blog

Advertisements

Icefaces Application on Tomcat 6 using Netbeans

Similar to the portlet development but simpler.

download latest icefaces 1.7 RC1

pack these in library manager as Icefaces 1.7 library

backport-util-concurrent.jar

commons-beanutils.jar

commons-digester.jar

commons-discovery.jar

commons-fileupload.jar

commons-logging.jar

icefaces.jar

icefaces-comps.jar

Create a web application project and use JavaServerFace framework.

include Icefaces 1.7 library that we create.

Add these to your web.xml

   <servlet>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <servlet-class>
            com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet
        </servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Blocking Servlet</servlet-name>
        <servlet-class>com.icesoft.faces.webapp.xmlhttp.BlockingServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <url-pattern>*.iface</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <url-pattern>/xmlhttp/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Blocking Servlet</servlet-name>
        <url-pattern>/block/*</url-pattern>
    </servlet-mapping>

Add a jspx to you project, if you put that under Web pages/jspx/foo.jspx

You will need to use it by http://localhost:8080/projectName/jspx/foo.iface

you are all set.

Enjoy!

Java AS, Container的體驗

早前因為研究portlet用到Jboss AS, Portal.

但他的startup超慢,因為要處理很多東西和支持EJB3吧

但如果自己玩開發,聽VP的Angus說,他們只用Tomcat,20秒不到就reboot完了.

我自己又還沒用到EJB,只是用來玩,那就想試一下Tomcat,以後要用web service再來弄JBoss AS吧.

一用之下發現Tomcat真的爽很多,幾秒就能讓用start起來試你的web app了 .

Develop JSF1.2 with Netbeans 5.5.1 running on Tomcat 6 with Mysql backend

My Environment

JSF 1.2

Netbeans 5.5.1

Tomcat 6

Mysql 5

Prepare Tomcat 6

Download and unpack Tomcat 6.

Run: [tomcat6]\bin\startup.bat in windows

Shutdowm: [tomcat6]\bin\shutdown.bat in windows

Change port: [tomcat6]\apache-tomcat-6.0.16\conf\server.xml

Add user: [tomcat6]\apache-tomcat-6.0.16\conf\tomcat-user.xml

<role rolename="manager"/> <user username="tomcat" password="s3cret" roles="manager"/>

Open server manager in Netbeans-> Add Server, choose Tomcat 5.5(I don’t see Tomcat 6 and don’t know how to add it). Catalina Home means where you unpack Tomcat 6. username/password is the manager username/password we set previously.

You can run and deploy project using Tomcat 6 by now.

Prepare Mysql

Download and run it as a service from Mysq which will be trivial to teach.

Copy JDB-connecter for Mysql under [tomcat6]\lib

Add the following setting in your context

<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"
removeAbandoned="true" emoveAbandonedTimeout="60"/>
</Context>

Context for tomcat 6 can be in the following ways!

  • in the $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
  • in the $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
  • in individual files (with a “.xml” extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. context#path.xml. The default web application may be defined by using a file called ROOT.xml.
  • if the previous file was not found for this application, in an individual file at /META-INF/context.xml inside the application files
  • inside a Host element in the main conf/server.xml

I prefer the /META-INF/context.xml in your project under <web-app>

Put this in your web.xml in your project:

<description>MySQL Test App</description>
 <resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/TestDB</res-ref-name>       
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth>   
</resource-ref>

***注意:如果你在Netbeans5.5裡面run你的project會有問題,因為netbeans5.5自動include mysql connector,會跟[tomcat 6]\lib裡面的相沖

You are now ready to use Myql in your project.

Prepare JSF

JSF1.2 need Servlet 2.5 and JSP 2.1 which tomcat 6 can provide.

Download JSF and JSTL

Create Web project and Add JSF framework in project properties.

This is the same as doing JSF setting in web.xml, netbeans user don’t need to do this by hand.

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Add JSF managed bean by right clicking in project navigator.

Which will automatically add setting in faces-config.xml by Netbeans,

you can do it yourself by adding the following in faces-config.xml:

<managed-bean>
<managed-bean-name>ManagedBean</managed-bean-name>
<managed-bean-class>maxi.fun.ManagedBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Replace library with those we download previously : jsf-api.jar and jsf-impl.jar for JSF ;standard.jar and jstl.jar for JSTL

JSF file can be .jsf or .jsp, I don’t know how to create .jsf. so I just use .jsp instead.

You are all set. Have Fun

NetBeans Hot Key

NetBeans 常用 Hot-Key:
Ctrl + Shift + F
: Reformat Code (與 eclipse 一樣)
Alt + Shift + F : Fix Imports
Alt + Shift + W : Surround With try-catch
Alt + O : Go to Source
Alt + F7 : Find Usages
Ctrl + F : Find
Ctrl + H : Replace
Ctrl + Shift + T : Comment
Ctrl + Shift + D : Uncomment
Ctrl + \ : Code Completion

最近在研究的東西

Tomcat – 因為是跑很快的container
Java Graph = Java2D, JGraph, JFreeChart – 因為最近想搞 Chart的東西
GUI = Swing, AWT, SWT – 純粹玩玩
Embedded DB = HSQLDB, Berkeley DB, Mysql embedded, Perst – 總覺得有天會有用
Packing = Izpack – 包裝軟體一向有興趣,但這個好像很麻煩,最後才玩