(十)web服务与javaweb结合(1)
一、解决方法
A . 编写一个监听器,在监听器中发布服务
二、案例一
- 方法A:编写一个监听器,在监听器中发布服务
1. 编写服务接口
package com.shyroke.service; import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService
public interface IFirst { @WebResult(name = "addResult")
public int add(@WebParam(name = "x") int x, @WebParam(name = "y") int y);
}
2. 实现服务接口
package com.shyroke.service; import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService(endpointInterface = "com.shyroke.service.IFirst")
public class IFirstImpl implements IFirst { public int add(int x, int y) {
return x + y;
} }
3. 在监听器中发布
package com.shyroke.listener; import javax.crypto.EncryptedPrivateKeyInfo;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.ws.Endpoint; import com.shyroke.service.IFirstImpl; public class MyListener implements ServletContextListener { public void contextInitialized(ServletContextEvent arg0) {
System.out.println("项目初始化。。"); /**
* 发布服务
*/ Endpoint.publish("http://localhost:3030/web_webService", new IFirstImpl()); System.out.println("发布服务。。。。。。");
} public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("项目销毁。。"); } }
4. 编写servlet
package com.shyroke.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
out.print("这是一个servlet"); out.flush();
out.close(); } }
5. web.xml配置监听器和servlet
<!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> <listener>
<listener-class>com.shyroke.listener.MyListener</listener-class>
</listener> <servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.shyroke.servlet.TestServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
6. 结果



缺陷
- 通过监听器发布的web服务,web服务的端口和web工程的端口是不能一致的,否则就会报错。
(十)web服务与javaweb结合(1)的更多相关文章
- (十一)web服务与javaweb结合(2)
一.解决问题及解决方法 解决问题:上章节用监听器的方式是有缺陷的:web服务的端口和web工程的端口不能一致. 解决方案:将webService绑定到web工程中,使得共用一个端口. 二.案例 2.1 ...
- (十二) web服务与javaweb结合(3)
一.需求 上一章节虽然将webservice和web项目绑定在了一起,但是还是不能共同一个端口,本章讲解webservice和web项目绑定且共同端口. 二.案例 2.1 创建web工程,并引入依赖 ...
- JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码
JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码 准备条件:一个创建好的 JavaWeb 项目 步骤: 1.创建一个类并实现 Filter 接口 import javax.ser ...
- SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务
一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- koa 基础(十二)koa-static 静态资源中间件 静态web服务
1.目录 2.app.js /** * koa-static 静态资源中间件 静态web服务 * 1.npm install --save koa-static * 2.const static = ...
- WEB服务-Nginx之十-keepalived
WEB服务-Nginx之10-keepalived 目录 WEB服务-Nginx之10-keepalived Keepalived和高可用 基本概述 Keepalived安装配置 Keepalived ...
- 上传文件服务与web服务分离
业务场景:1. 后端服务为java web应用,使用tomcat容器,多实例集群化部署.2. 前端使用nginx作为后端应用的反向代理. 业务需求:现在需要在java web应用端上传文件,同时还要能 ...
- Rest风格WEB服务(Rest Style Web Service)的真相
http://blog.csdn.net/jia20003/article/details/8365585 Rest风格WEB服务(Rest Style Web Service)的真相 分类: J2E ...
- Spring boot构建基于rest的Web服务
一.介绍:使用Spring Boot我们可以很容易的创建一个可独立运行的Rest web服务,其中内嵌tomact,我们只需“run”就可以查看效果了. Spring Boot利用Gradle或Mav ...
随机推荐
- js回车键事件
js回车键事件 一.总结 一句话总结: $("#focus").keypress(function(event){if(event.which === 13) { /*点击回车要执 ...
- Celery如何修复Python的GIL问题
小结: 1. Celery如何修复Python的GIL问题https://python.freelycode.com/contribution/detail/346 最近,我重读了Glyph写的Uny ...
- SpringBoot持久层技术
一.Springboot整合mybatis maven中添加对数据库与mybatis的依赖 <dependencies> <dependency> <groupId> ...
- Redis操作类型
- Ionic4.x 中的列表UI组件
1.普通列表 <ion-list> <ion-item> <ion-label>Peperoni</ion-label> </ion-item&g ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_06-SpringSecurityOauth2研究-Oauth2授权码模式-申请令牌
3.3 Oauth2授权码模式 3.3.1 Oauth2授权模式 Oauth2有以下授权模式: 授权码模式(Authorization Code) 隐式授权模式(Implicit) 密码模式(Reso ...
- Promise和Generator
异同: 1.promise解决的是串行的嵌套异步问题. 2.yield把Generator Function切割为有多个出口的Generation. 3.Promise是社区的研发产物,yield是E ...
- Angular实现简单数据计算与删除
AngularJS 1)什么是AngularJS AngularJS 简介 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. ...
- ABAP ole操作
1.ole 如何保存和退出call method of sheetname 'saves' exporting #1 = filepath #2 = 1. call method of applica ...
- darknet标签转化为COCO标签
import sys import json import cv2 import os import shutil dataset = { "info": { "desc ...