jsp实现的webservice_jsp实现的webservice的简单实例

时间:2020-10-15  来源: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等内容。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="predictions.Prediction" %>
   
    <%
        String verb = request.getMethod();
//        判断客户端http请求的类型
        if (!verb.equalsIgnoreCase("GET")){
            response.sendError(response.SC_METHOD_NOT_ALLOWED,"只允许GET请求");
        }else {
//            设置prediction的ervletContext
            preds.setServletContext(application);
//            输出xml到网页
            out.print(preds.getPredictions());
        }
 %>

测试:使用curl访问:

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

jsp实现的webservice_jsp实现的webservice的简单实例

http://m.bbyears.com/asp/104745.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新