zoukankan      html  css  js  c++  java
  • JSP+JDBC+Servlet--基于数据库的登陆验证

    采用JSP+Servle+JDBCt技术实现用户的登录验证,其中实现数据库的连接及其查询操作在servlet中实现。

    tijiao.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>提交页面</title>
    </head>
    <body>
    <form action="Login/DBcheck" method="post">
        登录名:<input type="text" name="username"/>
        密 码:<input type="password" name="passWd"/><br>
    
        <input type="submit" value="登录"/>
    </form>
    </body>
    </html>
    

    DBcheck.java:

    package Login;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.sql.*;
    
    public class DBcheck extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            String name = req.getParameter("username");
            String passwd = req.getParameter("passWd");
            RequestDispatcher dis =null;
            System.out.println(name+passwd);
            try {
                try {
                    String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    Connection comm = DriverManager.getConnection(url);
                    String SQL = "SELECT * FROM [user] where(username=? and password =?)";//注意:表名一定要带[]
                    //结果集
                    PreparedStatement pstmt = comm.prepareStatement(SQL);
                    pstmt.setString(1,name);
                    pstmt.setString(2,passwd);
                    ResultSet rs = pstmt.executeQuery();
                    if(rs.next()){
                        if(rs!=null){
                            rs.close();
                        }
                        if(pstmt!=null){
                            pstmt.close();
                        }
                        if(comm!=null){
                            comm.close();
                        }
    //                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
    //                    dis.forward(req,resp);
                        resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
                        System.out.println("hah");
                    }else{
                        if(rs!=null){
                            rs.close();
                        }
                        if(pstmt!=null){
                            pstmt.close();
                        }
                        if(comm!=null){
                            comm.close();
                        }
    //                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");
    //                    dis.forward(req,resp);
                        resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/error.jsp");
                        System.out.println("ggggggggg");
                    }
    
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    数据库:(user表)

    登陆成功页面:

    登陆失败页面: 

  • 相关阅读:
    ansible plugins 列表
    ansible common modules
    CentOS 7.3降低内核版本为7.2
    ansible ad-hoc 参考
    kafka监控工具kafka-manager
    zookeeper监控之taokeeper
    linux的ulimit各种限制之深入分析
    docker版的zabbix部署
    kubernetes介绍(1)
    部署k8s时容器中ping不通
  • 原文地址:https://www.cnblogs.com/zhahu/p/11931377.html
Copyright © 2011-2022 走看看