Issue
How to print this void method of TsetServlet class on jsp??
public void printRecordInformation() {
System.out.println("****** Each Record INFORMATIN *********");
for(Record record : data) {
System.out.println(record);
}
}
//Here is the whole code of the class
/*
* 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.
*/
import Clustring.Cluster;
import Clustring.ConClass;
import Clustring.KMeansClusters;
import Clustring.Record;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author hp
*/
public class KMean extends HttpServlet {
List<Record> data;
List<Cluster> clusters = new ArrayList<Cluster>();
static Map<Cluster, List<Record>> clusterRecords = new HashMap<Cluster, List<Record>>();
@Override
public void init(){
}
public void initiateCluster(int clusterNumber) throws SQLException {
int counter = 1;
ConClass c2=new ConClass();
data = c2.getData();
Iterator<Record> iterator = data.iterator();
Record record = null;
while(iterator.hasNext()) {
record = iterator.next();
if(counter <= clusterNumber) {
record.setClusterNumber(counter);
initializeCluster(counter, record);
counter++;
//
}else {
System.out.println(record);
System.out.println("** Cluster Information **");
for(Cluster cluster : clusters) {
System.out.println(cluster);
}
System.out.println("*********************");
double minDistance = Integer.MAX_VALUE;
Cluster whichCluster = null;
for(Cluster cluster : clusters) {
double distance = cluster.calculateDistance(record);
System.out.println(distance);
if(minDistance > distance) {
minDistance = distance;
whichCluster = cluster;
}
}
record.setClusterNumber(whichCluster.getClusterNumber());
whichCluster.updateCentroid(record);
clusterRecords.get(whichCluster).add(record);
}
System.out.println("** Cluster Information **");
for(Cluster cluster : clusters) {
System.out.println(cluster);
}
System.out.println("*********************");
}
}
public void initializeCluster(int clusterNumber, Record record) {
Cluster cluster = new
Cluster(clusterNumber,record.getProvince(),record.getCountry(),record.getConfirmed(), record.getSuspected(), record.getRecovered(), record.getDeath());
clusters.add(cluster);
List<Record> clusterRecord = new ArrayList<Record>();
clusterRecord.add(record);
clusterRecords.put(cluster, clusterRecord);
}
public void printRecordInformation() {
System.out.println("****** Each Record INFORMATIN *********");
for(Record record : data) {
System.out.println(record);
}
}
public void printCluster() {
System.out.println("****** FINAL CLUSTER INFORMATIN *********");
for (Map.Entry<Cluster, List<Record>> entry : clusterRecords.entrySet()) {
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
}
public void genereateRecord() throws SQLException {
ConClass c1=new ConClass();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int clusterNumber = 2;
KMean clustr = new KMean();
clustr.genereateRecord();
clustr.initiateCluster(clusterNumber);
clustr.printRecordInformation();
clustr.printCluster();
}catch(Exception e){
} finally{
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Solution
Use out.println
instead of System.out.println
and pass the PrintWriter out
as parameter for the method.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int clusterNumber = 2;
KMean clustr = new KMean();
clustr.genereateRecord();
clustr.initiateCluster(clusterNumber,out);
clustr.printRecordInformation(out);
clustr.printCluster(out);
} catch (Exception e) {} finally {}
}
public void printRecordInformation(PrintWriter out) {
out.println("****** Each Record INFORMATIN *********");
for(Record record : data) {
out.println(record);
}
}
do the same for all methods that contains printing such as printCluster
public void printCluster(PrintWriter out) {
out.println("****** FINAL CLUSTER INFORMATIN *********");
for (Map.Entry<Cluster, List<Record>> entry : clusterRecords.entrySet()) {
out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
as well for initiateCluster
public void initiateCluster(int clusterNumber,PrintWriter out) throws SQLException {
...... // replace System.out.println by out.println
}
for printing table: try this (I didn't test it, I wrote it directly)
public void printRecordInformation(PrintWriter out) {
out.println("****** Each Record INFORMATIN *********");
out.println("<br>");
out.println("<table border='1' >");
out.println("<tr>");
out.println("<th>c1</th>");
out.println("</tr>");
for(Record record : data) {
out.println("<tr>");
out.print("<td>");
out.print(record);
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");
}
Answered By - Bashir