码迷,mamicode.com
首页 > Web开发 > 详细

servlet与jsp篇(一)$.ajax交互

时间:2017-04-28 10:33:45      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:encode   att   exce   开启   extends   cli   arraylist   end   protocol   

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,查看内容,数据获取成功!

servlet与jsp篇(一)$.ajax交互

标签:encode   att   exce   开启   extends   cli   arraylist   end   protocol   

原文地址:http://www.cnblogs.com/zfygiser/p/6780075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!