recomm.java Source code

Java tutorial

Introduction

Here is the source code for recomm.java

Source

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rishabh
 */
public class recomm extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            /* TODO output your page here. You may use following sample code. */

            final String fiat = "FIAT";
            final String audi = "AUDI";
            final String ford = "FORD";
            final String speed = "Speed";
            final String millage = "Millage";
            final String userrating = "User Rating";
            final String safety = "safety";

            final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            dataset.addValue(1.0, fiat, speed);
            dataset.addValue(3.0, fiat, userrating);
            dataset.addValue(5.0, fiat, millage);
            dataset.addValue(5.0, fiat, safety);

            dataset.addValue(1.0, audi, speed);
            dataset.addValue(1.0, audi, userrating);
            dataset.addValue(1.0, audi, millage);
            dataset.addValue(1.0, audi, safety);

            dataset.addValue(4.0, ford, speed);
            dataset.addValue(2.0, ford, userrating);
            dataset.addValue(3.0, ford, millage);
            dataset.addValue(6.0, ford, safety);

            JFreeChart barChart = ChartFactory.createBarChart("CAR USAGE STATISTICS", "Category", "Score", dataset,
                    PlotOrientation.VERTICAL, true, true, false);

            int width = 640; /* Width of the image */
            int height = 480; /* Height of the image */
            File BarChart = new File("/home/rishabh/###amit/JavaApplication3BarChart.jpeg");
            ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet recomm</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet recomm at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}