码迷,mamicode.com
首页 > 其他好文 > 详细

用servlet校验密码2

时间:2019-03-30 12:00:31      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:gen   bre   iter   数据   try   sqlserver   rsn   get   ati   

 用servlet校验密码2

servlet校验代码

package Login; 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static final String  driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
    static final String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=TextDB";
    static final String  userName="sa";
    static final String userPwd="123";
 
    public Login() {
        // TODO Auto-generated constructor stub
    }
 
  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
     
        Connection conn=null;
        Statement stmt = null;
        response.setContentType("text/html;charset=utf-8");
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//驱动
            PrintWriter out =response.getWriter();
            conn=DriverManager.getConnection(dbURL,userName,userPwd);//连接数据库
            stmt =conn.createStatement();
             
            String un=request.getParameter("UersName");
            String upwd = request.getParameter("PassWrd");
            //out.println(upwd+"/n");
            String sql = "Select pwd from Uers_Table where uname =‘"+un+"‘";
            ResultSet rs = stmt.executeQuery(sql);//执行sql语句
            boolean isTrue = false;
        //  boolean isLoginIn = false;
            while(rs.next()){//遍历整个数据库
                String pswd = rs.getString("pwd");//取出数据库数据
                //out.println(pswd.replace(" ", ""));
                if(pswd.replace(" ", "").equals(upwd))
                {
                    isTrue=true;
                    out.println("登陆成功");
                    break;
                }
                else
                {
                    isTrue= true;
                    out.println("密码错误,登陆失败");
                }
             
            }
            if(isTrue==false)
            {
                out.println("用户名不存在");
            }
         
            rs.close();
            stmt.close();
            conn.close();
             
     
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    finally
        {
            try {
                if(stmt!=null)
                {
                    stmt.close();
                     
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            try {
                if(conn!=null)
                {
                    conn.close();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
             
                 
        }
     
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
    public void  destroy()
    {
         
    }
 
}

运行结果:

技术图片

 用户名错误:

技术图片

 密码错误:

技术图片

 

 

链接:https://pan.baidu.com/s/1qdRwyU7Xgr42Hh-dVwkJWw
提取码:pgqj
复制这段内容后打开百度网盘手机App,操作更方便哦

 

用servlet校验密码2

标签:gen   bre   iter   数据   try   sqlserver   rsn   get   ati   

原文地址:https://www.cnblogs.com/fengjianbk/p/10626367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!