使用ajax调用与JS的Java Servlet连接中的内部服务器错误500

时间:2019-01-25 11:52:04

标签: java ajax servlets http-status-code-500

我在JS中有以下代码,可以在服务器(而不是我的本地计算机)上调用Servlet。 该项目在Apache Tomcat APP服务器上运行 我收到错误代码:

  

net :: ERR_ABORTED 500(内部服务器错误)

var URL_SERVER = window.location.protocol
    + "//xx.xxx.xxx.xxx:xxx/TrackingTool/";

$(function() {

$("#testBtn").click(function() {

    $.ajax({
        url : URL_SERVER + "ServletTrackingTool",
        cache : false,
        type : "GET",
         contentType: "application/x-www-form-urlencoded",
        dataType: 'jsonp',
        crossDomain: true,

        success : function(ergebnis) {
            $(".result").empty();
            $.each(ergebnis, function(key, val) {

                console.log(val);

                var detail = "<h2>" + val.name + "</h2>";

                $(".result").append(detail);
            });

        }
    });

});

});

如果我从本地计算机使用url:/ ServletTrackingTool调用servlet,一切正常。

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String auswahl = request.getParameter("auswahl");
    String second = request.getParameter("second");

    System.out.println(auswahl);
    System.out.println(second);

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Credentials", "true");
    response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
    response.addHeader("Access-Control-Allow-Headers","origin, content-type, accept, authorization");
    response.addHeader("Access-Control-Max-Age", "1728000");

    PrintWriter zurueck = response.getWriter();

    // Enthält die Objekte
    List<Tracking> target = new ArrayList<Tracking>();

    Gson gson = new Gson();

    try {
        con = Datenbankverbindung();
        if (con != null) {
            System.out.println("Connected");
            cs = con.prepareCall("{call sp_testtabelle(?)}");
            cs.setString("Auswahl", "1");

            resultSet = cs.executeQuery();

            while (resultSet.next()) {
                Tracking neu = new Tracking();

                neu.setName(resultSet.getString("name"));

                target.add(neu);
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            resultSet.close();
            cs.clearBatch();
            cs.close();
            con.close();
            System.out.println("Connection close");
        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

    String jsonFormatBack = gson.toJson(target);

    zurueck.print(jsonFormatBack);
}

最后,Tomcat日志文件为:

GET /TrackingTool/trackingServlet/ServletTrackingTool HTTP/1.1" 404 1047
GET /TrackingTool/WEB-INF/classes/trackingServlet/ServletTrackingTool HTTP/1.1" 404 949
GET /TrackingTool/ServletTrackingTool HTTP/1.1" 500 1445
GET /TrackingTool/ServletTrackingTool?_=1548417258813 HTTP/1.1" 500 1445
GET /TrackingTool/ServletTrackingTool?_=1548417265044 HTTP/1.1" 500 1445
GET /TrackingTool/ServletTrackingTool?_=1548417598474 HTTP/1.1" 500 1445

0 个答案:

没有答案