Контроллер входа ниже
package com.vinay.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vinay.dao.LoginDao;
import com.vinay.dao.impl.LoginDoaImpl;
/**
* Servlet implementation class LoginController
*/
@WebServlet("/login")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static LoginDao loginDao = null;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
loginDao = new LoginDoaImpl();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(loginDao.login(userName, password));
}
}
jsp файла в разделе 'webapp / jsp'
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="login" method="post">
<label for="username">UserName</label>
<input type="text" id="username" placeholder="Usernmae" name="username">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
Doa Classes
package com.vinay.dao;
public interface LoginDao {
boolean login(String userName, String password);
}
DBConnectionManager
package com.vinay.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnectionManager {
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER_NAME = "";
private static final String PASSWORD = "";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER_NAME, PASSWORD);
}
}
Реализованные классы DAO
package com.vinay.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.vinay.dao.DBConnectionManager;
import com.vinay.dao.LoginDao;
public class LoginDoaImpl implements LoginDao {
@Override
public boolean login(String userName, String password) {
String sql = "SELECT * FROM users WHERE username=? AND password=?;";
try(Connection connection = DBConnectionManager.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);){
statement.setString(1, userName);
statement.setString(2, password);
return statement.execute();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
pom. xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vinay</groupId>
<artifactId>testj2eewebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>testj2eewebapp Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testj2eewebapp</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Я пытался использовать проект maven
Попробуйте код для вас здесь необходимо загрузить драйвер в блоке stati c, поскольку класс драйвера не инициализирован . Следовательно, это вызовет исключение. Я надеюсь, что этот код поможет вам.