JSON响应打印HTML而不是消息

时间:2016-02-18 10:51:51

标签: javascript json servlets

我正在尝试在控制台中打印JSON响应。但令我惊讶的是,在控制台中打印的值是整个HTML页面而不是JSON响应字符串。

以下是我的代码

<%@page import="org.bean.UserBean"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="myCssFile.css">
<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.10.0.min.js"></script>

<title>Insert title here</title>
<script type="text/javascript">
    function enableSubmit() {
        document.getElementById("getCase").disabled = true;
        document.getElementById("submitButton").disabled = false;
    }
    function enableGetCase() {
        document.getElementById("getCase").disabled = false;
        document.getElementById("submitButton").disabled = true;
    }
</script>
</head>
<body>
    <div class="header" id="header">
        <form id="form1">
            <input type="hidden" id="name" name="name1" value="index">
        </form>
        <span class="totalTime">Count is ${DAOCount}/${excelCount}</span> <span
            class="efficiency">${effeciency}</span>
    </div>
    <input type="hidden" id="counter" value="${countValue}">
    <div class="bodytag">
        <form>
            <input type="Submit" value="Get Case" name="getCase" id="getCase"
                onclick="enableSubmit()" />

            <table>

                <tr>
                    <td>Case Number</td>
                    <td><input id="Text1" type="text" value="" /></td>

                    <td>Case Owner</td>
                    <td><input id="Text6" type="text" value="" /></td>
                </tr>
                <tr>
                    <td>Source</td>
                    <td><input id="Text2" type="text" /></td>
                    <td>Status</td>
                    <td><input id="Text7" /></td>
                </tr>
                <tr>
                    <td class="auto-style2">Issue</td>
                    <td class="auto-style2"><input id="Text3" value="" type="text" /></td>
                    <td class="auto-style2">Reason</td>
                    <td class="auto-style2"><input id="Text8" value="" type="text" /></td>
                </tr>
                <tr>
                    <td>Date/Time Opened</td>
                    <td><input id="Text4" type="text" value="" /></td>
                    <td>Age(Days)</td>
                    <td><input id="Text10" type="text" value="" /></td>
                </tr>
                <tr>
                    <td>Resolution</td>
                    <td><select id="Select1" name="D1">
                            <option></option>
                    </select></td>
                    <td>Final Status</td>
                    <td><select id="Select2" name="D2">
                            <option value="     "></option>
                    </select></td>
                </tr>
                <tr>
                    <td>Start Time</td>
                    <td><input id="Text5" type="text" value="Start Time" /></td>
                    <td>End Time</td>
                    <td><input id="Text9" type="text" value="end Time" /></td>
                </tr>
            </table>
            <input type="button" value="Submit" name="submitButton"
                id="submitButton" disabled="disabled" onClick="enableGetCase()" />
        </form>
    </div>
    <script type="text/javascript" src="indexJS.js"></script>
</body>
</html>

indexJS.js

$(document).ready(function() {
    $("#form1").on('submit', function(e) {
        $.ajax({
            type : "get",
            url : "GetTheCounts",
            data : $(this).serialize(),
            success : function(msg) {
                console.log(JSON.stringify(msg));
            }
        });
        e.preventDefault();
    }).submit();
});

我的servlet如下

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

        GetTheCountsDAO getTheCountsDAO = new GetTheCountsDAO();
        try {
            response.setContentType("application/json");
            int excelCount = getTheCountsDAO.getTotalFromExcel();
            int DAOCount = getTheCountsDAO.getTotalFromDB();
            double getEffeciency = getTheCountsDAO.getEffeciency();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("DAOCount", DAOCount);
            jsonObject.put("excelCount", excelCount);
            jsonObject.put("effeciency", getEffeciency);
            jsonObject.put("countValue", 1);
            String json = new Gson().toJson(jsonObject);
            response.setCharacterEncoding("UTF-8");
            System.out.println("Returned String is " + json);
            response.getWriter().write(json);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

最初我认为问题可能出在Servlet响应上,当我做sysout时,收到的值是预期的。以下是结果。

返回字符串为{"DAOCount":43,"excelCount":882,"effeciency":81.94,"countValue":1}

请让我知道如何在我的JS中获得相同的内容。

由于

1 个答案:

答案 0 :(得分:4)

request.getRequestDispatcher("index.jsp").forward(request, response);

将JSON放入响应后,执行前向index.jsp转发,将Index HTML文档放入响应中。