Вывести метод void класса сервлета на jsp - PullRequest
0 голосов
/ 17 июня 2020

Как напечатать этот метод void класса TsetServlet на jsp ??

public void printRecordInformation() {
       System.out.println("****** Each Record INFORMATIN *********");
       for(Record record : data) {
           System.out.println(record);
       }
   }

// Вот весь код класса

 /*
 * 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>

}

1 Ответ

0 голосов
/ 17 июня 2020

Используйте out.println вместо System.out.println и передайте PrintWriter out в качестве параметра для метода.

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);
       }
}

сделайте то же самое для всех методов, содержащих печать, например 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()); 
}

, а также для initiateCluster

public void initiateCluster(int clusterNumber,PrintWriter out) throws SQLException {
    ...... // replace System.out.println by out.println

}

для печати таблицы: попробуйте это (я не тестировал, Я написал прямо)

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>");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...