import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; import com.brainysoftware.java.StringUtil; public class SearchServlet extends HttpServlet { private String keyword = ""; public void init() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("JDBC driver loaded"); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } } /**Process the HTTP Get request*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { sendPageHeader(response); sendSearchForm(response); sendPageFooter(response); } /**Process the HTTP Post request*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { keyword = request.getParameter("keyword"); sendPageHeader(response); sendSearchForm(response); sendSearchResult(response); sendPageFooter(response); } void sendSearchResult(HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); try { Connection con = DriverManager.getConnection("jdbc:odbc:JavaWeb"); System.out.println("got connection"); Statement s = con.createStatement(); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); String sql = "SELECT Id, FirstName, LastName, UserName, Password" + " FROM Users" + " WHERE FirstName LIKE '%" + StringUtil.fixSqlFieldValue(keyword) + "%'" + " OR LastName LIKE '%" + StringUtil.fixSqlFieldValue(keyword) + "%'"; ResultSet rs = s.executeQuery(sql); while (rs.next()) { String id = rs.getString(1); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); } s.close(); con.close(); } catch (SQLException e) { } catch (Exception e) { } out.println("
First NameLast NameUser NamePassword
" + StringUtil.encodeHtmlTag(rs.getString(2)) + "" + StringUtil.encodeHtmlTag(rs.getString(3)) + "" + StringUtil.encodeHtmlTag(rs.getString(4)) + "" + StringUtil.encodeHtmlTag(rs.getString(5)) + "DeleteUpdate
"); } /** * Send the HTML page header, including the title * and the tag */ private void sendPageHeader(HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Displaying Selected Record(s)"); out.println(""); out.println(""); out.println("
"); } /** * Send the HTML page footer, i.e. the * and the */ private void sendPageFooter(HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("
"); out.println(""); out.println(""); } /**Send the form where the user can type in * the details for a new user */ private void sendSearchForm(HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.println("

Search Form

"); out.println("
Please enter the first name, last name or part of any."); out.println("
"); out.println("
"); out.print("Name: "); out.println(""); out.println("
"); out.println("
"); out.println("
"); } }