Eclipse新建web项目,实现登录验证功能

安装好MySQl数据库

Eclipse新建web项目,实现登录验证功能_第1张图片

新建JavaWeb项目

项目名称为test3

Eclipse新建web项目,实现登录验证功能_第2张图片


    在Web-Content目录下新建login.jsp文件
       在浏览器中,需要提交的东西,使用表单form标签来进行提交,form中的action的值是表单数据提交到的jsp或Servlet的地址    
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Pagetitle> 
head>
<body>
<form action="LoginServlet">                                    //此处的LoginServlet为相对位置
username:<input type ="text" name="username"/><br>

password:<input type = "password" name="password"/><br>

<input type ="submit" name ="submit"/>




form>
body>
html>

新建名为MySQL的类来进行数据库操作

package com.lx.uestc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQL {

    private Connection connection = null;
public  MySQL()
{

    /*加载JdBC驱动
     * 
     */
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("load Driver successfully");
    } catch (Exception e) {
        System.out.println("load  Driver error");
        e.printStackTrace();
    }




    /*连接数据库
     * 
     */
    try {
       connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","liangxi333");
        //jdbc:mysql://localhost:端口号/数据库名称","用户名","密码"
        System.out.println("connect sql successfully");
    } catch (Exception e) {
        System.out.println("connect sql erro");
        e.printStackTrace();
    }



    /*检查用户账号密码正否
     * 
     */

}


/*连接数据库后要记得关闭
 * 
 */
private void CloseSqlConnect(Connection connection ){
    if (connection != null)
        try{
            connection.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
}

private ResultSet execSQL(String sql,Object... args) throws SQLException{

    //建立PreparedStatement对象

    PreparedStatement pStmt = connection.prepareStatement(sql);

    //为pStmt对象设置SQL参数值

    for(int i = 0; i < args.length; i++){

        pStmt.setObject(i+1, args[i]);

    }

    //执行SQL语句

    pStmt.execute();

    //返回结果集,如果执行的SQL语句不返回结果集,则返回null

    return pStmt.getResultSet();

}

public String checkUser(String username,String password){

    boolean has_username = false;

    boolean password_correct = false;

    ResultSet rs = null;

    try {

        rs = execSQL("select * from user");

    } catch (SQLException e) {

        System.err.println("查询数据库出错");

        e.printStackTrace();

        return null;

    }

    try {

        while(rs.next()){

            String temp_username = rs.getString("name").trim();

            String temp_password = rs.getString("password").trim();

            if(username.equals(temp_username)){

                has_username = true;

                if(password.equals(temp_password)){

                    password_correct = true;

                    return "hasUserNameAndPasswordCorrect";

                }

                return "hasUserNameButPasswordInCorrect";

            }

        }

    } catch (SQLException e) {

        System.err.println("操作ResultSet出错");

        e.printStackTrace();

    }

    return "hasNoUserName";

}
}

新建LoginServlet来响应浏览器请求

import java.io.IOException;
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.http.HttpSession;
import java.io.OutputStream;
import java.io.PrintWriter;


public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();

    }


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

        response.setContentType("text/html");

         String username = request.getParameter("username");

         String password = request.getParameter("password");

         PrintWriter  output = response.getWriter();

         output.println(" hello");


         MySQL mss = new MySQL();      //实例化数据库对象

        String result = mss.checkUser(username,password);

    if (result.equals("hasUserNameAndPasswordCorrect")) {

    output.println("user and password correct");
System.out.println("用户名和密码均正确");
} else if (result.equals("hasUserNameButPasswordInCorrect")) {
output.println("user correct but password wrong");
System.out.println("用户名正确,密码不正确");
} else if (result.equals("hasNoUserName")) {
output.println("no user");
System.out.println("没有此用户");
}
output.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

测试工程

  今天在测试中遇到一个问题,浪费很多时间,在测试的过程中,发现MySQL在实例化的过程中出错,原因是找不到 com.mysql.jdbc.Driver ,但是我确实是导入到工程中的。
 最后发现原因:tomcat在加载JDBC驱动时,Class.forName("com.mysql.jdbc.Driver");不会在项目的lib目录下寻找,而是在Tomcat—>lib目录下寻找,解决方法就是直接拷贝mysql-connector-java-5.0.8.jar到该目录下就可以
 最后附上测试结果

Eclipse新建web项目,实现登录验证功能_第3张图片

你可能感兴趣的:(网页开发,教程)