import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.ResultSet;
//jstl.jar wird gebraucht f¸r jsp.jstl....
public class Login extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// }
// doPost(req,res);
/*public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {*/
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
boolean login = false;
RequestDispatcher rq = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
ServletContext conn = getServletContext();
final String dbHost = "jdbc:mysql://localhost:3306/";
final String dbTable = "login";
final String dbUser = "root";
final String dbPassword = "root";
try {
// Load (and therefore register) the mysql Driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
// con = DriverManager.getConnection(
// "jdbc:mysql://localhost/login?user=root&password=root");
con = DriverManager.getConnection(dbHost + dbTable, dbUser, dbPassword);
// Create a Statement object
stmt = con.createStatement();
// Execute an SQL query, get a ResultSet
// Display the result set as a list
rs = stmt.executeQuery("Select * from logindata");
while(rs.next()) {
login = true;
}
if ((req.getParameter("username") == rs.getString("username")) && (req.getParameter("password") == rs.getString("password"))) {
out.println("Herzlich willkommen " + req.getParameter("username"));
}
else {
login = false;
rq = conn.getRequestDispatcher("/login.html");
rq.forward(req, res);
}
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
catch (SQLException ignored) { }
}
}
}