Java 通过doPost方法将JFreeChart发送到jsp
我想使用Java 通过doPost方法将JFreeChart发送到jsp,java,jsp,servlets,jfreechart,Java,Jsp,Servlets,Jfreechart,我想使用doPost方法将图表发送到jsp。用户可以从X.jsp中选择月份,然后在index.jsp上查看所选月份的图表 servlet public class RatingServletGraph1 extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
doPost
方法将图表发送到jsp
。用户可以从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);
}
}
公共类RatingServletGraph1扩展了HttpServlet{
public void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException,java.io.IOException{
连接=空;
试一试{
connection=ConnectionManager.getConnection();
}捕获(例外情况除外){
例如printStackTrace();
}
尝试
{
公共=新公共();
字符串fMonthCtrl=common.MonthControl(“fmonth”,null);
String LectID=(String)request.getSession().getAttribute(“CurrentSessionSerid”);
String LectPw=(String)request.getSession().getAttribute(“CurrentSessionUserPSA剑”);
字符串用户名=(字符串)请求.getSession().getAttribute(“currentSessionUser”);
字符串UserDept=(字符串)request.getSession().getAttribute(“currentSessionUserDepartment”);
int UserTtlCourse=(整数)request.getSession().getAttribute(“currentSessionUserClasses”);
String CourseID=(String)request.getSession().getAttribute(“currentSessionCourse”);
字符串fmonth=request.getParameter(“fmonth”);
RatingDAO评级=新的RatingDAO();
向量monthRating=新向量();
向量sdate=新向量();
JDBCCategoryDataset数据集=新的JDBCCategoryDataset(连接);
UserBean user=newuserbean();
RatingBean评级=新的RatingBean();
user.setID(LectID);
user.setPassword(LectPw);
额定值。setCourseid(CourseID);
额定值。设定月数(月数);
评级=评级dao.courseID(评级);
dataset.executeQuery(“从课程代码='“+CourseID+”,月份(课程日期)='“+fmonth+”,按课程日期分组的评级中选择课程日期、平均(评级));
JFreeChart chart=ChartFactory.createBarChart(“所选月份的每个类的评级:”、“日期”、“比率”、数据集);
图表.立根底色漆(颜色.灰色);
图表.油漆(颜色.黑色);
图.新的基本行程(3.0f);
图表.可见(真);
如果(数据集!=null)
{
HttpSession session=request.getSession(true);
整数宽度=400;
内部高度=350;
最终ChartRenderingInfo=新的ChartRenderingInfo(新标准EntityCollection());
response.setContentType(“image/png”);
setAttribute(“currentSessionUser”,用户名);
setAttribute(“currentSessionUserDepartment”,UserDept);
setAttribute(“currentSessionUserClasses”,UserTtlCourse);
session.setAttribute(“currentSessionCourse”,CourseID);
setAttribute(“fMonthCtrl”,fMonthCtrl);
session.setAttribute(“currentfmonth”,fmmonth);
session.setAttribute(“currentSessionSelectDate”,sdate);
setAttribute(“currentSessionMonthRating”,monthRating);
//sendRedirect(“index.jsp”);
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsPNG(输出、图表、宽度、高度、信息);
//Servlet JSP通信
RequestDispatcher reqDispatcher=getServletConfig().getServletContext().getRequestDispatcher(“/menu.jsp”);
请求调度员转发(请求、响应);
}
捕获(丢弃例外)
{
System.out.println(异常);
}
}
}
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>
</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>
评级服务图1
ProjectPackage.RatingServletGraph1
评级服务图1
/评级服务图1
如何使图形在index.jsp中可见?
我不能通过response.sendRedirect(“index.jsp”);
实现
如果你能帮忙,谢谢你