javaWeb连接mysql 简单登录页面

  今天上午学了javaWeb中的servlet部分,结合前面所学的jdbc的知识可以写一个简单的登录界面。

  用dao模式结合servlet做一个登陆测试,并给出相应的页面

  采用了Dao模式,这个框架有3个包:

  Util:接口,jdbc连接

  Dao:继承Util中接口的类

  servlet:写的登录界面,调用了Dao中的类,实现连接mysql的功能

  (Test是我为了测试数据库建的,请忽略)

  在这里插入图片描述

  下面是代码部分:

  Util包,有接口和jdbc连接:

  接口:

   package Util;

    public interface UserInte {
        public String Select(String username);
    }

  jdbc连接:

   package Util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class DBConnection{
        
        String url = "jdbc:mysql://localhost:3306/user";
        String user = "root";
        String password = "418521";
        Connection conn = null;
        Statement stmt = null;
        
        public Connection getConn(){
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url,user,password);
                stmt = conn.createStatement();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return conn;        
        }    
    }

  Dao包,写了具有查询功能的一个类,继承用了Util包中的接口,调用了DBConnection类,用来连接数据库。

   package Dao;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import Util.DBConnection;
    import Util.UserInte;
    public class UserImpl implements UserInte{
        Connection conn = null;
        public UserImpl(){
            DBConnection db = new DBConnection();
            conn = db.getConn();
        }
        public String Select(String username) {
            String password = null;
            try (Statement stmt = conn.createStatement();){
                String sql = null;
                sql="select password from login where username = '"+username+"'";
                ResultSet re = stmt.executeQuery(sql);
                
                if(re.next()){
                    password = re.getString(1);
                }            
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return password;
        }
    }

  serclet包,重写了doPost等方法:

   package servlet;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import Dao.UserImpl;
    import Util.UserInte;
    public class Servlet1 extends HttpServlet {
        public void destroy() {
            super.destroy(); 
            System.out.println("servlet1 destroy");
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            String password2 = null;
            UserInte u1 = new UserImpl();
            password2 = u1.Select(username);
            if(password2 == null){
                response.sendRedirect("failure.jsp");
            }else if(password.equals(password2)){
                request.getRequestDispatcher("success.jsp").forward(request, response);
            }else{
                response.sendRedirect("failure.jsp");
            }
        }
        public void init() throws ServletException {
            // Put your code here
            System.out.println("servlet1 init");
        }
    }

  下面是写的jsp文件

  登录界面:

  

       
        My JSP 'MyJsp.jsp' starting page
        
      
      
        登陆页面
        
            用户名:
            密码:
            
        
        
      
    

  登录成功

  

      
      
        欢迎页面
        welcome 
        
      
    

  登录失败


  
  
    错误页面
    登陆失败
    
    
  

[1]: https://xiaokai521-1253648611.cos.ap-beijing.myqcloud.com/20240809/7cb2cd0b05eda91f79bac68e858b2469_0.png
[2]: https://www.xiaokai521.cn/759.html
最后修改:2024 年 08 月 09 日
如果觉得我的文章对你有用,请随意赞赏