Servlet implementing runnable

Reply

Join Date: Jan 2009
Posts: 3
Reputation: kevin12 is an unknown quantity at this point 
Solved Threads: 0
kevin12 kevin12 is offline Offline
Newbie Poster

Servlet implementing runnable

 
0
  #1
Apr 27th, 2009
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


  1.  
  2. package Transaction;
  3.  
  4. import bean.*;
  5. import dealer.DealerDetails;
  6. import java.io.*;
  7. import java.sql.SQLException;
  8. import java.util.*;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.servlet.*;
  12. import javax.servlet.http.*;
  13. /*
  14.  * To change this template, choose Tools | Templates
  15.  * and open the template in the editor.
  16.  */
  17.  
  18.  
  19. /**
  20.  *
  21.  * @author kevo
  22.  */
  23. public class Transaction extends HttpServlet implements Runnable{
  24.  
  25. DealerDetails processTransaction = new DealerDetails();
  26. bean query = new bean();
  27. DealerDetails dealer = new DealerDetails();
  28. private String Originator;
  29. private String Amount;
  30. private String Receiver;
  31. private String Service;
  32. private String nService;
  33. private String TR48;
  34. private String Converse;
  35. private String dealerID = null;
  36. private String balance;
  37. private String MarginBalance;
  38. private String marginValue;
  39. private String purseValue;
  40. HttpServletRequest request = null;
  41. HttpServletResponse response = null;
  42. Thread Trans;
  43.  
  44. @Override
  45.  
  46. public void init(ServletConfig config) throws ServletException {
  47. super.init(config);
  48. Trans = new Thread(this);
  49. Trans.setPriority(Thread.MIN_PRIORITY);
  50. Trans.start();
  51.  
  52. }
  53.  
  54. public void processRequest(HttpServletRequest request, HttpServletResponse response)
  55. throws ServletException, IOException, SQLException {
  56. response.setContentType("text/html;charset=UTF-8");
  57. PrintWriter out = response.getWriter();
  58. try
  59. {
  60. Originator = request.getParameter("originator");
  61. String mes2 = request.getParameter("mes2");
  62. TR48 = request.getParameter("TR48");
  63. Converse = request.getParameter("converse");
  64.  
  65. String dealerVerify = "SELECT dealerID FROM tbldealerdetails WHERE Mobile = '"+Originator+"'";
  66. dealerID = query.select(dealerVerify);
  67. if (dealerID != null)
  68. {
  69.  
  70. StringTokenizer tok = new StringTokenizer(mes2, "+*-");
  71. if (tok.countTokens() >= 4) {
  72.  
  73. tok.nextToken();
  74. Receiver = tok.nextToken();
  75. Amount = tok.nextToken();
  76. Service = tok.nextToken();
  77.  
  78. String sqlService = "SELECT service FROM tblmargins WHERE (((dealerID = "+dealerID+" )OR (subdealerID = "+dealerID+")) AND (service = '"+Service+"'))";
  79. //String sqlService = "SELECT service FROM tblmargins WHERE (subdealerID = "+dealerID+" AND (service = '"+Service+"'))";
  80. nService = query.select(sqlService);
  81. System.out.println(Service);
  82. System.out.println("This is the service used when processing a transaction");
  83. System.out.println(nService);
  84. System.out.println("This is below");
  85. System.out.println(dealerID);
  86.  
  87. if (nService != dealerID)//(nService!=null)
  88. {
  89. if ((Receiver.length() == 8)||(Receiver.length() == 11))
  90. {
  91. String sqlmarginValue = "SELECT marginpurse FROM tblwallet WHERE dealerID = '"+dealerID+"'";
  92. marginValue = query.select(sqlmarginValue);
  93.  
  94. String sqlpurseValue = "SELECT currpurse FROM tblwallet WHERE dealerID = '"+dealerID+"'";
  95. purseValue = query.select(sqlpurseValue);
  96. System.out.println("This is the purse value " + purseValue);
  97.  
  98. if (Double.valueOf(Amount).doubleValue() < Double.valueOf(purseValue))
  99. {
  100. dealer.setdealerId(dealerID);
  101. dealer.setChild(dealerID);
  102. dealer.setSale(Amount);
  103. dealer.setService(Service);
  104. dealer.UpdateChildSales();
  105. dealer.UpdateParentWallet("w");
  106. System.out.println("This is coming ffrom the purse section");
  107. double ammnt = Double.valueOf(Amount).doubleValue();
  108. System.out.println("Thid id the double amt " +ammnt);
  109. }
  110.  
  111. else if(Double.valueOf(Amount).doubleValue() < Double.valueOf(marginValue))
  112. {
  113. dealer.setdealerId(dealerID);
  114. dealer.setChild(dealerID);
  115. dealer.setSale(Amount);
  116. dealer.setService(Service);
  117. //dealer.UpdateChildStock();
  118. dealer.UpdateChildSales();
  119. dealer.UpdateParentWallet("m");
  120. //process transaction
  121. System.out.println("This is coming ffrom the Margin section");
  122. }
  123.  
  124. else
  125. {
  126. out.println("You do not have enough money to process This transaction.");
  127. //out.println();
  128.  
  129. }
  130. }
  131.  
  132. else
  133. {
  134. out.println("You have entered an invalid cellphone number");
  135. }
  136. }
  137.  
  138. else
  139. {
  140. out.println("You are not allowed to provide the service you are attempting to use");
  141. }
  142. }
  143.  
  144. else if (tok.countTokens()==1) //Checking balance
  145. {
  146. String sqlbalance = "SELECT currpurse FROM tblwallet WHERE dealerID = '" + dealerID + "'";
  147. balance = query.select(sqlbalance);
  148.  
  149. String sqlMarginBalance = "SELECT marginpurse FROM tblwallet WHERE dealerID = '" + dealerID + "'";
  150. MarginBalance = query.select(sqlMarginBalance);
  151.  
  152. out.println("Your current balance is" +balance+ "and your margin balance is" +MarginBalance+"");
  153. }
  154.  
  155. else
  156. {
  157. out.println("Invalid number of Parameters");
  158. }
  159.  
  160. }
  161.  
  162. else
  163. {
  164. out.println("User not in database");
  165. }
  166. //System.out.println(dealerID);
  167.  
  168.  
  169. out.println("" + Receiver + "");
  170. out.println("" + Amount + "");
  171. processTransaction.setReceiver(Receiver);
  172. String K = processTransaction.getReceiver();
  173. out.println("" + K + "");
  174.  
  175. }
  176.  
  177. finally {
  178. out.close();
  179. }
  180.  
  181. }
  182.  
  183.  
  184.  
  185. public void run() {
  186. try {
  187. doGet(request, response);
  188. } catch (ServletException ex) {
  189. Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
  190. } catch (IOException ex) {
  191. Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
  192. }
  193. }
  194.  
  195. //@Override
  196.  
  197. //int i = 0;
  198.  
  199. /*@SuppressWarnings("static-access")
  200.   String ct = Trans.currentThread().getName();
  201.   System.out.println("This is the current thread" + ct);*/
  202.  
  203. @Override
  204. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  205. try {
  206. for(int i =0; i<5;i++)
  207. {System.out.println("hello" +dealerID +i);
  208. processRequest(request, response);
  209. try {
  210. Trans.sleep(10000);
  211. } catch (InterruptedException ex) {
  212. Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
  213. }
  214. }
  215. } catch (SQLException ex) {
  216. Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
  217. }
  218. }
  219.  
  220. /**
  221.   * Handles the HTTP <code>POST</code> method.
  222.   * @param request servlet request
  223.   * @param response servlet response
  224.   * @throws ServletException if a servlet-specific error occurs
  225.   * @throws IOException if an I/O error occurs
  226.   */
  227. @Override
  228. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  229. throws ServletException, IOException{
  230. try {
  231. processRequest(request, response);
  232. } catch (SQLException ex) {
  233. Logger.getLogger(Transaction.class.getName()).log(Level.SEVERE, null, ex);
  234. }
  235. }
  236.  
  237.  
  238.  
  239. @Override
  240. public void destroy() {
  241. System.out.println("hhello0");
  242. Trans.stop();
  243. }
  244. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Servlet implementing runnable

 
0
  #2
Apr 27th, 2009
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC