zoukankan      html  css  js  c++  java
  • servlet与jsp篇(一)$.ajax交互

    servlet其实是利用java类编写的服务器端应用程序,他的生命周期可以分为三个阶段:初始化阶段、运行阶段和消亡阶段;

    jsp页面实质上是一个HTML页面,但他包含了用户产生动态网页内容的java代码。

    他们之间的联系千丝万缕,今天就跟大家分享一下最近的学习心得:通过JQuery的$.ajax()实现两者的交互。

    1.servlet代码:servlet1.java

    package servlet1;
    
    import iface.RemoteServer;
    import iface.Thrift_TargetInfo;
    import net.sf.json.JSONArray;
    import org.apache.thrift.TException;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TProtocol;
    import org.apache.thrift.transport.TSocket;
    import org.apache.thrift.transport.TTransport;
    import org.apache.thrift.transport.TTransportException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    
    @WebServlet(name = "Servlet1", urlPatterns = "/getTgs")
    public class Servlet1 extends HttpServlet {
        private static final int SERVER_PORT = 16003;
        private static final String SERVER_IP = "192.168.31.235";
        public static RemoteServer.Client Thrift_Client;
        public static TTransport Thrift_transport;
    
        public void startClient() throws TTransportException {
            int TIMEOUT = 30000;
            Thrift_transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
            TProtocol protocol = new TBinaryProtocol(Thrift_transport);
            Thrift_Client = new RemoteServer.Client(protocol);
            Thrift_transport.open();
            System.out.println("Thrift16003端口已开启,客户端已创建...");
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            JSONArray jsonArray = new JSONArray();
            try {
                startClient();
                ArrayList<Thrift_TargetInfo> ships = (ArrayList<Thrift_TargetInfo>) Thrift_Client.Thrift_GetTarget();
                jsonArray = JSONArray.fromObject(ships);
                System.out.println(jsonArray);
            } catch (TException e) {
                e.printStackTrace();
            }
    
            //向前台的页面输出数据jsonArray
            PrintWriter out = response.getWriter();
            out.println(jsonArray);
            out.close();
        }
    }

    2.jsp页面中只需插入以下的脚本方法即可

    var radarTgs;
    $(function () {
        $.ajax({
            dataType: "json",    //数据类型为json格式
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            type: "GET",
            url: "/getTgs",
            statusCode: {
                404: function () {
                    alert('page not found');
                }
            },
            success: function (data) {
                radarTgs = data;
            }
        });
    
    });

    3.输出radarTgs,查看内容,数据获取成功!

  • 相关阅读:
    Vi/Vim查找替换使用方法【转】
    ios开发--NSDate与NSDateFormatter的相关用法【转】
    IOS开发基础之—MD5加密算法【转】
    struts2多图片上传实例【转】
    IOS开发之----常用的基本GDB命令【转】
    WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks
    Xcode8 NSLog打印json不完整的解决方案
    Swift 3.1 的一些新特性
    iOS中书写代码规范35条小建议
    女程序员做了个梦,众网友的神回复
  • 原文地址:https://www.cnblogs.com/zfygiser/p/6780075.html
Copyright © 2011-2022 走看看