其他分享
首页 > 其他分享> > 2021-11-04

2021-11-04

作者:互联网

用servlet连接数据库实现登陆验证

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
    <table>
      账号:<input type="text" name="sname" value="张三">
      密码:<input type="password" name="spwd" value="123">
      <input type="submit" value="登录">
    </table>
  </form>
</body>
</html>

 


public interface Dao {

    Student login(String sname, String spwd);

}
 

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


public class DaoImpl implements Dao{

    @Override
    public Student login(String sname, String spwd) {
        Student student = null;
        Util u = new Util();
        Connection conn = u.getConn();
        try {
            PreparedStatement ps = conn.prepareStatement("select * from student where sname=? and spwd=?");
            ps.setString(1,sname);
            ps.setString(2, spwd);
            ResultSet rs =ps.executeQuery();
            while(rs.next()) {
                int sid = rs.getInt("sid");
                int cid = rs.getInt("cid");
                student =  new Student(sid, sname, spwd, cid);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return student;
    }

}
 

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;

/**
 * Servlet implementation class Servlet
 */
@WebServlet("/login")
public class Servlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    
        request.setCharacterEncoding("utf-8");
        String sname = request.getParameter("sname");
        String spwd = request.getParameter("spwd");
        System.out.println(sname+""+spwd);
        
        Dao d = new DaoImpl();
        Student s = d.login(sname,spwd);
        if (s!=null) {
            System.out.println("登陆成功");
        }else {
            System.out.println("登陆失败");
        }
    }

}
 

public class Student {
    private int sid;
    private String sname;
    private String spwd;
    private int cid;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSpwd() {
        return spwd;
    }

    public void setSpwd(String spwd) {
        this.spwd = spwd;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public Student(int sid, String sname, String spwd, int cid) {
        this.sid = sid;
        this.sname = sname;
        this.spwd = spwd;
        this.cid = cid;
    }
}
 

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

public class Util {
    private static String URL = "jdbc:mysql://localhost:3306/mydb";
    private static String NAME = "root";
    private static String PWD = "123456";

    private static Connection conn;

    public static Connection getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL,NAME,PWD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        System.out.println(getConn());
//    }

}
 

 

标签:11,sname,spwd,String,04,cid,2021,import,public
来源: https://blog.csdn.net/Liuss0217/article/details/121154262