0

I literally tried every solution available and none of it works,my classpath is correct and working fine and I also tried adding mysqlconnector.jar in lib directory of servlet directory structure but to no avail was my problem resolved. By the Way, I am not using any IDE, I am coding via notepad and using tomcat manually to run servlet programs, It's easy to resolve problems in IDE but it is tough to do so in notepad.By the Way, I haveApache Tomcat 10 installed that uses jakarta instead of javax.The code of java servlet is mention below:

import java.util.*;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.sql.*;

public class loginn extends HttpServlet
{
    protected void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        String n=req.getParameter("uname");
        String p=req.getParameter("pass");
        out.println("<html><head><title>Login detail</title></head><body>");
        try
        {
            out.println("Yo!!!");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dad_servlet_assignment1","root","");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from login");
            boolean flag=false;
            while(rs.next() && !flag)
            {
                String un=rs.getString("username");
                out.println(un);
                String pass=rs.getString("password");
                if(n.equals(un) && p.equals(pass))
                {
                    flag=true;
                    break;
                }
            }
            out.println("YO!");
            if(flag)
            {
                out.println("<h1>Login Successful!</h1><br><br>");
                out.println("Welcome User: "+n+"<br><br>");
            }
            else
            {
                out.println("Invalid Username or password! <br><br>Enter Details Again<br><br>");
                RequestDispatcher rd=req.getRequestDispatcher("index.html");
                rd.forward(req,res);
            }

                out.println("</body></html>");
        }
        catch(Exception e)
        {
            out.println("Yikes");
            out.println("Exception: "+e.getMessage());
        }
        out.println("</body></html>");
    }
}

Guys, your help is highly appreciated! Thank you in Advance.

Solution:

By the way I found the solution myself in my case it wasn't able to find com.mysql.jdbc.Driver as my classpath for sqlconnector was in program files where as my apache classpath was in program files(x 86) hence I just added the mysqlconnector in apache tomcat 10's lib and then all I did was set an additional path for mysqlconnector.jar in environmental paths and tada! my problem solved!

0 Answers0