简体   繁体   中英

login is not working in my servlet program

I have a MySQL database where I created a userid "mads" and password "mads". I would like to test through a JSP site and servlet if the user exist in the database. I think my code is correct, but I keep getting the wrong exception... that my user is not valid. So I guess my code is not correct :-/ The connection to my database is successful. Without knowing it, then I have a feeling of that my servlet looks at the last column in my database and not the first? I don't know if that have anything to do with it. Can anybody see what it is wrong?

Best Regards Mads My JSP page looks like this:

<title>Validation</title>
</head>
<body>
    <br><br><br>
        <center>
            <h1>Please enter user name and password</h1>

            <form name="frm" action="LoginValidation" method="post">
                <input type="text" name="user">
                <input type ="password" name="pass">
                <input type="submit" value="Check">
            </form>
        </center>
</body>

and my Servlet here:

package jsp;

import java.io.*;
//import java.util.*;
import java.sql.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

@WebServlet(urlPatterns = {"/LoginValidation"})
public class Validation extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private ServletConfig config;

    public void init (ServletConfig config) 
    throws ServletException{
        this.config = config;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,IOException {


        PrintWriter out = response.getWriter();
        String connectionURL = "jdbc:mysql://localhost/dblogin";
        Connection connection = null;
        ResultSet rs;
        String userid = new String("");
        String password = new String("");

        response.setContentType("text/html");
        try {
            // Load the database driver
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", "");
            //Add the data into the database
            String sql = "SELECT userid, password FROM login";
            Statement s = connection.createStatement();
            s.executeQuery(sql);
            rs = s.getResultSet();

            while(rs.next()) {
                userid = rs.getString("userid");
                password = rs.getString("password");
            }
            rs.close();
            s.close();

        } catch(Exception e) {
            System.out.println("Exception is: " + e);
          }

            if(userid.equals(request.getParameter("userid")) && password.equals(request.getParameter("password"))) {
                out.println("The user is valid");
            } 
            else {
                out.println("You are not valid");
            }
    }

}

In this code you are comparing user input values with table's last row'data....write compare code inside the while loop to compare all values....and make redirection if any value match with database values........

package jsp;

import java.io.*;
//import java.util.*;
import java.sql.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

 @WebServlet(urlPatterns = {"/LoginValidation"})
public class Validation extends HttpServlet {

private static final long serialVersionUID = 1L;
private ServletConfig config;

public void init (ServletConfig config) 
throws ServletException{
    this.config = config;
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {


    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/dblogin";
    Connection connection = null;
    ResultSet rs;
    String userid = new String("");
    String password = new String("");

    response.setContentType("text/html");
    try {
        // Load the database driver
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(connectionURL, "root", "");
        //Add the data into the database
        String sql = "SELECT userid, password FROM login";
        Statement s = connection.createStatement();
        s.executeQuery(sql);
        rs = s.getResultSet();

        while(rs.next()) {
            userid = rs.getString("userid");
            password = rs.getString("password");

                if(userid.equals(request.getParameter("userid")) && password.equals(request.getParameter("password"))) 
                {
                    out.println("The user is valid");

                    //Write exit program code or rediraction code here
                } 

        }
        rs.close();
        s.close();

    } catch(Exception e) {
        System.out.println("Exception is: " + e);
      }


 }

}

//I think it is not efficent way to make login........but i just gave you problem solution in your type login.........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM