// The class dbtest has two functionalities: servlet and JSP-compliant bean. // When it is installed in a servlet-compatible Web server such as Java Web // Server or Jigsaw, it acts as a servlet. The entry point is service(). // // dbtest is also a server-side JavaBeans component. When it is installed // in an environment where Java Server Pages is supported, it acts as a // JSP-compatible bean. A JSP page can use this bean to access a database. package test; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.net.*; import java.sql.*; public class dbtest extends HttpServlet implements SingleThreadModel { // from basic.java Connection db; // The connection to the database Statement st; // Our statement to run queries with // internal stuff --------------------- private Connection _connection; private String _error = null; // bean properties ----------------------------------------------- private boolean _connected = false; private String _username = null; private String _password = null; // property - connected public boolean getConnected() { return _connected; } public void setConnected(boolean on) { if (on == _connected) return; // already in the requested state if (on) { System.out.println("dbtest.connect: called"); try { Class.forName("postgresql.Driver"); _connection = DriverManager.getConnection ("jdbc:postgresql:webdb", "postgres", "postgres"); _connected = on; } catch (Exception e) { _error = "dbtest.connect: " + e; System.out.println(_error); } } else // off { System.out.println("dbtest.disconnect: called"); try { _connection.close(); _connected = on; } catch (Exception e) { _error = "dbtest.disconnect: " + e; System.out.println(_error); } } } // property - username public String getUsername() { return _username; } public void setUsername(String username) { _username = username; } // property - password public String getPassword() { if (_connected == false || _username == null) return null; try { System.out.println("dbtest.query: called"); Statement stmt = _connection.createStatement(); ResultSet rs = stmt.executeQuery ("select * from club_y where c_name = '" + _username + "'"); if (rs.next()) _password = rs.getString(2); else _password = null; rs.close(); stmt.close(); } catch (Exception e) { _error = "dbtest.query: " + e; System.out.println(_error); _password = null; } return _password; } public void setPassword() {} // dummy // servlet entry --------------------------------------------------- public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("dbtest servlet"); String username = req.getParameterValues("username")[0]; out.println("Searching email address for " + username + " ...

"); setConnected(true); if (getConnected()) { setUsername(username); String c_email = getPassword(); if (c_email == null) out.println("Not found

"); else out.println("email address is " + c_email + "

"); setConnected(false); } if (_error != null) out.println("ERROR: " + _error + "

"); out.println(""); out.flush(); } }