简体   繁体   中英

NetBeans Servlet MySQL

THIS HAS BEEN SOLVED

The problem lay with the parameter names.

I am trying to create a basic login servlet that will check the login details of employees.

However my servlet doesn't seem to be interacting with the data base and always returns me to the "tryagain.jsp". What am I doing wrong? I am using glassfish and netbeans. I have j/connector driver in the library files as well.

THIS HAS BEEN SOLVED

The problem lay with the parameter names.

Here is the code.

package loginServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
 *
 * @author asus
 */
public class Login1 extends HttpServlet {


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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        Connection conn;
        PreparedStatement ps;
        ResultSet rs;
        String DRIVER = "com.mysql.jdbc.Driver";
        String DATABASE_URL = "jdbc:mysql://localhost:3306/northwind";
       String uname=request.getParameter("username");
String passwd=request.getParameter("userPass");
        try {
            // load the driver class
            Class.forName(DRIVER);

            // establish connection to database                              
            conn =
                    DriverManager.getConnection(DATABASE_URL, "jonh86", "ADMIN");
            System.out.println("Connected....");
            ps = conn.prepareStatement("select * from userdata where user_name = ? and password = ?");
            ps.setString(1, uname);
            ps.setString(2, passwd);
            rs = ps.executeQuery();
            if (rs.next()) {

            RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet");
            rd.forward(request, response);

//out.println(rs.getString(2));
            } else {
                RequestDispatcher rd = request.getRequestDispatcher("tryagain.jsp");
            rd.include(request, response);
            }
        } 


        catch (Exception e) {
            System.out.println(e);
        }

    }

}

You can find the reason yourself with some proper error checking. There are heaps of reasons why it could fail.

// establish connection to database                              
            conn = DriverManager.getConnection(DATABASE_URL, "jonh86", "ADMIN");
            if(conn == null) System.out.println("Could not Connect");
            System.out.println("Connected...."); //--this code is misleading
            ps = conn.prepareStatement("select * from userdata where user_name = ? and password = ?");
            ps.setString(1, uname);
            ps.setString(2, passwd);
            rs = ps.executeQuery();

Also, you should be catching SQLExceptions and printing them:

catch (SQLException e) {
    System.out.println("Exception = " + e);
}

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