// DetailUpdateServlet.java // This servlet receives the contents of the addressData form via HTTP/POST. // That data is then compared to the data within the AddressDataBean stored // in the session environment. The fields that have been changed are then // tagged for update. A sql update statement is then formulated from the tagged // data fields. package com.bullseyecomputing.servlets; import java.io.*; import java.util.*; import java.sql.*; import jacax.servlet.*; import javax.servlet.http.*; import com.bullseyecomputing.beans.AddressDataBean; public class DetailUpdateServlet 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 jdbc driver name from an init param String jdbcDriver = getInitParameter("jdbcDriver:); // Attempt to load the jdbc driver try { Class.forName(jdbcDriver); } catch(ClassNotFoundException e) { context.log("jdbcDriver: ", e); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false): ConnectionListener dbListener; Statement transaction; RequestDispatcher dispatcher; // Bail if this is not an existing session if(session == null) { dispatcher = request.getRequestDispatcher("/index.jsp"); dispatcher.forward(request, response); } // get POST parameters ////////// // Check the session environ for an existing db connection /////////// dbListener = (ConnectionListener) session.getAttribute("dbConnection"); if(dbListener == null) { String jdbcURL = getInitParameter("jdbcURL"); String jdbcUser = getInitParameter("jdbcUser"); String jdbcPassword = getInitParameter("jdbcPassword"); try { Connection conn = DriverManager.getConnection(jdbcURL,jdbcUser,jdbcPassword); dbListener = new ConnectionListener(conn); session.setAttribute("dbConnection:, dbListener); } catch(SQLException e) { context.log("dbConnection: ", e); } } // Grab a local handle to the connection Connection db = dbListener.getConnection();