Hey! I've got a little problem. I have to make a web site for a university essay. I curently have to create a search engine. Users can enter a hotel name in a search bar and results have to appear in another screen. All of this has to be done with java servlets. I think there's something I don't see and it's wrong. My problem is that anything I enter in the search bar appears as a result in the results page, even if there's not such a name in the database. So if i enter eg "asdfa" it will show asdfa in the results page. Here's the code I've made so far. I think that the java file that connects to the database is right. I checked the query in MySQL and it's right. The search form is also right, using the post method and forwarding input to the Search Receiver servlet. The SearchReceiver file might be the wrong one... Any suggestions, help, heads up? :)
SearchReceiver
Database Connector
SearchReceiver
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
* @author ismgroup
*/
public class SearchReceiver extends HttpServlet {
/**
* Handles HTTP POST requests.
*
* @param request
* the request object
* @param response
* the response object
*
* @throws IOException
* if an input or output error is detected when the servlet
* handles the GET request
* @throws ServletException
* if the request for the GET could not be handled
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html; charset=ISO-8859-7");
PrintWriter out = new PrintWriter(response.getWriter(), true);
/*
* gets parameters from the request.
*/
String searchInput = request.getParameter("nameField");
/*
* Converts from ISO-8859-1 to ISO-8859-7 (Greek) in case
* the user typed his/her name in Greek
*/
try {
/*
* checks if all the parameters have value.
*/
dbConnector theConnector2 = new dbConnector(searchInput);
theConnector2.open();
if(!(searchInput.length()>0)) {
theConnector2.close();
RequestDispatcher dispatcher1 = getServletContext().getRequestDispatcher("/servlet/searchError");
dispatcher1.forward(request, response);
}
theConnector2.searchProcess();
theConnector2.close();
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>");
out.println("<html>");
out.println("<head>");
out.println(" <meta content='text/html; charset=windows-1253'");
out.println(" http-equiv='content-type'>");
out.println(" <title>Search Results</title>");
out.println("</head>");
out.println("<body style='color: rgb(0, 0, 0); background-color: rgb(51, 255, 51);'");
out.println(" alink='#000099' link='#000099' vlink='#990099'>");
out.println("<div style='text-align: center;'>");
out.println("<h1>Search Results</h1>");
out.println("<br>");
out.println(searchInput + "<br>");
out.println("<span style='font-weight: bold;'><br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("</span>");
out.println("<hr style='width: 100%; height: 2px;'>");
out.println("<div style='text-align: left;'><span style='font-weight: bold;'></span>Back to");
out.println("στην <a href='../optimusHotels.html'>main page...</a><br>");
out.println("<span style='font-weight: bold;'></span></div>");
out.println("<br>");
out.println("</div>");
out.println("</body>");
out.println("</html>");
theConnector2.close();
} catch (Exception ex) {
out.println("<html>");
out.println("<body");
out.println("Exception: " + ex.getMessage());
out.println("</body>");
out.println("</html>");
}
}
}
Code:
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Provides all necessary methods in order to carry out a transaction with
* database.
*
* @author Nikos
*/
class dbConnector {
private String searchInput = "";
private String errorMessages = "";
private Connection con = null;
private PreparedStatement stmt = null;
private ResultSet rs = null
[B]private final String searchQuery = "select hotelName from hotel where hotelName like '%?%'";[/B]
/**
* A method to get errors.
*
* @return String, representing the error message.
*/
public String getErrorMessages() {
return errorMessages;
}
/**
* The Constructor.
*
*/
public dbConnector(String searchInput) {
this.searchInput = searchInput;
}
/**
* Provides a connection with the Database Server. Initializes JDBC driver
* for MySQL. Establishes a connection with the Database Server.
*
* @throws SQLException
* (with the appropriate message) if any driver or connection
* error occured.
*/
public void open() throws SQLException {
try {
// for JDBC driver to connect to mysql, the .newInstance() method
// can be ommited
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e1) {
errorMessages = "MySQL Driver error: <br>" + e1.getMessage();
throw new SQLException(errorMessages);
}
try {
con = DriverManager.getConnection(
"jdbc:mysql://*"******",
"******", "******");
} catch (Exception e2) {
errorMessages = "Could not establish connection with the Database Server: <br>"
+ e2.getMessage();
con = null;
throw new SQLException(errorMessages);
}
}
/**
* Ends the connection with the database Server. Closes all Statements and
* ResultSets. Finally, closes the connection with the Database Server.
*
* @throws SQLException
* (with the appropriate message) if any error occured.
*/
public void close() throws SQLException {
try {
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
if (con != null)
con.close();
} catch (Exception e3) {
errorMessages = "Could not close connection with the Database Server: <br>"
+ e3.getMessage();
throw new SQLException(errorMessages);
}
}
[B] public boolean searchProcess()[/B] {
if (con == null) {
errorMessages = "You must establish a connection first!";
return false;
}
try {
stmt = con.prepareStatement(searchQuery);
stmt.setString(1, searchInput);
// execute query
rs = stmt.executeQuery();
int counter = 0;
while (rs.next())
counter++;
if (counter == 1) {
stmt.close();
rs.close();
return true;
} else {
errorMessages = "Error Login: <br>Invalide username or password!";
stmt.close();
rs.close();
return false;
}
} catch (Exception e4) {
errorMessages = "Error while executing authentication query: <br>"
+ e4.getMessage();
return false;
}
}
}// end of class
Comment