Java Servlet/Jsp/CanvasJs 从 Servlet 获取数据到 Jsp

您好,我是 Web 开发和 Java 的新手,提前感谢您的帮助。


我正在尝试将 servlet 中的预定义数据获取到 jsp 中的 canvasjs 图中。


我使用 Eclipse Oxygen 和 TomCat 8.5


简而言之,我的代码如下所示:


ChartValues.java 只有变量实例化,所以我认为不值得展示


jsp 文件:


<%@page import="saagnik.ChartValues"%>

<%@page import="java.util.ArrayList"%>

<%@page session="true" %>


    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!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=UTF-8">

    <script type="text/javascript"src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>


        <script type="text/javascript">


        window.onload = function() {


        var datapoints=[];


        var chart = new CanvasJS.Chart("chartContainer1",{

           title:{

                text: "Anzahl der Requests"

            },

            data: [{

                    type: "splineArea",

                    name: "Anzahl",

                    dataPoints: datapoints

                }]

        });


        String test=(String)session.getAttribute("test1");


        datapoints.push({x: new Date(2015, 03, 10), y: 10});


        chart.render();

    </script>

    </header>

    <body>

         <div id="chartContainer1"></div>

    </body>

    </html>

当我评论会话行时,程序工作正常,但是当包含它时,图表将无法构建。


Servlet 文件:


    package saagnik; 

    import java.io.IOException;

    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 javax.servlet.http.HttpSession;


    @WebServlet("/ChartServlet")

    public class ChartServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

                doPost(request,response);

        }


一只甜甜圈
浏览 90回答 1
1回答

牛魔王的故事

我的错误是认为脚本和 scriptlet 是相似的,并且我可以在运行 jsp 文件时看到完成的图表。运行 JSP 时该值为 null。我需要在脚本中使用 Javascript 并从 servlet 运行图形才能使其正常工作。将 servlet 传输从会话更改为请求。小服务程序:&nbsp; &nbsp; protected void doGet(HttpServletRequest request, HttpServletResponse response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws ServletException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; Integer value=15;&nbsp; &nbsp; &nbsp; &nbsp; request.setAttribute("value", value);&nbsp; &nbsp; &nbsp; &nbsp; request.getRequestDispatcher("CharttestCanvas3.jsp").forward(request, response);&nbsp; &nbsp; &nbsp; &nbsp; }对于 JSP,我结合了脚本和 scriptlet 来使其工作。联合应用程序:&nbsp; &nbsp; <%Integer value = (Integer)request.getAttribute("value");%>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; window.onload = function() {&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; var aa=+'<%=value%>';&nbsp; &nbsp; datapoints.push({x: new Date(2015, 03, 10), y: aa});&nbsp; &nbsp; chart.render();&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; </script>脚本中是来自请求的值。通过 scriptlet=value 值进入我的脚本,加号将 var 确定为 int。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java