0

大家好,尝试创建一个简单的数据库登录 java servlet。它会直奔 Login failed. Please try again.

这是我的数据库中的集合,我有表user- id, uniid, name, password,email

例如我在表中有以下数据

uniid - 18357593
name - Jared Hoight
password - password
email - jhall@students.monash.edu.au

这是完整的登录servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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 javax.servlet.RequestDispatcher;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    private void sendLoginForm(HttpServletResponse response, 
              boolean withErrorMessage) 
              throws ServletException, IOException {

              response.setContentType("text/html"); 
              PrintWriter out = response.getWriter(); 
              out.println("<HTML>"); 
              out.println("<HEAD>"); 
              out.println("<TITLE>Login</TITLE>"); 
              out.println("</HEAD>"); 
              out.println("<BODY>"); 
              out.println("<CENTER>"); 

              if (withErrorMessage) 
                out.println("Login failed. Please try again.<BR>"); 

              out.println("<BR>"); 
              out.println("<BR><H2>Login Page</H2>"); 
              out.println("<BR>"); 
              out.println("<BR>Please enter your user name and password."); 
              out.println("<BR>"); 
              out.println("<BR><FORM METHOD=POST>"); 
              out.println("<TABLE>"); 
              out.println("<TR>"); 
              out.println("<TD>User Name:</TD>"); 
              out.println("<TD><INPUT TYPE=TEXT NAME=uniid></TD>"); 
              out.println("</TR>"); 
              out.println("<TR>"); 
              out.println("<TD>Password:</TD>"); 
              out.println("<TD><INPUT TYPE=PASSWORD NAME=password></TD>"); 
              out.println("</TR>"); 
              out.println("<TR>"); 
              out.println("<TD ALIGN=RIGHT COLSPAN=2>"); 
              out.println("<INPUT TYPE=SUBMIT VALUE=Login></TD>"); 
              out.println("</TR>"); 
              out.println("</TABLE>"); 
              out.println("</FORM>"); 
              out.println("</CENTER>"); 
              out.println("</BODY>"); 
              out.println("</HTML>"); 
            } 


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        sendLoginForm(response, false); 

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, 
              HttpServletResponse response) 
              throws ServletException, IOException {  
                String uniid = request.getParameter("uniid"); 
                String password = request.getParameter("password"); 
                if (login(uniid, password)) {
                  RequestDispatcher rd = 
                    request.getRequestDispatcher("AnotherServlet"); 
                  rd.forward(request, response); 
                } 
                else {
                  sendLoginForm(response, true); 
                } 

    }

     boolean login(String uniid, String password) {
            try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
              Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
              System.out.println("got connection"); 

              Statement s = con.createStatement(); 
              String sql = "SELECT uniid FROM user" + 
                " WHERE uniid='" + uniid + "'" + 
                " AND password='" + password + "'"; 
              ResultSet rs = s.executeQuery(sql); 
              if (rs.next()) {
                rs.close(); 
                s.close(); 
                con.close(); 
                return true; 
              } 
                rs.close(); 
                s.close(); 
                con.close(); 
            } 
            catch (ClassNotFoundException e) {
              System.out.println(e.toString()); 
            } 
            catch (SQLException e) {
              System.out.println(e.toString()); 
            } 
            catch (Exception e) {
              System.out.println(e.toString()); 
            } 
            return false; 
          } 
        }
4

1 回答 1

1

数据库驱动程序

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

不是与 MySQL 一起使用的正确方法。它应该是

Class.forName("com.mysql.jdbc.Driver");

当您的login方法中发生异常时,将打印异常字符串并返回 false。

于 2012-10-01T01:25:12.490 回答