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.

128 lines
4.0 KiB

  1. // DetailQueryServlet.java
  2. // This servlet receives an addressee identifier as a request argument,
  3. // and makes the required query/(queries) to retrieve the addressee's
  4. // detail info. This info is loaded into a DataBean that is then stored
  5. // in the session environment. The servlet then redirects to the correct
  6. // JSP for display.
  7. package com.bullseyecomputing.servlets;
  8. import java.io.*;
  9. import java.util.*;
  10. import java.sql.*;
  11. import javax.servlet.*;
  12. import javax.servlet.http.*;
  13. import com.bullseyecomputing.beans.AddressDataBean;
  14. public class DetailQueryServlet extends HttpServlet {
  15. ServletContext context;
  16. public void init(ServletConfig config) throws ServletException {
  17. super.init(config);
  18. // Grab a handle to the local context
  19. context = getServletContext();
  20. // Get the name of the jdbc driver from an init param
  21. String jdbcDriver = getInitParameter("jdbcDriver");
  22. // Now try to load the driver
  23. try {
  24. Class.forName(jdbcDriver);
  25. }
  26. catch(ClassNotFoundException e) {
  27. context.log("jdbcDriver: ", e);
  28. }
  29. }
  30. public void doGet(HttpServletRequest request, HttpServletResponse response)
  31. throws ServletException, IOException {
  32. HttpSession session = request.getSession(false);
  33. ConnectionListener dbListener;
  34. Statement transaction;
  35. ResultSet results;
  36. RequestDispatcher dispatcher;
  37. // Bail if we're not part of a session
  38. if(session == null) {
  39. dispatcher = context.getRequestDispatcher("/index.jsp");
  40. dispatcher.forward(request, response);
  41. }
  42. // Look for an existing db connection in the session environment
  43. dbListener = (ConnectionListener) session.getAttribute("dbConnection");
  44. // Create one if it's not there
  45. if(dbListener == null) {
  46. // Load the params from init
  47. String jdbcURL = getInitParameter("jdbcURL");
  48. String jdbcUser = getInitParameter("jdbcUser");
  49. String jdbcPassword = getInitParameter("jdbcPassword");
  50. // Attempt to make the connection
  51. try {
  52. Connection conn = DriverManager.getConnection(jdbcURL,jdbcUser,jdbcPassword);
  53. dbListener = new ConnectionListener(conn);
  54. // Push the new connection into the session environment
  55. session.setAttribute("dbConnection", dbListener);
  56. }
  57. catch(SQLException e) {
  58. context.log("dbConnection", e);
  59. }
  60. }
  61. // Grab a local handle to the session's db connection
  62. Connection db = dbListener.getConnection();
  63. // Build the query based on init and request parameters
  64. String selected = getInitParameter("selected");
  65. String table = getInitParameter("table");
  66. String id = request.getParameter("id");
  67. StringBuffer query = new StringBuffer();
  68. query.append("select ").append(selected);
  69. query.append(" from ").append(table);
  70. query.append(" where id = ").append(id);
  71. //
  72. context.log(query.toString());
  73. //
  74. // Execute the query
  75. try {
  76. transaction = db.createStatement();
  77. results = transaction.executeQuery(query.toString());
  78. // move to the first (and only) record
  79. results.next();
  80. // Create a DataBean to store the results
  81. AddressDataBean address = new AddressDataBean();
  82. // Push the data into the DataBean
  83. address.setIdNumber(Integer.toString(results.getInt(1)));
  84. address.setName(results.getString(2));
  85. address.setStreet(results.getString(3));
  86. address.setCity(results.getString(4));
  87. address.setState(results.getString(5));
  88. address.setZipcode(results.getString(6));
  89. address.setPlus4(results.getString(7));
  90. address.setAreacode(results.getString(8));
  91. address.setPhone(results.getString(9));
  92. // Push the AddressBean into the session environment
  93. session.setAttribute("address.data", address);
  94. // Close the database transaction
  95. results.close();
  96. transaction.close();
  97. }
  98. catch(SQLException e) {
  99. context.log("DetailQueryServlet: Error during SQL transaction", e);
  100. }
  101. // Forward to the corresponding JSP
  102. dispatcher = request.getRequestDispatcher("addressData.jsp");
  103. dispatcher.forward(request, response);
  104. }
  105. }