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

// DetailQueryServlet.java
// This servlet receives an addressee identifier as a request argument,
// and makes the required query/(queries) to retrieve the addressee's
// detail info. This info is loaded into a DataBean that is then stored
// in the session environment. The servlet then redirects to the correct
// JSP for display.
package com.bullseyecomputing.servlets;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.bullseyecomputing.beans.AddressDataBean;
public class DetailQueryServlet extends HttpServlet {
ServletContext context;
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Grab a handle to the local context
context = getServletContext();
// Get the name of the jdbc driver from an init param
String jdbcDriver = getInitParameter("jdbcDriver");
// Now try to load the driver
try {
Class.forName(jdbcDriver);
}
catch(ClassNotFoundException e) {
context.log("jdbcDriver: ", e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
ConnectionListener dbListener;
Statement transaction;
ResultSet results;
RequestDispatcher dispatcher;
// Bail if we're not part of a session
if(session == null) {
dispatcher = context.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
// Look for an existing db connection in the session environment
dbListener = (ConnectionListener) session.getAttribute("dbConnection");
// Create one if it's not there
if(dbListener == null) {
// Load the params from init
String jdbcURL = getInitParameter("jdbcURL");
String jdbcUser = getInitParameter("jdbcUser");
String jdbcPassword = getInitParameter("jdbcPassword");
// Attempt to make the connection
try {
Connection conn = DriverManager.getConnection(jdbcURL,jdbcUser,jdbcPassword);
dbListener = new ConnectionListener(conn);
// Push the new connection into the session environment
session.setAttribute("dbConnection", dbListener);
}
catch(SQLException e) {
context.log("dbConnection", e);
}
}
// Grab a local handle to the session's db connection
Connection db = dbListener.getConnection();
// Build the query based on init and request parameters
String selected = getInitParameter("selected");
String table = getInitParameter("table");
String id = request.getParameter("id");
StringBuffer query = new StringBuffer();
query.append("select ").append(selected);
query.append(" from ").append(table);
query.append(" where id = ").append(id);
//
context.log(query.toString());
//
// Execute the query
try {
transaction = db.createStatement();
results = transaction.executeQuery(query.toString());
// move to the first (and only) record
results.next();
// Create a DataBean to store the results
AddressDataBean address = new AddressDataBean();
// Push the data into the DataBean
address.setIdNumber(Integer.toString(results.getInt(1)));
address.setName(results.getString(2));
address.setStreet(results.getString(3));
address.setCity(results.getString(4));
address.setState(results.getString(5));
address.setZipcode(results.getString(6));
address.setPlus4(results.getString(7));
address.setAreacode(results.getString(8));
address.setPhone(results.getString(9));
// Push the AddressBean into the session environment
session.setAttribute("address.data", address);
// Close the database transaction
results.close();
transaction.close();
}
catch(SQLException e) {
context.log("DetailQueryServlet: Error during SQL transaction", e);
}
// Forward to the corresponding JSP
dispatcher = request.getRequestDispatcher("addressData.jsp");
dispatcher.forward(request, response);
}
}