Hi, I am writing a jsp app. Thing is I cannot get the "hhello0" String in the destroy method to echo after the thread is stopped, fourth line from the bottom.
Thanx already
:S

package Transaction;

import bean.*;
import dealer.DealerDetails;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author kevo
 */
public class Transaction extends HttpServlet implements Runnable{

    DealerDetails processTransaction = new DealerDetails();
    bean query = new bean();
    DealerDetails dealer = new DealerDetails();
    private String Originator;
    private String Amount;
    private String Receiver;
    private String Service;
    private String nService;
    private String TR48;
    private String Converse;
    private String dealerID = null;
    private String balance;
    private String MarginBalance;
    private String marginValue;
    private String purseValue;
    HttpServletRequest request = null;
    HttpServletResponse response = null;
    Thread Trans;

    @Override

    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    Trans = new Thread(this);
    Trans.setPriority(Thread.MIN_PRIORITY);
    Trans.start();

    }

        public void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            Originator = request.getParameter("originator");
            String mes2 = request.getParameter("mes2");
            TR48 = request.getParameter("TR48");
            Converse = request.getParameter("converse");

            String dealerVerify = "SELECT dealerID FROM tbldealerdetails WHERE Mobile = '"+Originator+"'";
            dealerID = query.select(dealerVerify);
            if (dealerID != null)
            {

                StringTokenizer tok = new StringTokenizer(mes2, "+*-");
                if (tok.countTokens() >= 4) {

                    tok.nextToken();
                    Receiver = tok.nextToken();
                    Amount = tok.nextToken();
                    Service = tok.nextToken();

                    String sqlService = "SELECT service FROM tblmargins WHERE (((dealerID = "+dealerID+" )OR (subdealerID = "+dealerID+")) AND (service = '"+Service+"'))";
                    //String sqlService = "SELECT service FROM tblmargins WHERE (subdealerID = "+dealerID+" AND (service = '"+Service+"'))";
                    nService = query.select(sqlService);
                    System.out.println(Service);
                    System.out.println("This is the service used when processing a transaction");
                    System.out.println(nService);
                    System.out.println("This is below");
                    System.out.println(dealerID);

                    if (nService != dealerID)//(nService!=null) 
                    {
                           if ((Receiver.length() == 8)||(Receiver.length() == 11))
                            {
                               String sqlmarginValue = "SELECT marginpurse FROM tblwallet WHERE dealerID = '"+dealerID+"'";
                               marginValue = query.select(sqlmarginValue);

                               String sqlpurseValue = "SELECT currpurse FROM tblwallet WHERE dealerID = '"+dealerID+"'";
                               purseValue = query.select(sqlpurseValue);
                                System.out.println("This is the purse value " + purseValue);

                               if (Double.valueOf(Amount).doubleValue() < Double.valueOf(purseValue))
                               {
                                   dealer.setdealerId(dealerID);
                                   dealer.setChild(dealerID);
                                   dealer.setSale(Amount);
                                   dealer.setService(Service);
                                   dealer.UpdateChildSales();
                                   dealer.UpdateParentWallet("w");
                                   System.out.println("This is coming ffrom the purse section");
                                   double ammnt = Double.valueOf(Amount).doubleValue();
                                   System.out.println("Thid id the double amt " +ammnt);
                               }

                               else if(Double.valueOf(Amount).doubleValue() < Double.valueOf(marginValue))
                               {
                                   dealer.setdealerId(dealerID);
                                   dealer.setChild(dealerID);
                                   dealer.setSale(Amount);
                                   dealer.setService(Service);
                                   //dealer.UpdateChildStock();
                                   dealer.UpdateChildSales();
                                   dealer.UpdateParentWallet("m");
                                   //process transaction
                                   System.out.println("This is coming ffrom the Margin section");
                               }

                               else
                               {
                                   out.println("You do not have enough money to process This transaction.");
                                   //out.println();

                               }
                            }

                           else
                           {
                               out.println("You have entered an invalid cellphone number");
                           }
                    }

                    else
                    {
                        out.println("You are not allowed to provide the service you are attempting to use");
                    }
                    }

                else if (tok.countTokens()==1) //Checking balance
                {
                    String sqlbalance = "SELECT currpurse FROM tblwallet WHERE dealerID = '" + dealerID + "'";
                    balance = query.select(sqlbalance);

                    String sqlMarginBalance = "SELECT marginpurse FROM tblwallet WHERE dealerID = '" + dealerID + "'";
                    MarginBalance = query.select(sqlMarginBalance);

                    out.println("Your current balance is" +balance+ "and your margin balance is" +MarginBalance+"");
                }

                else
                {
                    out.println("Invalid number of Parameters");
                }

            }

            else
            {
                out.println("User not in database");
            }
            //System.out.println(dealerID);


            out.println("" + Receiver + "");
            out.println("" + Amount + "");
            processTransaction.setReceiver(Receiver);
            String K = processTransaction.getReceiver();
            out.println("" + K + "");

        }

        finally {
            out.close();
        }

        }



    public void run() {
        try {
            doGet(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //@Override

//int i = 0;

        /*@SuppressWarnings("static-access")
        String ct = Trans.currentThread().getName();
        System.out.println("This is the current thread" + ct);*/

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            for(int i =0; i<5;i++)
            {System.out.println("hello" +dealerID +i);
            processRequest(request, response);
                try {
                    Trans.sleep(10000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 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{
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    

    @Override
    public void destroy() {
        System.out.println("hhello0");
    Trans.stop();
        }
}

You don't need to make your servlet implement the Runnable interface; each request is anyways handled and served in a separate thread. The destroy method of a servlet is called when the servlet is put out of commission. As an exercise, try shutting down Tomcat and monitor the SDTOUT or log files for your message string "hhello0".

I would recommend reading the JEE 5 tutorial to know more about servlets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.