jsp实现的webservice的简单实例

您所在的位置:网站首页 webservice接口开发简单例子 jsp实现的webservice的简单实例

jsp实现的webservice的简单实例

#jsp实现的webservice的简单实例| 来源: 网络整理| 查看: 265

jsp webservice用到的比较少但是用到就是一个大东西了经常用过其它的api形式了而webservice用到不多,下面我们来看一篇关于jsp实现的webservice的简单实例吧,具体如下。

Prediction类(javabean)

package predictions; import java.io.Serializable; /**  * Created by AlexY on 2016/6/28.  */ public class Prediction implements Serializable {     private static final long serialVersionUID = 7570089771880396982L;     private String who;  // 人     private String what; //他的prediction     public Prediction() {     }     public String getWhat() {         return what;     }     public void setWhat(String what) {         this.what = what;     }     public String getWho() {         return who;     }     public void setWho(String who) {         this.who = who;     } }

Predictions类(javabean)

从predictions.db文件中读取文本内容到predictions数组,然后将数组序列化为xml

package predictions; import javax.servlet.ServletContext; import java.beans.XMLEncoder; import java.io.*; /**  * Created by AlexY on 2016/6/28.  */ public class Predictions {     private int n = 32;     private Prediction[] predictions;     private ServletContext servletContext;     public Predictions() {     }     //    Servlet被用来从war文件中的text文件中读取数据     public ServletContext getServletContext() {         return servletContext;     }     public void setServletContext(ServletContext sctx) {         this.servletContext = sctx;     }     //空实现     public void setPredictions(Prediction[] ps){} //    getPredictions返回一个表示Predictions 数组的xml文件     public String getPredictions(){ //        检测是否设置了ServletContext         if ( getServletContext() == null){             return null;         } //        检测是否已经读取了数据         if ( predictions ==null){             populate();         } //        将Predictions数组转换为xml文档         return toXML();     } //      将Predictions数组转换为xml文档     private String toXML() {         String xml = null;         ByteArrayOutputStream out = new ByteArrayOutputStream();         XMLEncoder encoder = new XMLEncoder(out);         encoder.writeObject(predictions);  //序列化为xml         encoder.close();         xml = out.toString();         return xml;     } //    将文本中的内容读取到predictions数组     private void populate() {         String filename = "/WEB-INF/data/predictions.db";         InputStream in = servletContext.getResourceAsStream(filename); //        将文本文件中的内容读取到predictions数组         if ( null != in){ //            因为文件中每条记录就是一行,而每一行who和what字段是通过"!"分隔的             InputStreamReader isr = new InputStreamReader(in);             BufferedReader reader = new BufferedReader(isr);             String line = null;             int i = 0;             try {                 predictions = new Prediction[n];                 while ( null != ( line = reader.readLine())){                     String[] parts =  line.split("!");                     Prediction p = new Prediction();                     p.setWho(parts[0]);                     p.setWhat(parts[1]);                     predictions[i ] = p;                 }             } catch (IOException e) {                 e.printStackTrace();             }         }     } }

predictions.db:

被读取的文本文件,一条记录一行,每行的内容都有"!"分隔为两部分。

Cornelius Tillman!Managed holistic contingency will grow killer action-items. Conner Kulas!Vision-oriented zero administration time-frame will generate back-end interfaces. Loraine Ryan!Triple-buffered scalable function will productize visionary infomediaries. Patricia Zulauf!Reactive radical knowledge base will aggregate extensible vortals. River Wiza!Face to face client-server pricing structure will whiteboard robust communities. Jarred Wehner!Future-proofed 5th generation protocol will strategize web-enabled networks. Emily Roob!Cross-group fresh-thinking encoding will reinvent dot-com systems. Elvis Ernser!Customizable assymetric database will visualize virtual action-items. Kathryn Hilpert!User-centric non-volatile open architecture will iterate world-class vortals. Tanner Dietrich!Enhanced zero tolerance system engine will evolve turn-key deliverables. Linnie Funk!Distributed dynamic moratorium will iterate magnetic e-commerce. Emery Ward!Synergistic demand-driven functionalities will visualize compelling vortals. Craig Leuschke!Robust intermediate extranet will facilitate best-of-breed synergies. Shayna Lehner!Digitized optimal conglomeration will exploit proactive relationships. Hollis McCullough!Universal fault-tolerant architecture will synthesize bleeding-edge channels. Mina Hayes!Cloned assymetric intranet will enable innovative functionalities. River Friesen!Decentralized 24/7 hub will target robust web-readiness. Carmel Becker!Synergistic disintermediate policy will expedite back-end experiences. Bartholome Walsh!Triple-buffered didactic emulation will visualize integrated channels. Russel Robel!Configurable intangible alliance will scale sexy ROI. Charlene Mertz!Triple-buffered neutral collaboration will incubate B2B deliverables. Letitia Pacocha!User-centric multi-state success will transform proactive convergence. Lottie Marks!Open-source multi-tasking time-frame will monetize rich partnerships. Kaden Crona!Optional static definition will unleash dynamic e-tailers. Everardo Lind!De-engineered systematic emulation will deploy out-of-the-box partnerships. Lilyan Thompson!Synergistic 24/7 website will transition 24/7 methodologies. Alessia O'Connell!Reactive value-added middleware will engineer next-generation partnerships. Reymundo Champlin!Self-enabling reciprocal synergy will generate seamless portals. Immanuel Bergstrom!Assimilated intermediate superstructure will drive vertical methodologies. Dahlia Robel!Proactive demand-driven open architecture will innovate impactful networks. Deven Blanda!Balanced clear-thinking utilisation will expedite collaborative initiatives. Hiram Gulgowski!Versatile tangible application will maximize rich e-business. predictions.jsp:

显示生成xml文档,这里去掉了html的body等内容。

       

测试:使用curl访问:

$ curl --request GET http://localhost:8080/predictions.jsp

您可能感兴趣的文章: c# WebService实例分享 JSP常见问题 用vs.net创建webservice flash简易计算器-Web_Service开发-.NET教 JSP 简介 php怎么调用自建证书的webservice JavaScript调用WebService的实现方法 Jquery调用Webservice传递Json数组 php调用WebService传递时间参数的方法 JSP 自动刷新 JSP 页面重定向



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3