JSP Addressbook app for VIP Express (circa Jun 2002)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

191 lines
6.2 KiB

  1. // ListQueryServlet.java
  2. // Servlet that will use its request parameters to to generate a
  3. // collection of DataBeans with an SQL query. The SQL connection
  4. // is a persistent part of the HttpSession.
  5. package com.bullseyecomputing.servlets;
  6. import java.io.*;
  7. import java.util.*;
  8. import java.sql.*;
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11. import com.bullseyecomputing.beans.AddressListBean;
  12. public class ListQueryServlet extends HttpServlet {
  13. ServletContext context;
  14. ResultSet results;
  15. public void init(ServletConfig config) throws ServletException {
  16. super.init(config);
  17. // Grab a handle to our context
  18. context = getServletContext();
  19. // Get the jdbcDriver from an init param, and try to load it
  20. String jdbcDriver = getInitParameter("jdbcDriver");
  21. try {
  22. Class.forName(jdbcDriver);
  23. }
  24. catch(ClassNotFoundException e) {
  25. context.log("ListQueryServlet: jdbcDriver failure");
  26. }
  27. }
  28. public void doGet(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30. HttpSession session = request.getSession(false);
  31. ConnectionListener dbSession;
  32. Statement transaction;
  33. RequestDispatcher dispatcher;
  34. // If we have no session, we're in the wrong place
  35. if(session == null) {
  36. dispatcher = request.getRequestDispatcher("/index.jsp");
  37. dispatcher.forward(request, response);
  38. }
  39. // Look in the session environment for our database connection
  40. dbSession = (ConnectionListener) session.getAttribute("dbConnection");
  41. // Create one if needed
  42. if(dbSession == null) {
  43. String jdbcURL = getInitParameter("jdbcURL");
  44. String jdbcUser = getInitParameter("jdbcUser");
  45. String jdbcPassword = getInitParameter("jdbcPassword");
  46. try {
  47. Connection conn = DriverManager.getConnection(jdbcURL,jdbcUser,jdbcPassword);
  48. dbSession = new ConnectionListener(conn);
  49. session.setAttribute("dbConnection", dbSession);
  50. }
  51. catch(SQLException e) {
  52. context.log("Error creating database connection");
  53. }
  54. }
  55. // Get a local handle to the connection stored in the session
  56. Connection db = dbSession.getConnection();
  57. // Formulate our query based on init and request parameters
  58. String selected = getInitParameter("selected");
  59. String table = getInitParameter("table");
  60. String newFilter = request.getParameter("filter");
  61. String newOrder = request.getParameter("orderby");
  62. String newLimit = request.getParameter("limit");
  63. if(newFilter != null) { // add error checking for filter value
  64. session.setAttribute("filter", newFilter);
  65. }
  66. if(newOrder != null) { // add error checking for orderby value
  67. String oldOrder = (String) session.getAttribute("orderby");
  68. String direction = (String) session.getAttribute("direction");
  69. context.log("oldOrder = "+oldOrder+" "+direction);
  70. // Set initial orderby values
  71. if(oldOrder == null) { oldOrder = ""; }
  72. if(direction == null) { direction = "asc"; }
  73. // Toggle direction
  74. if(oldOrder.equals(newOrder)) {
  75. context.log("toggle direction");
  76. if(direction.equals("asc")) {
  77. direction = "desc";
  78. }
  79. else {
  80. direction = "asc";
  81. }
  82. }
  83. else {
  84. context.log("'"+oldOrder+"' != '"+newOrder+"'");
  85. }
  86. // Push values into session
  87. session.setAttribute("orderby", newOrder);
  88. session.setAttribute("direction", direction);
  89. context.log("newOrder = "+newOrder+" "+direction);
  90. }
  91. if(newLimit != null) { // add error checking for limit value
  92. session.setAttribute("limit", newLimit);
  93. }
  94. // Get values from session
  95. String filter = (String) session.getAttribute("filter");
  96. String orderby = (String) session.getAttribute("orderby");
  97. String direction = (String) session.getAttribute("direction");
  98. String limit = (String) session.getAttribute("limit");
  99. StringBuffer query = new StringBuffer();
  100. query.append("select ").append(selected);
  101. query.append(" from ").append(table);
  102. if(filter != null && filter != "") {
  103. StringTokenizer str = new StringTokenizer(filter,":");
  104. query.append(" where ").append(str.nextToken());
  105. query.append(" ~* '").append(str.nextToken()).append("'");
  106. // Multiple filters?
  107. while(str.hasMoreTokens()) {
  108. query.append(" and ").append(str.nextToken());
  109. query.append(" ~* '").append(str.nextToken()).append("'");
  110. }
  111. }
  112. if(orderby != null) {
  113. query.append(" order by ").append(orderby);
  114. }
  115. if(direction != null) {
  116. query.append(" ").append(direction);
  117. }
  118. if(limit != null) {
  119. query.append(" limit ").append(limit);
  120. }
  121. // Execute our calculated SQL query
  122. try {
  123. transaction = db.createStatement();
  124. context.log(query.toString());
  125. results = transaction.executeQuery(query.toString());
  126. // Traverse the ResultSet and create an ArrayList of DataBeans
  127. ArrayList resultList = new ArrayList();
  128. String dbField = "";
  129. while(results.next()) {
  130. // Create a new DataBean for each row of data
  131. AddressListBean address = new AddressListBean();
  132. // Push the result data into the DataBean
  133. address.setIdNumber(Integer.toString(results.getInt(1)));
  134. address.setName(results.getString(2));
  135. address.setStreet(results.getString(3));
  136. dbField = results.getString(4).trim();
  137. address.setCity(dbField.equals("") ? "�" : dbField);
  138. dbField = results.getString(5).trim();
  139. address.setState(dbField.equals("") ? "�" : dbField);
  140. dbField = results.getString(6).trim();
  141. address.setZipcode(dbField.equals("") ? "�" : dbField);
  142. dbField = results.getString(7).trim();
  143. address.setPlus4(dbField.equals("") ? "�" : dbField);
  144. // Add the DataBean to the ArrayList
  145. resultList.add(address);
  146. }
  147. // Push the ArrayList into the session environment
  148. session.setAttribute("resultList", resultList);
  149. // Close the query transaction
  150. results.close();
  151. transaction.close();
  152. }
  153. catch(SQLException e) {
  154. context.log("Error processing SQL Query", e);
  155. }
  156. // Forward on to the corresponding jsp
  157. dispatcher = context.getRequestDispatcher("/addressList.jsp");
  158. dispatcher.forward(request, response);
  159. }
  160. }