0

I have been given an assignment where I have to create an HTML, servlet and connect it to the database for login. My code doesn't have errors but when I enter the credentials in HTML it gives "HTTP Status 404 - Not Found

Type Status Report

Message The requested resource [/loginDemo] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."

HMTL

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center>
        <div>
            <form action= "loginDemo" method ="POST">
            <table>
                <tr>
                    <td>Username</td>
                    <td><input type = "text" class = "form-control" name = "username" placeholder = "username"></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type = "password" class = "form-control" name = "password" placeholder = "password"></td>
                </tr>
                <tr>
                    <td colspan = "8" style="text-align: right"><input class="btn btn-success" type = "submit" value = "login" ><a href="">Forgot Password<a/></td>
                
                </tr>
               
            </table>
            </form> 
        </div>
    </center>
    </body>
</html>

welcomePage html

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <h1> Welcome to this page</h1>
    </body>
</html>

Servlet Code

import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 *
 * @author HP
 */
@WebServlet(urlPatterns = {"/loginDemo"})
public class loginDemo extends HttpServlet {

  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
            try{
               // Class.forName("com.mysql.cj.jdbc.Driver");
               Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login_credentials", "root", "root");//Establishing connection
               
               System.out.println("Database connection was Successful");
               
               
                String username = request.getParameter("username"); 
                String password = request.getParameter("password");
                
                PreparedStatement prepStatement = connection.prepareStatement("select *from users");
                
               //creating resultSet objet
               ResultSet resultSet = prepStatement.executeQuery();
               
               //getting results
               String u = resultSet.getString("username");
               String p = resultSet.getString("password");
               
               if(resultSet.next()){
                   RequestDispatcher rd = request.getRequestDispatcher("welcomePage.html");
                   rd.forward(request, response);
               }else{
                   out.println("<font color = red size=22>Login Failed!!</br>");
                   out.println("<a href = index.html>Try Again!!!</a>");
                   
               }
               
                
        
            } catch (SQLException e){
                System.out.println("Error connection to the Database!!!");
                
            } 
          }
    

}

Can you please help...

0 Answers0