Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.54 KB | None | 0 0
  1. package com.examples.servlets;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.servlet.RequestDispatcher;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18.  
  19. /**
  20. *
  21. * @author Surajitdas
  22. */
  23. public class Search extends HttpServlet {
  24. private String mobilenumber;
  25. private String country;
  26. /**
  27. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  28. * methods.
  29. *
  30. * @param request servlet request
  31. * @param response servlet response
  32. * @throws ServletException if a servlet-specific error occurs
  33. * @throws IOException if an I/O error occurs
  34. */
  35. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37. response.setContentType("text/html;charset=UTF-8");
  38. try (PrintWriter out = response.getWriter()) {
  39. /* TODO output your page here. You may use following sample code. */
  40. out.println("<!DOCTYPE html>");
  41. out.println("<html>");
  42. out.println("<head>");
  43. out.println("<title>Servlet Search</title>");
  44. out.println("</head>");
  45. out.println("<body>");
  46. out.println("<h1>Servlet Search at " + request.getContextPath() + "</h1>");
  47. out.println("</body>");
  48. out.println("</html>");
  49. }
  50. }
  51.  
  52. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  53. /**
  54. * Handles the HTTP <code>GET</code> method.
  55. *
  56. * @param request servlet request
  57. * @param response servlet response
  58. * @throws ServletException if a servlet-specific error occurs
  59. * @throws IOException if an I/O error occurs
  60. */
  61. @Override
  62. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  63. throws ServletException, IOException {
  64. int count =0;
  65. PrintWriter out = response.getWriter();
  66. try {
  67. //processRequest(request, response);
  68. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  69. Connection conn = null;
  70. PreparedStatement myStmt = null;
  71. ArrayList al = new ArrayList();
  72. ArrayList pid = new ArrayList();
  73. conn = DriverManager.getConnection("jdbc:sqlserver://MPESA\SQL2012;user=realm;password=friend;database=ElmaTest");
  74.  
  75. if(conn!=null)
  76. {
  77. out.println("Connection Succcesful");
  78. myStmt = conn
  79. .prepareStatement("exec p_GetAccountCustomer ?,?,?,?,?,?,?");
  80.  
  81. myStmt.setString(1, country);
  82. myStmt.setString(2, "99");
  83. myStmt.setString(3, mobilenumber);
  84. myStmt.setString(4, "");
  85. myStmt.setString(5, "0");
  86. myStmt.setString(6, "2001");
  87. myStmt.setString(7, "127.0.0.1");
  88. ResultSet rs= myStmt.executeQuery();
  89. while(rs.next())
  90. {
  91. count ++;
  92. String country = rs.getString("CustomerCountry");
  93. String customerid = rs.getString("CustomerID");
  94. String TitleofAccount = rs.getString("TitleofAccount");
  95. String FirstName = rs.getString("FirstName");
  96. String LastName = rs.getString("LastName");
  97. String City = rs.getString("City");
  98. String Address = rs.getString("Address");
  99. String emailid = rs.getString("EmailID");
  100. String typeofid = rs.getString("TypeOfID");
  101. String Idnumber = rs.getString("IDNumber");
  102. String branchid = rs.getString("BranchID");
  103. String cardnumber = rs.getString("CardNumber");
  104. String bankaccntid = rs.getString("BankAccountID");
  105. String currencyid = rs.getString("CurrencyID");
  106. String isspeciallimit = rs.getString("IsSpecialLimit");
  107. String dailylimit = rs.getString("DayTransactionLimit");
  108. al.add(rs.getString("CardNumber"));
  109. al.add(bankaccntid);
  110. al.add(currencyid);
  111. al.add(rs.getString("DayTransactionLimit"));
  112. al.add(isspeciallimit);
  113. pid.add(al);
  114.  
  115.  
  116. System.out.println("Count" + count);
  117. System.out.println(country);
  118. System.out.println(customerid);
  119. System.out.println(TitleofAccount);
  120. System.out.println(FirstName);
  121. System.out.println(LastName);
  122. System.out.println(City);
  123.  
  124. System.out.println(Address);
  125. System.out.println(emailid);
  126. System.out.println(typeofid);
  127. System.out.println(Idnumber);
  128. System.out.println(branchid);
  129. System.out.println(cardnumber);
  130.  
  131.  
  132. System.out.println(bankaccntid);
  133. System.out.println(currencyid);
  134. System.out.println(isspeciallimit);
  135. request.setAttribute("count", count);
  136.  
  137. request.setAttribute("country", country);
  138. request.setAttribute("customerid", customerid);
  139. request.setAttribute("TitleofAccount", TitleofAccount);
  140. request.setAttribute("FirstName", FirstName);
  141. request.setAttribute("LastName", LastName);
  142.  
  143. request.setAttribute("City", City);
  144. request.setAttribute("emailid", emailid);
  145. request.setAttribute("typeofid", typeofid);
  146. request.setAttribute("Idnumber", Idnumber);
  147. request.setAttribute("branchid", branchid);
  148.  
  149. request.setAttribute("cardnumber", cardnumber);
  150. request.setAttribute("bankaccntid", bankaccntid);
  151. request.setAttribute("currencyid", currencyid);
  152. request.setAttribute("isspeciallimit", isspeciallimit);
  153. request.setAttribute("dailylimit", dailylimit);
  154.  
  155. request.setAttribute("pid",pid);
  156.  
  157. RequestDispatcher rd = request.getRequestDispatcher("CustomerDetails.jsp");
  158. rd.forward(request, response);
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. }
  168.  
  169.  
  170. }
  171. } catch (ClassNotFoundException ex) {
  172. Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
  173. } catch (SQLException ex) {
  174. Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
  175. }
  176.  
  177. }
  178.  
  179. /**
  180. * Handles the HTTP <code>POST</code> method.
  181. *
  182. * @param request servlet request
  183. * @param response servlet response
  184. * @throws ServletException if a servlet-specific error occurs
  185. * @throws IOException if an I/O error occurs
  186. */
  187. @Override
  188. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  189. throws ServletException, IOException {
  190. //processRequest(request, response);
  191. PrintWriter out = response.getWriter();
  192. mobilenumber = request.getParameter("mobilenumber");
  193. country = request.getParameter("country");
  194.  
  195. doGet(request,response);
  196. //out.println(mobilenumber);
  197. }
  198.  
  199. /**
  200. * Returns a short description of the servlet.
  201. *
  202. * @return a String containing servlet description
  203. */
  204. @Override
  205. public String getServletInfo() {
  206. return "Short description";
  207. }// </editor-fold>
  208.  
  209.  
  210.  
  211. }
  212.  
  213. <%@page import="java.util.Iterator"%>
  214. <%@page import="java.util.ArrayList"%>
  215. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  216. <!DOCTYPE html>
  217. <html xmlns="http://www.w3.org/1999/xhtml">
  218. <head>
  219. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  220. <title>HomePage</title>
  221. <script src="JavaScript/SpryMenuBar.js" type="text/javascript"></script>
  222. <link href="styles/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
  223. </head>
  224.  
  225. <body>
  226. <ul id="MenuBar1" class="MenuBarVertical">
  227. <li><a href="#">Update Limit</a></li>
  228. </li>
  229. <li><a href="#">Delink Account</a></li>
  230. <li><a href="#">Edit CardNumber</a>
  231.  
  232. </li>
  233. <li>
  234. <a href="#">Linked Accounts</a>
  235. </li>
  236. <li><a href="#">SMS Alerts</a>
  237. <li><a href="#">Join Account</a>
  238.  
  239.  
  240. </ul>
  241. <p>&nbsp;</p>
  242. <p>&nbsp;</p>
  243.  
  244. <form method="post" action="Search">
  245. <hr />
  246. <hr />
  247. <p>
  248. Client Details
  249. <hr />
  250. <hr />
  251. </p>
  252. <p>Mobile Number:-
  253. <label>
  254. <input type="text" name="mobilenumber" id="mobilenumber" />
  255. </label>
  256.  
  257. </p>
  258. <p>Country:-
  259. <label>
  260. <input type="text" name="country" id="country" />
  261. </label>
  262.  
  263. </p>
  264.  
  265. <input type="submit" formaction="Search" name="New" id="New" value="Search" />
  266.  
  267.  
  268.  
  269. </form>
  270. </html>
  271. <html>
  272. <form method ="get" action="Search">
  273. <p> Title:-
  274. <input type="text" name="Title" id="Title" value='<%=(request.getAttribute("TitleofAccount")==null)?"":request.getAttribute("TitleofAccount")%>' />
  275. </p>
  276. <p>Middle Name:-
  277. <label>
  278. <input type="text" name="MiddleName" id="Middle Name" />
  279. </label>
  280. </p>
  281. <p>Address:-
  282. <label>
  283. <input type="text" name="address" id="address" />
  284. </label>
  285. </p>
  286. <p>City:-
  287. <label>
  288. <input type="text" name="City" id="City"value='<%=(request.getAttribute("City")==null)?"":request.getAttribute("City")%>'/>
  289. </label>
  290. </p>
  291. <p>Type of ID:-
  292. <label>
  293. <input type="text" name="Idtype" id="Idtype" value='<%=(request.getAttribute("typeofid")==null)?"":request.getAttribute("typeofid")%>' />
  294. </label>
  295. </p>
  296. <p>Elma ID:-
  297. <label>
  298. <input type="text" name="elmaid" id="elmaid" value='<%=(request.getAttribute("customerid")==null)?"":request.getAttribute("customerid")%>'/>
  299. </label>
  300. </p>
  301. <p>First Name:-
  302. <label>
  303. <input type="text" name="firstname" id="firstname" value='<%=(request.getAttribute("FirstName")==null)?"":request.getAttribute("FirstName")%>' />
  304. </label>
  305. </p>
  306. <p>Last Name:-
  307. <label>
  308. <input type="text" name="LastName" id="LastName" value='<%=(request.getAttribute("LastName")==null)?"":request.getAttribute("LastName")%>'/>
  309. </label>
  310. </p>
  311. <p>Registration Branch:-
  312. <label>
  313. <input type="text" name="registrationbranch" id="registrationbranch" />
  314. </label>
  315. </p>
  316. <p>Email address:-
  317. <label>
  318. <input type="text" name="emailid" id="emailid" value='<%=(request.getAttribute("emailid")==null)?"":request.getAttribute("emailid")%>' />
  319. </label>
  320. </p>
  321. <p>ID No:-
  322. <label>
  323. <input type="text" name="idno" id="idno" />
  324. </label>
  325. </p>
  326. <hr />
  327. <hr />
  328. <p>
  329. Account Details
  330. <hr />
  331. <hr />
  332. </p>
  333.  
  334. <p>Bank A/C id :-
  335. <label>
  336. <input type="text" name="accountid" id="accountid"value='<%=(request.getAttribute("bankaccntid")==null)?"":request.getAttribute("bankaccntid")%>' />
  337. </label>
  338. </p>
  339. <p>Transaction Limit:-
  340. <label>
  341. <input type="text" name="transactionlimit" id="transactionlimit" />
  342. </label>
  343. </p>
  344. <p>Currency:-
  345. <input type="text" name="currency" id="currency"value='<%=(request.getAttribute("currencyid")==null)?"":request.getAttribute("currencyid")%>'/>
  346. </p>
  347. <p>Daily Limit:-
  348. <label>
  349. <input type="text" name="dailylimit" id="dailylimit" value='<%=(request.getAttribute("dailylimit")==null)?"":request.getAttribute("dailylimit")%>'/>
  350. </label>
  351. </p>
  352. <p>
  353. <input type="submit"formaction="Connectioncheck" name="Update" id="Update" value="Update" />
  354. </p>
  355. <p>&nbsp;</p>
  356. <hr />
  357. Account List
  358. <hr />
  359.  
  360.  
  361.  
  362. <table width="700px" align="center"
  363. style="border:1px solid #000000;">
  364. <tr>
  365. <td colspan=4 align="center"
  366. style="background-color:teal">
  367. <b>User Record</b></td>
  368. </tr>
  369. <tr style="background-color:lightgrey;">
  370. <td><b>Account No</b></td>
  371. <td><b>Card Number</b></td>
  372. <td><b>CurrencyID</b></td>
  373. <td><b>Daily Limit</b></td>
  374. <td><b>Status Limit</b></td>
  375.  
  376.  
  377. </tr>
  378.  
  379. <%
  380. // Integer count = Integer.parseInt((String)session.getAttribute("count"));
  381. int count =0;
  382. String color = "#F9EBB3";
  383. if (request.getAttribute("pid") != null) {
  384. ArrayList al = (ArrayList) request.getAttribute("pid");
  385. System.out.println(al);
  386. Iterator itr = al.iterator();
  387. while (itr.hasNext()) {
  388.  
  389. if ((count % 2) == 0) {
  390. color = "#eeffee";
  391. }
  392. count++;
  393. ArrayList pList = (ArrayList) itr.next();
  394. %>
  395. <tr style="background-color:<%=color%>;">
  396. <td><%=pList.get(0)%></td>
  397. <td><%=pList.get(1)%></td>
  398. <td><%=pList.get(2)%></td>
  399. <td><%=pList.get(3)%></td>
  400. <td><%=pList.get(4)%></td>
  401.  
  402. </tr>
  403. <%
  404. }
  405. }
  406. if (count == 0) {
  407. %>
  408. <tr>
  409. <td colspan=4 align="center"
  410. style="background-color:#eeffee"><b>No Record Found..</b></td>
  411. </tr>
  412. <% }
  413. %>
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424. </table>
  425.  
  426.  
  427. </form>
  428. </html>
  429.  
  430. Warning: StandardWrapperValve[Search]: Servlet.service() for servlet Search threw exception
  431. java.lang.IllegalStateException: Cannot forward after response has been committed
  432. at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448)
  433. at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
  434. at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
  435. at com.examples.servlets.Search.doGet(Search.java:163)
  436. at com.examples.servlets.Search.doPost(Search.java:200)
  437. at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
  438. at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  439. at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  440. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
  441. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  442. at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  443. at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  444. at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  445. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  446. at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
  447. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
  448. at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
  449. at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
  450. at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
  451. at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
  452. at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
  453. at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  454. at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
  455. at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
  456. at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
  457. at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
  458. at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  459. at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
  460. at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
  461. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
  462. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
  463. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
  464. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
  465. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
  466. at java.lang.Thread.run(Thread.java:745)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement