package net.titaniclinux.daogen.examples; import java.io.*; import java.util.*; import java.text.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Example2 extends HttpServlet { private ServletConfig config; private ServletContext context; private CustomerDao customerHandler; private String jdbcDriver; private String jdbcUrl; private String jdbcLogin; private String jdbcPasswd; public void init(ServletConfig servletConfig) throws ServletException { // Store initial servlet configuration: config = servletConfig; context = config.getServletContext(); // Database Connection information. In real applications the database // connections are usually pooled and these connection parameters are // not hard-coded, but instead configurable in properties files or // web.xml files. However, for simplicity we keep them here now. jdbcDriver = "jdbc:mysql"; jdbcUrl = "localhost/test"; jdbcLogin = "username"; jdbcPasswd = "password"; try { // If you do not use MySQL database, you need to change the MySQL // driver to your own JDBC database driver. (See the line below:) DriverManager.registerDriver(new org.gjt.mm.mysql.Driver()); } catch (SQLException error) { System.out.println(error.toString()); throw new ServletException(error.toString()); } // Create the DAO component instance. This object can be created // easily, since the constructor takes no arguments. We will need // the Database resources (Connection) only when we want to actually // use this DAO for something. Note: All DAO classes that are generated // with DaoGen are thread safe! This means, that for multi threaded // programs (like HttpServlets) you need to create only one instance // and you can then share it for all requests (threads). The advantage // of doing so is that your programs will use less memory. customerHandler = new CustomerDao(); } public void destroy() { System.out.println("Example servlet is shutting down."); } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // This servlet will handle all requests the same way: doPost (request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the database connection from helper method: Connection conn = this.getNewConnection(); try { // First, load all customers from database. This can be done, // if the amount of customers is not huge. In this example we // expect to have only few customers, so we have no problem. You can // also use countAll() method before loadAll() to make sure you // are not loading too many records. List allCustomers = customerHandler.loadAll(conn); // Set the Request attribute, so that we can access the data in JSP. // The allCustomers list contains now the value objects for all records // in database, but these value objects are completely detached from the // database and we can handle this list without even knowing about JDBC // and the overhead of database queries. request.setAttribute("customerList", allCustomers); } catch (SQLException error) { System.out.println(error.toString()); throw new ServletException(error.toString()); } finally { // Whatever happens, close the opened connection. This does not affect // the Value Objects that are already loaded in runtime memory. this.closeConnection(conn); } // Finally, forward the request to JSP page for presentation. // You should never code the html inside your servlets, and even // this is the most simple servlet example, we avoid it too! response.setContentType("text/html"); RequestDispatcher reqd = context.getRequestDispatcher("/customer-list.jsp"); reqd.include(request, response); } /** * Private methods for handling Database Connection. This is the most simple way * to use the connection, but it has several drawbacks. The connections are not * pooled, and so the performance suffers. Also, this is considered as an old-style * way for handling connection, as all new application servers support convenient * pooled datasources. However, their configuration can be difficult to understand * for newbies, so this simplistic way should be enough for this example program. */ private Connection getNewConnection() throws ServletException { try { // Now we use the DriverManager to get the connection to Database: Connection conn = DriverManager.getConnection (jdbcDriver + "://" + jdbcUrl, jdbcLogin, jdbcPasswd); return conn; } catch (Exception error) { System.out.println("Database connection can not be created!"); System.out.println("Error description: " + error.toString()); throw new ServletException(error.toString()); } } private void closeConnection(Connection conn) { try { // Close the database connection. conn.close(); } catch (Exception error) { System.out.println("Error when closing Database connection!"); } } }