随笔 - 119  文章 - 3173  trackbacks - 0
<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520465
  • 排名 - 93

最新评论

为了满足广大网友的要求,今天抽时间搞了下WebServices 在tomcat中的发布
相关文章:
tomcat启动时自动加载servlet
学习Java6(一) WebServices (1)服务端
学习Java6(一) WebServices (2)客户端

新建一个servlet,偶太,能少打一个字符都是好的,所以servlet写的非常简洁,也适合初学者看得懂。。。。。。。。。。
WebServiceStarter.java

 1 import javax.servlet.ServletException;
 2 import javax.servlet.http.HttpServlet;
 3 import javax.xml.ws.Endpoint;
 4 
 5 public class WebServiceStarter extends HttpServlet {
 6     
 7     private static final long serialVersionUID = 5870534239093709659L;
 8 
 9     public WebServiceStarter() {
10         super();
11     }
12 
13     public void destroy() {
14         super.destroy();
15     }
16 
17     public void init() throws ServletException {
18         System.out.println("准备启动服务");
19         Endpoint.publish("http://localhost:8080/HelloService"new Hello());
20         System.out.println("服务启动完毕");
21     }
22 }
23 

web service类Hello.java也是非常简单
 1 
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 import javax.jws.soap.SOAPBinding;
 6 
 7 @WebService(targetNamespace = "http://jdk.study.hermit.org/client")
 8 @SOAPBinding(style = SOAPBinding.Style.RPC)
 9 public class Hello {
10     @WebMethod
11     public String sayHello(String name) {
12         return "hello:" + name;
13     }
14 }
web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 5     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6     <servlet>
 7         <servlet-name>WebServiceStarter</servlet-name>
 8         <servlet-class>WebServiceStarter</servlet-class>
 9         <load-on-startup>1</load-on-startup>
10     </servlet>
11 </web-app>
12 

ok
就这三个文件。。。。。。。。。啥jar都不要。。。。
发布,启动服务器
2007-1-5 13:28:37 org.apache.catalina.core.AprLifecycleListener init
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: G:\JDK6\bin;F:\tomcat6\bin
2007-1-5 13:28:37 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2007-1-5 13:28:37 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 937 ms
2007-1-5 13:28:38 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2007-1-5 13:28:38 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.7
2007-1-5 13:28:38 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2007-1-5 13:28:38 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2007-1-5 13:28:38 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
准备启动服务
服务启动完毕
2007-1-5 13:28:39 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2007-1-5 13:28:39 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2007-1-5 13:28:39 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=16/62  config=null
2007-1-5 13:28:39 org.apache.catalina.startup.Catalina start
信息: Server startup in 1969 ms


访问:http://localhost:8080/HelloService?wsdl
 1   <?xml version="1.0" encoding="UTF-8" ?> 
 2 <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jdk.study.hermit.org/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://jdk.study.hermit.org/client" name="HelloService">
 3   <types /> 
 4 <message name="sayHello">
 5   <part name="arg0" type="xsd:string" /> 
 6   </message>
 7 <message name="sayHelloResponse">
 8   <part name="return" type="xsd:string" /> 
 9   </message>
10 <portType name="Hello">
11 <operation name="sayHello" parameterOrder="arg0">
12   <input message="tns:sayHello" /> 
13   <output message="tns:sayHelloResponse" /> 
14   </operation>
15   </portType>
16 <binding name="HelloPortBinding" type="tns:Hello">
17   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
18 <operation name="sayHello">
19   <soap:operation soapAction="" /> 
20 <input>
21   <soap:body use="literal" namespace="http://jdk.study.hermit.org/client" /> 
22   </input>
23 <output>
24   <soap:body use="literal" namespace="http://jdk.study.hermit.org/client" /> 
25   </output>
26   </operation>
27   </binding>
28 <service name="HelloService">
29 <port name="HelloPort" binding="tns:HelloPortBinding">
30   <soap:address location="http://localhost:8080/HelloService" /> 
31   </port>
32   </service>
33   </definitions>
看到以上代码就ok!
客户端写法照旧

呵呵,这下大家满意了吧。。。。。。。。。
有冲动想把项目里的xfire撤掉了。。。。。。。。。。。。。。。。。
posted on 2007-01-05 13:38 交口称赞 阅读(12512) 评论(40)  编辑  收藏 所属分类: Java6J2EE & WEB

FeedBack:
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-05 13:41 交口称赞
忘了说环境

eclipse3.2.1+myeclipse5.1
jdk6
tomcat6.0.7

出现问题请先检查下自己的环境。

如果你在其它环境下也能通过谢谢你告诉我  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-05 13:43 交口称赞
刚才测试了
jboss4.0.5也没问题

看来基本上可以通用  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-07 13:57 冷面阎罗
不过我感觉还是不实用,因为现在jdk6在项目中基本上是不能被用到,目前大多数还是用的jdk4.所以这样的 只能是我们玩玩而已.用到实际开发估计还要一段时间  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-07 18:53 交口称赞
呵呵
在新项目里面可以直接用哦
我们项目现在就用到了JDK5的很多新特性

而且很多开源的东西都用的很多新东西

感觉用起来比xfire还爽  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-07 18:54 交口称赞
有空来测试下性能。。。。。。。。。

看看哪个牛

谁有LoadRunner8.1的License啊  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-07 23:36 正需要啊
z终于盼到了  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-08 22:45 东方游

  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-01-11 16:55 jht
nod
我去SUN JAVA网站上也找了几篇文章,挺好的,大家可以自己去找找  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:11 sdf
can you tell me, how can i create a servlet in eclipse and how can i publish this WS? in very detail!!

Thanks a lot!  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:13 sdf
can i develop this WS without Xfire?  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:34 交口称赞
1.服务端程序:我要怎样在eclipse里写"servlet"和"web service"两个java程序?
新建一个web项目
新建servlet
新建java类
内容按照上面的例子  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:37 交口称赞
2.我怎样执行他们,才能把他们发布出去?

一般项目在eclipse都是自动编译的
我是用的myeclipse
可以直接部署到tomcat或者jboss,有个小按钮,点了以后是向导式的,你一步一步做,看你英文这么好,应该搞的定
这个就是IDE的基本使用

启动tomcat就算发布出去了
测试是否发布成功,本文已经写了  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:40 交口称赞
您能否在说说分别用java6开发WS和用axis2开发ws的优缺点

axis2没用过,不敢说,但是印象不好

java6的WS经过我的这几篇文章探索,个人还有很多问题没解决,感觉还是不要用在项目里面的好,毕竟现在很多项目还是在jdk1.4或者1.5

你要是项目里面需要用webservice推荐你使用xfire

  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-16 21:41 交口称赞
can i develop this WS without Xfire?

你直接用java6的webservice可以不用XFIRE  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-04-23 16:26 daisy
不知道在NetBeans里可以吗?直接在一个Web项目里依次建好上面几个文件,然后启动Tomca就可以吗?  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-23 16:58 交口称赞
楼上抱歉
没在NB下面试过

理论上应该可以

还是建议你用eclipse吧
为你以后工作好  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-04-24 16:02 daisy
为什么我用NB写的WS(JDK1.6)放在这里说:syntax error,annotations are only
available if source level is 5.0?
然后我在你要求的环境下写的代码,和你的格式一样.仍然在@WebService @SOAPBinding @WebMethod下面有红色波浪线,并报告上述错误?
直接建一个calss文件,编写WS,可以吗?  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-24 16:05 交口称赞
可以,你是不是装的jdk6?
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-04-24 16:12 daisy
因为我是急着完成需要的功能,所以并没有时间去多看基础的东西.本来对eclipse不熟悉.
所以问的问题可能显得有些白痴,不过请你陈情帮忙看看,谢谢了!

我现在JDK1.5 1.6 都有了!我以为是JDK版本的问题,换成1.5的也不行
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-04-24 16:14 daisy
可以加你的QQ,或者MSN吗?万分感谢!  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-04-24 16:15 daisy
如果你不想公开,我告诉你我的吧!谢谢了!
QQ:315768093  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-24 16:22 交口称赞
你项目中还是别用这个了

用xfire吧

你弄个jdk6
估计你们头是不会为了你的功能改项目JDK的

  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-26 17:32 HEDY
Netbeans中开发Webservice更容易  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-26 19:26 sdf
Thank you for your reply. i cann't write chinese on my computer, sorry:(

so, must "WebServiceStarter.java" be a "servlet"? and must i create a Web-Project?

I have a WS with axis1.x, now i wanne to change its library only with java6. for example, i hve to authenticate the user with "BasicHandler.invoke()", but it only belones to axis, what should i do, if i write this function in java6? which one can be instead it? and so on... the things like this are so many, that makes me headache.

That' my pleasure, if i could talk with you in msn
i'm iceapplexin@hotmail.com

Thank you  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-04-30 21:16 sdf
It doesn't work!!!
By the compiling WebServiceStarter.java:
1 error
WebServiceStarter.java:19: cannot find symbol
symbol : class Hello
location: class WebServiceStarter
Endpoint.publish("http://localhost:8080/HelloService", new Hello());


Tell me Why,please!!!   回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-01 00:34 交口称赞
。。。。
楼上
我会放一个myeclipse项目,到时候你直接导入就可以了。。。。。


或者建个eclipse的web项目。。。。

你要那种?  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-01 03:43 sdf
"eclipse的web项目" is better for me,

but even better, if you do it with "Tomcat Project" and without MyEclipse (I mean only Eclipse alone, without the "deploy"-Button, how would you do the deployment step by step per hand?
Is this so right?: Deployment = copy all *.class files in the dictionary WEB-INF/classes?).

Thanks a lot!  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-01 07:11 交口称赞
不用myeclipse没问题。。
你有WTP吗?
Web Tools Platform
http://download.eclipse.org/eclipse/downloads/

有这个做WEB开发也可以  回复  更多评论
  
# Developing Java6 WS with TomcatProject in Eclipse 2007-05-03 19:03 sdf
Oh, yes. I have done it!!
I will write it here for you as Thanks and for you all.

Step by step
0> first you must have Tomcat-plugin in your eclipse

1>. Server-side-programm

1. File->new->other->java->
create a Tomcat-Project named "TomWs"->finish

2. in WEB-INF\src create

HelloServlet.java: a Servlet, the only duty is to publish the WS-endpoint onto a URL (hier is Http://localhost:8080/services)

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.swing.JOptionPane;
import javax.xml.ws.Endpoint;


public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
Endpoint endpoint = Endpoint.publish( "http://localhost:8080/services",
new sayHello() );
res.setContentType( "text/html" );
PrintWriter out = res.getWriter();
out.println( "<html>" );
out.println( "Hallo, mein erstes Servlet meldet sich." );
out.println( "</html>" );
out.close();
JOptionPane.showMessageDialog( null, "Server beenden" );
endpoint.stop();

}
}
SayHello.java: a normal class, describes the Web-Service with Jax-Ws Annotation.

import javax.jws.soap.SOAPBinding;
import javax.jws.*;

@WebService(name="XinServ",targetNamespace="http:///")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class sayHello
{
@WebMethod
public String hello( String name )
{
return "Hello " + name + "!";
}

@WebMethod(operationName="body-mass-index")
@WebResult(name = "your-bmi")
public double bmi( @WebParam(name="height") double height,
@WebParam(name="weight") double weight )
{
return weight / (height * height) / 100 * 100;
}
}

3. compile the tow classes in Web-Inf\classes with the two commandos:

set classpath=C:\Tomcat\jakarta-tomcat-5.5.9\common\lib\servlet-api.jar
javac -d classes -sourcepath src src\HelloServlet.java

4. create web.xml in WEB-INF\web.xml
<!DOCTYPE web-app PUBLIC
'-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

5. start Tomcat-server in eclipse
Repeat to compile the classes = reload them in Tomcat
6. in URL-Address input:
http://localhost:8080/TomWs/hello, It’s the address of the Servlet, that only tomcat automatic loads. In the browser will show “Hello….”, and there are small dialog "server beenden", don’t close it, then go on with input the
http://localhost:8080/services?wsdl. Wenn you can see this, you’re done!
7. leave the little widow open.
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-03 19:05 sdf

Go on with the
2>. Client-side-programm

1. create a another java-project

2. create a “wsimport.bat” file in the project-folder with

wsimport -keep -p com http://localhost:8080/services?wsdl

3. run the wsimport.bat, there will tow class automatic producted.

4. create a class named “HelloClient.java”
package com;
public class ClientHello {
public static void main( String[] args )
{

XinServ port = new SayHelloService().getXinServPort();
System.out.printf( "%s Your BMI is %.1f%n",
port.hello( "Xin" ),
port.bodyMassIndex( 183, 84 ) );
}
}

5. compile this and run it as java application in eclipse.

6. if you cann't see anny eerors, than congratulations!
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-04 15:52 高手
楼上的好佩服你啊  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-04 17:13 sdf
But This way is uncommon to write a WS, do you think, that develop WS with Java6+Tomcat is better and easier then with axis2? I don't know :(
can anyone give some idears to me about that?  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-30 11:02 agoo
准备启动服务
2007-5-30 10:55:04 org.apache.catalina.core.ApplicationContext log
严重: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: javax/xml/ws/Endpoint
运行以上程序出现的错误,
环境是JRE1.6
tomcat5.5  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-05-30 12:25 交口称赞
JRE1.6
还是JDK?
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布[未登录] 2007-06-13 14:59 edward
报以下错误,误人子弟
java.net.BindException: Address already in use: bind  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-06-14 09:23 交口称赞
Address already in use: bind

你自己找个金山词霸翻译一下吧
  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2007-06-21 09:17 杨一
我有一个疑问:
Web Service是怎样自动部署到Tomcat中的?
您这样做仅仅是在JVM自带的服务器中发布了WS,而Tomcat也仅仅起到了触发JVM自带服务器的作用,我想知道怎样把WSDL部署到Tomcat里面,完全由这个成熟的Web服务器托管,或者您告诉我,现在已经做到这一点了  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2008-11-06 10:37 jeekchen
这种方式只能单线程访问的,没实际用处  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2015-10-09 12:30 你二大爷
Address already in use: bind
晕死;;;  回复  更多评论
  
# re: 学习Java6(一) WebServices (3)在tomcat中发布 2015-11-10 17:58 setyg
靠,,这就叫发布到tomcat了,,忽悠谁呢。、  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: