Kodlar normal gibi geldi. LongServlet'in tanınmasında bir sıkıntı olabilir. Yine de kodları aşağıda attığım gibi revize edip deneyin isterseniz.
JSP:
<body>
<div class="login-box">
<h2>Giriş</h2>
<form action="LoginServlet" method="post">
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Şifre" required>
<button type="submit">Giriş</button>
<div class="register-link">
<p>Hesabınız yok mu? <a href="kayit.jsp">Kayıt olun</a></p>
</div>
</form>
</div>
</body>
</html>
LoginServlet:
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String sifre = request.getParameter("password");
// Veritabanı bağlantısı oluşturma
String url = "jdbc:postgresql://localhost:5432/Mulakat";
String username = "postgres";
String dbPassword = "jacobo93";
try {
Connection baglanti = DriverManager.getConnection(url, username, dbPassword);
// Veritabanı sorgusu oluşturma
String query = "SELECT * FROM users WHERE email = ? AND sifre = ?";
PreparedStatement preparedStatement = baglanti.prepareStatement(query);
preparedStatement.setString(1, email);
preparedStatement.setString(2, sifre);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
// Giriş başarılı
response.getWriter().write("Giriş başarılı!");
} else {
// Giriş başarısız
response.getWriter().write("Giriş başarısız!");
response.sendRedirect("kayit.jsp");
}
// Kapatma işlemleri
resultSet.close();
preparedStatement.close();
baglanti.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Veritabanı bağlantısı:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect {
static String url = "jdbc:postgresql://localhost:5432/Mulakat";
public static Connection baglanti = null;
public static void connect() throws SQLException {
try {
// JDBC sürücüsünü yükleme (eğer gerekliyse)
// Class.forName("org.postgresql.Driver");
baglanti = DriverManager.getConnection(url, "postgres", "jacobo93");
if(baglanti != null) {
System.out.println("Veritabanı bağlantısı tamamlandı");
} else {
System.out.println("Veritabanı bağlantısı başarısız.");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}