通过doPost方法将JFreeChart发送到jsp

时间:2015-06-02 09:32:26

标签: java jsp servlets jfreechart

我想使用jsp方法将我的图表发送到doPost。用户可以从X.jsp中选择一个月,然后在index.jsp上查看所选月份的图表。

的servlet

public class RatingServletGraph1 extends HttpServlet {


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

    Connection connection = null;

    try {
        connection=ConnectionManager.getConnection();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    try
    {   
         Common common = new Common();

         String fMonthCtrl = common.MonthControl("fmonth", null);
         String LectID = (String)request.getSession().getAttribute("currentSessionUserID");
         String LectPw = (String)request.getSession().getAttribute("currentSessionUserPsasword");
         String Username = (String)request.getSession().getAttribute("currentSessionUser");
         String UserDept = (String)request.getSession().getAttribute("currentSessionUserDepartment");
         int UserTtlCourse = (Integer)request.getSession().getAttribute("currentSessionUserClasses");
         String CourseID = (String)request.getSession().getAttribute("currentSessionCourse");
         String fmonth = request.getParameter("fmonth");

         RatingDAO ratings = new RatingDAO();
         Vector<String> monthRating= new Vector<String>();
         Vector<String> sdate= new Vector<String>();
         JDBCCategoryDataset dataset = new JDBCCategoryDataset(connection);

         UserBean user = new UserBean();
         RatingBean rating = new RatingBean();

         user.setID(LectID);
         user.setPassword(LectPw);
         rating.setCourseid(CourseID);
         rating.setFmonth(fmonth);

         rating = RatingDAO.courseID(rating);

         dataset.executeQuery("Select c_date, AVG(rating) From rating where course_code='"+CourseID+"' and MONTH(c_date)='"+fmonth+"' group by c_date");
         JFreeChart chart = ChartFactory.createBarChart("Rating of every class for the month selected : ", "Date", "Rate", dataset);
         chart.setBackgroundPaint(Color.gray);
         chart.setBorderPaint(Color.black);
         chart.setBorderStroke(new BasicStroke(3.0f));
         chart.setBorderVisible(true);

         if (dataset != null)
         {
              HttpSession session = request.getSession(true);       

              int width = 400;
              int height = 350;
              final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
              response.setContentType("image/png");

              session.setAttribute("currentSessionUser", Username); 
              session.setAttribute("currentSessionUserDepartment", UserDept);
              session.setAttribute("currentSessionUserClasses", UserTtlCourse);
              session.setAttribute("currentSessionCourse", CourseID);
              session.setAttribute("fMonthCtrl", fMonthCtrl);
              session.setAttribute("currentfmonth", fmonth);
              session.setAttribute("currentSessionSelectDate", sdate);
              session.setAttribute("currentSessionMonthRating", monthRating);
              //response.sendRedirect("index.jsp");

              OutputStream out=response.getOutputStream();
              ChartUtilities.writeChartAsPNG(out, chart, width, height,info);


            //Servlet JSP communication
            RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/menu.jsp");
            reqDispatcher.forward(request,response);
         }

    catch (Throwable theException)      
    {
         System.out.println(theException); 
    }
}

}

的index.jsp

<div id="content">
                        <br>
                        <center>
                            <img src="RatingServletGraph1" width="500" height="350" border="0"/>
                        </center>
                        <table ALIGN="RIGHT" WIDTH="15%" BORDER="0" CELLSPACING="0" CELLPADDING="0" BGCOLOR="#f2f2f2">
                            <td>
                                <a href="graph.jsp"><font size="4" color="0000FF">-Back-</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            </td>
                        </table>
                </div>

的web.xml

<servlet>
<servlet-name>RatingServletGraph1</servlet-name>
<servlet-class>ProjectPackage.RatingServletGraph1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>RatingServletGraph1</servlet-name>
<url-pattern>/RatingServletGraph1</url-pattern>
</servlet-mapping>

如何在index.jsp?中显示图表我无法通过response.sendRedirect("index.jsp");执行此操作。

谢谢你能提供帮助。

0 个答案:

没有答案