/******************************************************************** * * StateQuerier.java * * Last Modified: 5/12/00 * * This is a one servlet example which implements a form which * asks for the name of a state, and then sends theinputted data * to the same servlet by the POST method. The method doPost() * then handles the POST request queries the STATE table and * then display the results. * ********************************************************************/ import java.io.*; import java.text.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class StateQuerier extends HttpServlet { public static String USERNAME = new String("user"); public static String PASSWORD = new String("pass"); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); String title = "State Info Query Engine"; out.println("" + title + ""); out.println(""); out.println(""); out.println("

" + title + "

"); String param = request.getParameter("param"); if (param != null) { out.println("Thanks for the lovely param='" + param + "' binding."); } out.println("

"); out.print("

"); out.println("State Name:"); out.println(""); out.println("
"); out.println("
"); out.println("
"); out.println("
"); out.println(""); out.println("
"); out.println(""); out.println(""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String stateName = request.getParameter("statename"); String state_capital = ""; String state_flower = ""; String state_bird = ""; response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); if (stateName != null) { out.println("" + "You have requested info for " + stateName + ""); out.println(""); out.println(""); try { // Load the Oracle Driver Class.forName("oracle.jdbc.driver.OracleDriver"); } catch(ClassNotFoundException e) { } try { // Get a connection from the connection factory Connection con = DriverManager.getConnection("jdbc:oracle:thin:@oracle-prod:1521:8PRD", USERNAME, PASSWORD); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT CAPITAL, FLOWER, BIRD FROM STATE WHERE NAME = '" + stateName + "'"); while(rs.next()) { state_capital = rs.getString("CAPITAL"); state_flower = rs.getString("FLOWER"); state_bird = rs.getString("BIRD"); //out.println("State Name: " + state_name + "Capital: " + state_capital); } } catch (SQLException e) { } if(state_capital.equals("")) { out.println("No info for State " + stateName + ""); } else { out.println("State " + stateName + " has capital at " + state_capital + "

"); out.println("The State Flower of " + stateName + " is " + state_flower + "

"); out.println("The State Bird of " + stateName + " is " + state_bird + "

"); } } else { out.println("" + "You have not requested a state name" + ""); out.println(""); out.println(""); } out.println(""); out.println(""); } }