登录

controller:

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		response.setContentType("text/html;charset=utf-8");
		//设置请求编码
		request.setCharacterEncoding("utf-8");
		//获取用户名和密码  
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		LoginService ls = new LoginServiceImpl();
		User u = ls.checkLoginService(userName, password);
		System.out.println(u);
		//响应处理结果
		if (u != null) {
			response.getWriter().write("登录成功,欢迎您:" + u.getU_name());
		}else{
			response.getWriter().write("登录失败");
		}
		
	}
}

service层:

public interface LoginService {
	//判断用户是否输入正确的信息
	User checkLoginService(String userName,String password);
}

ServiceImpl层:

public class LoginServiceImpl implements LoginService {
	// 创建Dao层对象
	LoginDao ld = new LoginDaoImpl();
	
	public User checkLoginService(String userName, String password) {
		// TODO Auto-generated method stub
		return ld.checkLoginDao(userName,password);
	}
	
}

Dao层:

public interface LoginDao {
	//判断用户输入信息是否正确
	User checkLoginDao(String userName, String password);
}

DaoImpl层:

public class LoginDaoImpl implements LoginDao{
	@Override
	public User checkLoginDao(String userName, String password) {
		
		ResultSet rs = null;
		PreparedStatement stat = null;
		Connection con = null;
		User u = null;
		
		try {
			//1.注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获得连接
			String  url = "jdbc:mysql://localhost:3306/course";
			String user = "root";
			String password1 = "123456";
			con = DriverManager.getConnection(url,user,password1);
			String sql = "select * from WEB_USER WHERE U_NAME = '" + userName + "' and U_PWD = '"+password+"'";
			//String sql = "SELECT * FROM USER_LOGIN WHERE U_NAME = ? AND U_PWD = ?";
			// 创建sql命令对象
			stat = con.prepareStatement(sql);
		
			// 给占位符赋值
			//stat.setString(1,userName);
			//stat.setString(2,password);
		
			// 执行
			rs = stat.executeQuery(sql);
			//6.处理结果集resulrset boolean next 返会true,有结果集,返回false则没有
			while(rs.next()){
				u = new User();
				u.setU_id(rs.getInt("U_ID"));
				u.setU_name(rs.getString("U_NAME"));
				u.setu_pwd(rs.getString("U_PWD"));
				u.setu_email(rs.getString("U_EMAIL"));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(stat != null){
				try {
					stat.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		System.out.println("---");
		return u;
	}

}

数据库建表的sql语句手欠删掉了
实体类对象如下:

public class User {
	private int u_id;
	private String u_name;
	private String u_pwd;
	private String u_email;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(int u_id, String u_name, String u_pwd, String u_email) {
		super();
		this.u_id = u_id;
		this.u_name = u_name;
		this.u_pwd = u_pwd;
		this.u_email = u_email;
	}
	public int getU_id() {
		return u_id;
	}
	public void setU_id(int u_id) {
		this.u_id = u_id;
	}
	public String getU_name() {
		return u_name;
	}
	public void setU_name(String u_name) {
		this.u_name = u_name;
	}
	public String getu_pwd() {
		return u_pwd;
	}
	public void setu_pwd(String u_pwd) {
		this.u_pwd = u_pwd;
	}
	public String getu_email() {
		return u_email;
	}
	public void setu_email(String u_email) {
		this.u_email = u_email;
	}
	@Override
	public String toString() {
		return "User [u_id=" + u_id + ", u_name=" + u_name + ", u_pwd=" + u_pwd + ", u_email=" + u_email + "]";
	}
	
}

前端的登录界面:

<html>
<head>
	<meta charset="UTF-8">
	<title>登录</title>
	<style type="text/css">
	*{
	margin:0px;
	padding:0px;
	font-size:16px;
	}
	li{
		list-style-type:none;
	}	
	.loginModel{
		width:500px;
		margin:50px auto;
		padding:20px;
		border:1px solid green;
		border-radius:10px;
	}
	h2{
		text-align:center;
	}
	.loginModel li {
		margin:40px 0px;
	}
	.loginModel li h2{
		font-size:28px;
	}
	.loginModel li input{
		width:300px;
		height:40px;
		line-height:40px;
		vertical-align:middle;
	}
	.loginModel li .inputinfo{
		width:100px;
		display:inline-block;
	}
	.loginModel .submitline{
		text-align:center;
	}
	.promptinfo{
		color:red;
	}
	</style>
	<script type="text/javascript" src="./jquery-3.4.1.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#userName").keyup(function(){
				checkLogin();
			});
			//判断登录信息是否符合格式
			function checkLogin(){
				console.log("hduiid")
				if(!/\s{2,}/.test($("#userName").val())){
					$(".promptinfo").text("请输入两位数以上的用户名");
				}
			}
		});
	</script>
	</head>
	<body>
	<!-- 登陆模块 -->
		<form action = "/Land/LoginServlet" method ="post">
			<ul class = "loginModel">
				<li>
					<h2>欢迎登陆</h2>
				</li>
				<li>
					<span class ="inputinfo">用户名:</span>
					<input type="text" name="userName" id="userName" placeholder="请输入用户名">
				</li>
				<li>
				<span class = "inputinfo">密码:</span>
				<input type = "password" name = "password" id = "password" placeholder ="请输入密码">
			</li>
			<li class = "promptinfo">
				
			</li>
			<li class="submitline">
				<input type="submit" value="登录">
			</li>

		</ul>
	</form>

	
</body>
</html>

发布了58 篇原创文章 · 获赞 43 · 访问量 3036
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章