用servlet校验密码2

Posted fengjianbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用servlet校验密码2相关的知识,希望对你有一定的参考价值。

 用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的主要内容,如果未能解决你的问题,请参考以下文章

用servlet校验密码2

用servlet校验密码2

用Servlet进行用户名和密码校验

将 XSLT 转换的 XML 片段写入 XMLStreamWriter

XSLT:无法将操作数转换为“结果树片段”

用servlet进行用户名和密码校验