如何将JSONObject发送到JSP以及如何接收它?

时间:2015-11-28 17:36:49

标签: java json jsp servlets

我是这些JSP和Servlet的新手。 我现在有以下内容:

web.xml

    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

welcome.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome <%=request.getAttribute("resultset")%></title>
</head>
<body>
    <h3>Login successful!!!</h3>
    <h4>
        Hello,
        <%=request.getAttribute("resultset")%>
    </h4>
</body>
</html>

那是我的LoginServlet.java

public class LoginServlet extends HttpServlet{  

    private static final long serialVersionUID = 1L;  

    public void doPost(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {    

        response.setContentType("text/html");    
        PrintWriter out = response.getWriter();      

        HttpSession session = request.getSession(false);  
        if(session!=null)  
        session.setAttribute("name", username);  

        RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        request.setAttribute("resultset", "Hallo");
        rd.forward(request,response);

        out.close();    
    }    
}  

所以现在我想要的是以下内容。我还想将以下JSONObject(因为使用Highchart-JS-API)从LoginServlet转移到welcome.jsp

JSONObject json1 = new JSONObject(data1);
JSONObject json2 = new JSONObject(data2);
JSONArray array = new JSONArray();
array.put(json1);
array.put(json2);
JSONObject finalObject = new JSONObject();
finalObject.put("DeltaRealTime", array);

我的问题是,我应该如何将其发送到welcome.jsp?而且,我应该如何在JSP端接收它?像这样:

<script type="text/javascript">
$(function (???????) {

    var seriesOptions = [];
    $.getJSON(??????????, function(data) {

        // Populate series
        for (i = 0; i < data.DeltaRealTime.length; i++){
            seriesOptions[i] = {
                name: data.DeltaRealTime[i].name,
                data: [data.DeltaRealTime[i].key, parseFloat(data.DeltaRealTime[i].value)]
            };
        }

        $('#container').highcharts({
            chart: {
                type: 'spline',
                [...]
        });
    });
});
</script>

0 个答案:

没有答案