用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;

/**
 * Servlet implementation class Login
 */
@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";

    /**
     * Default constructor. 
     */
    public Login() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		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/1F99cVbUDfTLePu2aJDuCxA
提取码:l5i1

 

转载于:https://www.cnblogs.com/lzc396732672/p/10622213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值