Example usage for java.io PrintWriter print

List of usage examples for java.io PrintWriter print

Introduction

In this page you can find the example usage for java.io PrintWriter print.

Prototype

public void print(Object obj) 

Source Link

Document

Prints an object.

Usage

From source file:com.netscape.cmstools.user.UserCertShowCLI.java

public void execute(String[] args) throws Exception {
    // Always check for "--help" prior to parsing
    if (Arrays.asList(args).contains("--help")) {
        printHelp();/*from   www  .ja va 2 s.  c  o m*/
        return;
    }

    CommandLine cmd = parser.parse(options, args);

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length != 2) {
        throw new Exception("Incorrect number of arguments specified.");
    }

    boolean showPrettyPrint = cmd.hasOption("pretty");
    boolean showEncoded = cmd.hasOption("encoded");

    String userID = cmdArgs[0];
    String certID = cmdArgs[1];
    String file = cmd.getOptionValue("output");

    UserClient userClient = userCertCLI.getUserClient();
    UserCertData userCertData = userClient.getUserCert(userID, URLEncoder.encode(certID, "UTF-8"));

    String encoded = userCertData.getEncoded();
    if (encoded != null && file != null) {
        // store cert to file
        PrintWriter out = new PrintWriter(new FileWriter(file));
        out.print(encoded);
        out.close();
    }

    MainCLI.printMessage("Certificate \"" + userCertData.getID() + "\"");

    UserCertCLI.printCert(userCertData, showPrettyPrint, showEncoded);
}

From source file:com.ss.Controller.T4uArrangeSeatPlanServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  www.  j  a  v  a2s .  c o  m*/
 *
 * @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 {
    int scheduleId = 0;
    try {
        scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
        String[] allUSeats = request.getParameterValues("seats[]");
        // Check whether the seats are occupied or unavailable in table T4U_schedule
        boolean occupied = false;
        if (allUSeats != null) {
            String oSeats = T4uScheduleDAO.getOSeatsById(scheduleId);
            for (String seat : allUSeats)
                if (oSeats.contains(seat)) {
                    occupied = true;
                    break;
                }
        }
        if (occupied) {
            // Seat occupied, need to buy again
            PrintWriter out = response.getWriter();
            out.print("occupied");
            out.flush();
        } else {
            String uSeats = "";
            if (allUSeats != null)
                for (String seat : allUSeats)
                    uSeats += "'" + seat + "',";
            if (T4uScheduleDAO.updateUSeatsById(scheduleId, uSeats)) {
                response.setHeader("Content-Type", "text/plain");
                PrintWriter out = response.getWriter();
                out.print("success");
                out.flush();
            }
        }
    } catch (NumberFormatException ex) {
    }
}

From source file:feedme.controller.AddOrDeleteCategoryServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);// w  w w. j ava  2s . c  o m
    request.setCharacterEncoding("UTF-8");
    response.setStatus(HttpServletResponse.SC_OK);
    String CategoryName = request.getParameter("categoryName").trim();
    DbRestaurantsManagement ob = new DbRestaurantsManagement();

    boolean flag = false;

    HashMap<String, Integer> cat = new DbHPOnLoad().getCategories();

    for (String se : cat.keySet()) {
        if (se.contains(CategoryName))
            flag = true;
    }
    int result = 0;
    if (!flag)
        result = ob.addNewCategory(CategoryName);

    PrintWriter out = response.getWriter();
    if (result == 1) {
        if (isAjax(request) == true) { // Stay in the same page, and sand json message

            try {
                cat = new DbHPOnLoad().getCategories();
                JSONObject catObj = new JSONObject();
                JSONArray catArray = new JSONArray();
                for (Entry<String, Integer> entry : cat.entrySet()) {
                    catArray.put(
                            new JSONObject().put("cat_id", entry.getValue()).put("cat_name", entry.getKey()));
                }
                catObj.put("categories", catArray);
                catObj.put("status", true);
                response.setContentType("application/json");
                PrintWriter writer = response.getWriter();
                writer.print(catObj);
                response.getWriter().flush();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else { // redirect to othe page

        }
    }
}

From source file:Employee.DateGetter.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w ww  .j  a  v  a  2 s. c om
 *
 * @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("application/json");

    //create and initialize our profile object with passed in parameters
    String ssn = request.getParameter("SSN");
    String profileId = "";
    profileId = request.getParameter("profileId");
    JSONArray jsons = new JSONArray();
    getDates(ssn, jsons, profileId);
    PrintWriter printout = response.getWriter();
    printout.print(jsons);
}

From source file:joanakeyrefactoring.javaforkeycreator.JavaProjectCopyHandler.java

public void addClassToTest(List<String> classContent, StaticCGJavaClass javaClass)
        throws FileNotFoundException, IOException {
    String relPathForJavaClass = getRelPathForJavaClass(javaClass);
    String className = javaClass.getOnlyClassName();
    File relPathFile = new File(pathToNew + "/" + relPathForJavaClass);
    relPathFile.mkdirs();//from  ww  w.j  a v  a  2 s .  com
    File javaFile = new File(pathToNew + relPathForJavaClass + "/" + className + ".java");
    javaFile.createNewFile();
    String classFileAsOneString = "";
    for (String l : classContent) {
        classFileAsOneString += l + System.lineSeparator();
    }
    PrintWriter out = new PrintWriter(javaFile);
    out.print(classFileAsOneString);
    out.close();
}

From source file:net.bashtech.geobot.BotManager.java

public static String postRemoteDataTwitch(String urlString, String postData, int krakenVersion) {
    URL url;/* w  w w  .  j  a v  a 2  s. c  om*/
    HttpURLConnection conn;

    try {
        url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        conn.setFixedLengthStreamingMode(postData.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept", "application/vnd.twitchtv.v" + krakenVersion + "+json");
        conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken);
        conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID);
        // conn.setConnectTimeout(5 * 1000);
        // conn.setReadTimeout(5 * 1000);

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        inStream.close();
        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:Employee.CreateMailingList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request//from  w  ww  . j  a v  a 2s. co  m
 * @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");
    JSONArray jsonArray = new JSONArray();
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;user=sa;password=nopw");

        Statement st = con.createStatement();

        String query = "SELECT P.SSN, P.FirstName, P.LastName, P.Email "
                + "FROM [MatchesFromAbove].[dbo].[Person] P, [MatchesFromAbove].[dbo].[Customer] C"
                + " WHERE C.Active = 'true' AND P.SSN = C.SSN";

        ResultSet rs = st.executeQuery(query);
        //loop through result set and create the json objects
        while (rs.next()) {
            JSONObject emailToAdd = new JSONObject();
            emailToAdd.put("ssn", rs.getString("SSN"));
            emailToAdd.put("firstName", rs.getString("FirstName"));
            emailToAdd.put("lastName", rs.getString("LastName"));
            emailToAdd.put("email", rs.getString("Email"));
            //add the json object that we're passing into the json array
            jsonArray.add(emailToAdd);
        }
        //set the content type of our response
        response.setContentType("application/json");
        //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function.
        PrintWriter printout = response.getWriter();
        printout.print(jsonArray);
        printout.flush();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return;
    }
}

From source file:Employee.getAllCustomers.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w ww  .  j  ava 2  s.c o m
 *
 * @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 {
    //json to pass back to our ajax request
    JSONArray jsonArray = new JSONArray();
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;user=sa;password=nopw");

        Statement st = con.createStatement();

        String query = "SELECT * FROM [MatchesFromAbove].[dbo].[Customer] WHERE active=1";

        ResultSet rs = st.executeQuery(query);

        //loop through result set and create the json objects
        while (rs.next()) {
            JSONObject customerToAdd = new JSONObject();
            customerToAdd.put("ssn", rs.getString("SSN"));
            customerToAdd.put("ppp", rs.getString("PPP"));
            customerToAdd.put("rating", rs.getInt("Rating"));
            customerToAdd.put("lastActiveDate", rs.getTimestamp("LastActive").toString());
            //add the json object that we're passing into the json array
            jsonArray.add(customerToAdd);
        }
        //set the content type of our response
        response.setContentType("application/json");
        //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function.
        PrintWriter printout = response.getWriter();
        printout.print(jsonArray);
        printout.flush();
        con.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return;
    }
}

From source file:net.bashtech.geobot.BotManager.java

public static String postCoebotConfig(String postData, String channel) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;//w ww . j  a  va2 s. co  m
        HttpURLConnection conn;

        try {
            url = new URL("http://coebot.tv/api/v1/channel/update/config/" + channel.toLowerCase() + "$"
                    + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();

            String response = "";

            Scanner inStream = new Scanner(conn.getInputStream());

            while (inStream.hasNextLine())
                response += (inStream.nextLine());

            inStream.close();
            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return "";
    } else
        return "";
}

From source file:net.bashtech.geobot.BotManager.java

public static String postCoebotVars(String postData, String requestURL) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;/*from  w  w w. j  a  v  a  2 s  . c  o m*/
        HttpURLConnection conn;

        try {
            url = new URL(requestURL + "$" + BotManager.getInstance().CoeBotTVAPIKey + "$"
                    + BotManager.getInstance().nick);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();

            String response = "";

            Scanner inStream = new Scanner(conn.getInputStream());

            while (inStream.hasNextLine())
                response += (inStream.nextLine());

            inStream.close();
            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return "";
    } else
        return "";
}