使用servlet实现登录

简单的登录页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
    <!-- web3:项目名,在LoginServlet前必须加项目名,如果不加则默认视为工作空间地址 -->
    <form action="/web3/LoginServlet" method="post">
        username:<input type="text" name="username" /> 
        password:<input type="password" name="password" />
        <input type="submit" value="login"> 
    </form>
</body>
</html>

 

登录成功页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
    登录成功!!
</body>
</html>
View Code

 

登录失败页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
    登录成功!!
</body>
</html>
View Code

 

Servlet处理代码:

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    // 使用doPost()可以隐藏向服务器发送请求的内容
    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ServletContext application = getServletContext();
        // 设置服务器返回页面的编码,如果不设置默认会使中文乱码
        response.setContentType("text/html;Charset=utf-8");
        // 获取<input type="?" name="username">中的username的值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 如果请求中username为空,把username改为空字符串,避免指针为空的异常
        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }
        PrintWriter out = response.getWriter();
        out.println(username + ":" + password);
        User user = new User(username, password);
        // mvc结构
        UserDao userDao = new UserDao();
        // 判断是否有值返回
        boolean b = userDao.getUserByNameAndPassword(user);
        if (b) {
            // 跳转到success.html页面
            RequestDispatcher requestDispatcher = application
                    .getRequestDispatcher("/success.html");
            requestDispatcher.forward(request, response);
        } else {
            RequestDispatcher requestDispatcher = application
                    .getRequestDispatcher("/fail.html");
            //将fail.html包含在当前页面内部
            requestDispatcher.include(request, response);
        }
    }
}

 

Servlet配置:

    <!-- login -->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.gem.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
View Code

 

连接数据库:

public class DBUtils {
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/school";
            String user = "root";
            String password = "rootpassword";
            return DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
View Code

 

建表语句:

CREATE TABLE user_test(
id int PRIMARY KEY auto_increment,
username VARCHAR(12) not null,
userpassword VARCHAR(12) not null
)

INSERT INTO user_test(username,userpassword) VALUES('tom','123456');

SELECT username,userpassword from user_test WHERE username='tom' and userpassword='123456'
View Code

 

UserDao与数据库交互:

public class UserDao {
    public boolean getUserByNameAndPassword(User user) {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String sql = "SELECT * from user_test WHERE username=? and userpassword=?";
        try {
         preparedStatement = connection.prepareStatement(sql);
         preparedStatement.setString(1, user.getName());
         preparedStatement.setString(2, user.getPassword());
         resultSet=preparedStatement.executeQuery();
         if (resultSet.next()) {
            return true;
        }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if (resultSet!=null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (preparedStatement!=null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection!=null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}
View Code

 

User实体类:

package com.gem.pojo;

public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        super();
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
View Code

 

posted @ 2015-09-01 19:23  pepelu  阅读(556)  评论(0编辑  收藏  举报