2010年10月12日 星期二

Web Service 的幾種發佈和調用的方法

http://hi.baidu.com/lzj83520/blog/category/Java/index/0

第一種:
用JDK的API (annotation)
1) 先建立 Web Service EndPoint
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello{
@WebMethod
public String hello(String name){
return "Hello, "+name+"\n";
}

public static void main(String[] args){
//create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello",hello);
}
}
2) 用 apt 編譯 (compile) Hello.java (ex: apt -d [存放編譯後file的dir] Hello.java )
3) http://localhost:8080/hello?wsdl
4) wsimport 生成客戶端 (client side)
wsimport -p . -keep http://localhost:8080/hello?wsdl
5) 客戶端程式:
class HelloClient{
public static void main (String[] args){
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("嗨, 你好!");
System.out.println(hello);
}
}


第二種:
使用 xfile
(客戶端) Client Side:


第三種:
使用 axis1.4

第四種
使用 axis2
(服務端) Server Side: POJO
(服務端) Server Side: XML 發佈 web service

(客戶端) Client Side:

2010年10月11日 星期一

Web Service Create (Axis1)

http://onjava.com/pub/a/onjava/2002/06/05/axis.html?page=1

Creating Web Services with Apache Axis1

05.22.2002

Web services have been a buzzword for a while. A friend used to say "Web services are like high school sex. Everyone is talking about doing it, but hardly anyone is, and those that are probably aren't doing it well." These days, though, Web services are moving to college, so to speak, and lots of people are starting to "do it" more often and better than before. Tools are maturing, and creating and working with Web services isn't all that hard anymore.

IBM has given a lot of code to the Apache group, including SOAP4J, their group of SOAP tools. The Apache SOAP and SOAP4J guys got together and are working on the latest and greatest tool set called Apache AXIS, which features not only better performance, but also some new features that make it trivial to play in this new world. I see the most common actions being "I want to expose this functionality as a Web service," and "I want to access that Web service." Surely it should be very straight-forward to strap on this interface, and you shouldn't have to learn everything there is to know about the underlying platform. This is the same idea as not having to know about the IP and TCP layer when accessing a URL over HTTP. Let's keep it simple, folks.

In this article, I will show two parts of this new system:
  • First, I will show the "easy to deploy" feature that lets you drop a source file into the AXIS Web application and have it become a Web service -- just like that!
  • Then we will use the new WSDL2Java and Java2WSDL tools to see how we can quickly get a WSDL descriptor and access the associated Web service, and then how to easily expose some Java code.
All of the code that is listed (and downloadable) was written for Apache Axis beta1. There are more instructions on running the code at the end of the article.

Deploying Your Code as a Web Service in One Easy Step

The Apache guys realized that it would be really nice to be able to drop some code somewhere and have it become a Web service "just like that." This simplicity is a current trend; Microsoft has it in .NET, and BEA in the WebLogic 7 platform. But just how easy is it to:

  • Deploy a piece of code?
  • Write a client that accesses the Web service?
  • Obtain the WSDL for the deployed Web service?

Working With a Production Web Service

Although it is really easy and convenient to shove our Java code under the Axis directory as a .jws file, that will not be the way you deploy all of your Web services. A lot of the time we want more fine-grained control over the Web service, to tweak it, and to use other more advanced features. Luckily, with other tools, it is still easy for us to work with our code in a more formal manner.

Let's walk through the following process:

  1. We have a piece of code that calculates the Fibonacci sequence for a given iteration.
  2. We want to take the existing code, wrap it up as a Web service, and then deploy it to the Apache Axis system.
  3. Once we have a running service on the server side, we will create Java stubs that allow us to communicate with the service, only requiring the WSDL.

After going through this full process, you will be able to create clients to any Web services (when given the WSDL), and wrap upany code, exposing it as a Web service.

Here are the steps we will walk through:

  1. View: Take a peek at the existing Fibonacci code.
  2. Java2WSDL: Generate the WSDL file for the given Fibonacci interface.
  3. WSDL2Java: Generate the server side wrapper code, and stubs for easy client access.
  4. FibonacciSoapBindingImpl: Fill in wrapper to call the existing Fibonacci code.
  5. Deploy: Deploy the service to Apache Axis.
  6. Client: Write a client that uses the generated stubs, to easily access the Web service.

2010年10月10日 星期日

Java Exceptions (interview 問到囉!!!)

For run time exceptions

  • method signature does not need to declare runtime exceptions
  • caller to a method that throws a runtime exception is not forced to catch the runtime exception
  • Runtime exceptions extend from RuntimeException or Error
Example:
ArithmeticException, IllegalArgumentException, ClassCastException, NullPointerException, IndexOutOfBoundException,
SystemException, NoSuchMethodException.
For checked exceptions:
  • method must declare each checked exception it throws
  • caller to a method that throws a checked exception must either catch the exception or throw the exception itself
  • Checked exceptions extend from Exception

Checked exceptions indicate an exceptional condition from which a caller can conceivably recover. Runtime exceptions indicate a programmatic error from which a caller cannot normally recover.

Checked exceptions force you to catch the exception and to do something about it. You should always catch a checked exception once you reach a point where your code can make a meaningful attempt at recovery. However, it is best not to catch runtime exceptions. Instead, you should allow runtime exceptions to bubble up to where you can see them.


Java Swing (Lightweight) vs. AWT (Heavyweight)

http://j2se.myweb.hinet.net/article/java/hw02.htm


Lightweight Component 與 Heavyweight Component


我們常會聽到許多人說 Swing 是 lightweight component,而 AWT 是 heavyweight component,指的就是 Swing是由純 Java code 所寫成,因此 Swing 解決了 Java 因 Window 類別而無法跨平台的問題,使視窗功能亦具有跨平台與延展性的特性。而且 Swing 不需佔有太多系統資源,因此我們稱 Swing 為 lightweight component,表示我們可以利用它輕易地做出各種變化。相對於 Swing,由於 AWT 的 native code 具有 C 語言的成分,若您想自行更動 AWT 的視窗變化時,您必須撰寫自己的 C 語言原生碼,然後再搭配 AWT 的原生碼與 JDK 函式庫。遇到不同的作業平台時,又必須重新修改和編譯自己所寫的原生碼。因此 AWT 不具跨平台特性、耗時且難以理解,且又耗費系統資源,所以就稱之為 heavyweight component,表示 AWT 元件是不容易更動的。而在本文第一段所提到,用 Swing 寫成的程式不保證能夠執行於所有瀏覽器上,則是單指 Java applet網頁程式而言,其肇因於微軟因商業競爭考量,其 IE 瀏覽器本身不再繼續支援 Java 程式的緣故。


2010年10月7日 星期四

Java Primitive Data Types (interview 竟然有問!!!)

boolean

1-bit. May take on the values true and false only.

true and false are defined constants of the language and are not the same as True and False, TRUE and FALSE, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.

byte

1 signed byte (two's complement). Covers values from -128 to 127.

short

2 bytes, signed (two's complement), -32,768 to 32,767

int

4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.

long

8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

float

4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.

double
8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
char

2 bytes, unsigned, Unicode, 0 to 65,535

Chars are not the same as bytes, ints, shorts or Strings.


資料型別可以分基本資料型別以及使用者自訂型別

基本資料型別java 內定有8種基本型別。

有整數類 : byte, short , int , long . 資料長度為 8-bits,16-bits,32-bits,64-bits

浮點數類: double , float. 資料長度為32-bits,64bits

文字類: char資料長度為2bytes且為Unicode`.

邏輯類: boolean .

8種基本型別有各自的固定記憶體長度跟資料格式。

2010年10月6日 星期三

Hibernate Annotations and Middlegen....etc.

http://fupeg.blogspot.com/2006/07/hibernate-annotations.html

Today I found myself in a familiar position -- needing to write ORM backed code. This was a really simple case actually. I had one new table I needed to add plus a class+DAO to access it from my code. Time to call on my old friend Hibernate.

Perhaps I am "old-fashioned" but I usually like to write DDL first. I can see the logic behind writing an object first and then working from there (object -> mapping -> DDL) but it's just easier for me to write the DDL first. So I wrote that and created my table. Then I fired up Middlegen to create my Hibernate mapping. At that point I paused.

Middlegen is great for generating a Hibernate mapping (hbm) file. I decided not to use this and instead use Java annotations. Hibernate Annotations provides full EJB3 Annotation support for any Hibernate application. So I downloaded it and quickly scanned through some of its documentation. I saw that I needed Hibernate 3.2, so I downloaded the latest version of that 3.2CR3 as 3.2 is not GA yet.

I was already pretty familiar with the EJB3 Annotations as I've been one of the many observers to the evolution of that spec. I was ready to write my annotations, but I still needed a class to annotate. So I went ahead and used Middlegen for that. That required me to generate the hbm file, but I simply discarded it when I was done.

Now I annotated my simple class. I'm using Eclipse 3.2 and it handles annotations very well. I simply write the simple name for the annotation, like @Entity and then I could press ctrl-1 to add the appropriate import statement, just like I would do for a class. It also checked my syntax, so it immediately told me that I didn't need quotes for my nullable attribute when I wrote @Column(name="foo", nullable="false").

Now I had an annotated POJO. Next I wrote the DAO class that accomplished my use cases. They were pretty simple, just two methods. I was using Spring so this was especially easy using its HibernateDaoSupport class.

Next I wrote a simple unit test. I told Eclipse that I wanted a new JUnit test case, and it asked if I wanted to do a 3.x or 4.x test case. I chose 4.x, since I was in an annotative mood. Since this was my first unit test that involved Hibernate Annotations, I decided to keep it as simple as possible and bypass Spring. Thus I simply created my Hibernate session factory in my setUp() method, using the AnnotationConfiguration class, of course.

I ran my test and it failed. Not big surprise, don't all unit tests fail the first time? What was surprising was the error message. I had a stinkin' class not found exception. The class that was not found was org.hibernate.loader.custom.SQLQueryReturn. I looked in my hibernate3.jar and sure enough, it wasn't there!

Luckily it was easy enough to Google for this class and that quickly lead me to a thread on java.net. Turns out while Hibernate Annotations needs Hibernate 3.2, it only works with Hibernate 3.2CR2 not 3.2CR3. In other words, if you download the latest Hibernate and Hibenate Annotations, thye don't work together. I went back to the Hibernate site to download 3.2CR2. I noticed that the general purpose Hibernate download page indicated 3.2CR2 as the latest, even though the home page indicated Hibernate 3.2CR3 was the latest and released on July 6.

So I downloaded 3.2CR2 and used it and ... no problems. I did read on the forums that there definitely were problems with that release, I was just lucky enough not to run into them.

Maybe I should put the annotations on the shelf, for now? I'm guessing that it will be stable well before the code I'm working on will be stable, but it's hard to say given their roadmap...

2010年9月29日 星期三

Eclipse Web Service Setting



參考以下:
http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html


<

AxisAdminServlet Class not found exception occurs as deploying the web service.

問題簡述:
Eclipse Java EE IDE for Web Developers. (Build id: 20090621-0832)
建立 web service後, 啟動 Tomcat 5.5 顯示錯誤訊息
java.lang.NoClassDefFoundError: org/apache/http/HttpResponseFactory

解決方法:
從 Apache Axis2 官網中下載 axis2-1.5-bin.zip, 從zip檔案的lib目錄中複製httpcore-4.0.jar 至
該專案的 WEB-INF/lib 目錄中