简体   繁体   English

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

[英]Java Servlet/Jsp/CanvasJs Getting Data from Servlet into Jsp

Hi I am new to Webdevelopment and Java, thanks for the help in advance.嗨,我是 Webdevelopment 和 Java 的新手,提前感谢您的帮助。

I am trying to get predefined data from a servlet into a canvasjs graph in a jsp.我正在尝试将预定义数据从 servlet 获取到 jsp 中的 canvasjs 图中。

I use Eclipse Oxygen and TomCat 8.5我使用 Eclipse 氧气和 TomCat 8.5

In short my code looks like:简而言之,我的代码如下所示:

ChartValues.java just has the variable instantiation so I don't think it's worth showing ChartValues.java 仅具有变量实例化,因此我认为不值得展示

jsp File: 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>

When I comment the session row the program works fine but when it is included the graphs won't build.当我评论 session 行时,程序运行良好,但是当包含它时,图表将无法构建。

Servlet File:小服务程序文件:

    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);
        }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

            int test =10;
            HttpSession session=request.getSession(true);
            session.setAttribute("test1", test);

            response.sendRedirect("CharttestCanvas3.jsp");

        }
}

Also tried it with requests in doGet like:还尝试了 doGet 中的请求,例如:

ChartValues[] articles =
                new ChartValues[] {new ChartValues(39500,1.5,0.5,"16/01/09/2019"), new ChartValues(49500,1.5,0.5,"17/01/09/2019")};
        request.setAttribute("articles", articles);

        RequestDispatcher dispatcher = request.getRequestDispatcher("CharttestCanvas3.jsp");
        dispatcher.forward(request, response);

And in Jsp with在 Jsp 中

ArrayList<ChartValues> chartvalue1 =  (ArrayList<ChartValues>)request.getAttribute("chartdata");

But I got the same result.但我得到了同样的结果。

My Error was to think that script and scriptlets are similar and that I can see the finished graph when running the jsp file.我的错误是认为脚本和小脚本相似,并且在运行 jsp 文件时可以看到完成的图形。 When running JSP the value is null.运行 JSP 时,该值为 null。 I needed to use Javascript in script and run the graph from servlet to get it to work.我需要在脚本中使用 Javascript 并从 servlet 运行图形以使其工作。

Changed the servlet transfer from a session to a request.将 servlet 传输从 session 更改为请求。

servlet:小服务程序:

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

        Integer value=15;
        request.setAttribute("value", value);
        request.getRequestDispatcher("CharttestCanvas3.jsp").forward(request, response);
        }

For the JSP I combined script and scriptlets to get it to work.对于 JSP,我结合了脚本和 scriptlet 以使其工作。

JSP: JSP:

    <%Integer value = (Integer)request.getAttribute("value");%>

    <script type="text/javascript">
    window.onload = function() {
    .
    .
    .
    var aa=+'<%=value%>';
    datapoints.push({x: new Date(2015, 03, 10), y: aa});
    chart.render();
    .
    .
    .
    </script>

In the scriptlet is the value from the reques.在 scriptlet 中是来自请求的值。 Via scriptlet=value the value gets into my script, the plus determines the var as an int.通过 scriptlet=value 值进入我的脚本,加号将 var 确定为 int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM