简体   繁体   中英

Java Servlet/Jsp/CanvasJs Getting Data from Servlet into Jsp

Hi I am new to Webdevelopment and Java, thanks for the help in advance.

I am trying to get predefined data from a servlet into a canvasjs graph in a jsp.

I use Eclipse Oxygen and 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

jsp File:

<%@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.

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:

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

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. When running JSP the value is null. I needed to use Javascript in script and run the graph from servlet to get it to work.

Changed the servlet transfer from a session to a request.

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:

    <%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. Via scriptlet=value the value gets into my script, the plus determines the var as an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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