servlet与jsp篇$.ajax交互

Posted 小木屋欢迎大家~ ~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,查看内容,数据获取成功!

以上是关于servlet与jsp篇$.ajax交互的主要内容,如果未能解决你的问题,请参考以下文章

使用JSP/Servlet技术开发—— 第十一章 Ajax交互扩展

HTML,CSS,JavaScript,AJAX,JSP,Servlet,JDBC,Structs,Spring,Hibernate,Xml等概念

基于Tomcat服务器,通过JQuery的Ajax方法访问Servlet实现前后端数据同步/异步交流(不必跳转或者刷新网页实现JSP前后端交互)

基于Tomcat服务器,通过JQuery的Ajax方法访问Servlet实现前后端数据同步/异步交流(不必跳转或者刷新网页实现JSP前后端交互)

Servlet 与 Ajax 交互一直报status=parsererror

Servlet 与 Ajax 交互一直报status=parsererror