首页 > 代码库 > webService案例
webService案例
1.基本原理
表现层 《----------》业务层 (自己的)
1.调用别人的(公网)《----------》 公网手机号查询客户端《----------》公网手机号查询服务端
2.发布自己的
2.开发步骤
2.1创建web项目
2.2生成公网客户端代码
查询服务地址:http://www.webxml.com.cn/zh_cn/index.aspx
生成客户端代码:
wsimport -p cn.ceshi.mobile -s . http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
2.3创建SEI接口
@WebService public interface MobileInterface { public String queryMobile(String phoneNum); }
2.4创建SEI实现类
//根据WSDL文件,编写代码 private MobileCodeWSSoap mobileClient; public String queryMobile(String phoneNum) { return mobileClient.getMobileCodeInfo(phoneNum, ""); } public MobileCodeWSSoap getMobileClient() { return mobileClient; } public void setMobileClient(MobileCodeWSSoap mobileClient) { this.mobileClient = mobileClient; }
2.5创建页面
<form action="queryMobile.action" method="post">
手机号码归属地查询:<input type="text" name="phoneNum" />
<input type="submit" value="http://www.mamicode.com/查询"/>
</form>
2.6创建servlet
private MobileInterface mobileService;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { String phoneNum = request.getParameter("phoneNum"); if(null!=phoneNum && !"".equals(phoneNum)) { //获取spring的上下文 (如果学习springMVC就不用这样,直接通过注解) WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); mobileService =(MobileInterface)context.getBean("mobileService"); String result = mobileService.queryMobile(phoneNum); request.setAttribute("result", result); } request.getRequestDispatcher("/WEB-INF/jsp/queryMobile.jsp").forward(request, response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }
2.7配置spring的配置文件 applicationContext.xml
<!-- 通过jaxws发布服务 --> <jaxws:server address="/mobile" serviceClass="cn.itcast.mobile.server.MobileInterface"> <jaxws:serviceBean> <ref bean="mobileService"/> </jaxws:serviceBean> </jaxws:server> <!-- 配置服务实现类 --> <bean name="mobileService" class="cn.itcast.mobile.server.MobileInterfaceImpl"> <property name="mobileClient" ref="mobileClient"></property> </bean> <!--配置公网客户端 --> <jaxws:client id="mobileClient" address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
serviceClass="cn.itcast.mobile.MobileCodeWSSoap"> </jaxws:client>
2.8配置web.xml
<!-- 设置spring的环境 --> <context-param> <!--contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <!--配置mobileServlet --> <servlet> <servlet-name>mobileServlet</servlet-name> <servlet-class>cn.itcast.mobile.server.servlet.MobileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mobileServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
webService案例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。