MVC实现登录注册

登录具体思路

客户端通过界面层发送登录请求到控制层,控制层接收到请求并转发给模型层(servlet实现,数据以javabean实体类传输),模型层处理请求实现登录功能(javabean实现)

实现

login.jsp发出登录请求到LoginServlet.java,再此,将表单数据封装在实体类内,随着请求转发到模型层LoginDao.java作具体的功能(数据库查操作)

1、login.jsp表单

	<form action="LoginServlet" method="post">
			<input  type="text" name="name" ><br>
			<input  type="password" name="pwd" ><br>
			<input  type="submit" value="登录">
		</form>

2、实体类Login.java

	public class Login {
	private int id;
	private String name;
	private String pwd;
	
	public Login() {
		
	}
	
	public Login( String name, String pwd) {
		this.name = name;
		this.pwd = pwd;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
}

3、LoginServlet.java

	import java.io.IOException;
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:接受view请求,并分发给model处理
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理登录
		request.setCharacterEncoding("utf-8");//统一编码
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		
		Login login=new Login(name,pwd);//将name,pwd封装成javabean
		
		//调用模型层的 登陆功能
		int result=LoginDao.login(login);
		
		if(result>0) {
			response.sendRedirect("welcome.jsp");
		}else {
			response.sendRedirect("login.jsp");
			System.out.println("失败");
		}
	  	
	}

	/**
	 * @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);
	}

}

4、LoginDao.java

	import com.entiy.Login;
import java.sql.*;

//model模型层,处理登录
public class LoginDao {
	public static int login(Login login){//登录
		
		int flag=-1;//-1:系统异常 0:账号或密码错误 1:登陆成功
		int result=-1;
		String sql;
		Connection  conn = null;
		PreparedStatement  pstmt = null;
		ResultSet rs = null;//建立结果集对象
		
		// URL指向要访问的数据库名blog

		String url = "jdbc:mysql://127.0.0.1/blog?serverTimezone=UTC";//不加serverTime时区就报错

		// MySQL配置时的用户名

		String user = "root";

		// Java连接MySQL配置时的密码

		String password = "";

		try {

			// 1 加载驱动程序

			Class.forName("com.mysql.jdbc.Driver");

			// 2 连接数据库

			conn = DriverManager.getConnection(url, user, password);

			// 要执行的SQL语句
			sql = "select count(*) from user where name=? and pwd=?";
			
			pstmt = conn.prepareStatement(sql);//防止sql注入攻击
			pstmt.setString(1,login.getName());
			pstmt.setString(2,login.getPwd());
			
			rs = pstmt.executeQuery();//返回值表示增删改几条数据
			if(rs.next()) {
				result=rs.getInt(1);
			}
			if(result>0) {
				flag=1;
			}else {
				flag=0;//登陆失败,用户名或密码有误
			}
			
			
			rs.close();
			pstmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			//System.out.print("Sorry,can`t find the Driver!");
			//e.printStackTrace();
			flag=-1;//系统异常
			
			} catch (SQLException e) {
				//e.printStackTrace();
				flag=-1;//系统异常
				} catch (Exception e) {
					//e.printStackTrace();
					flag=-1;//系统异常
					}finally {
						try {//关闭
							if(rs!=null)rs.close();
							if(pstmt!=null)pstmt.close();
							if(conn!=null)conn.close();
						}catch (SQLException e) {
							//e.printStackTrace();
							flag=-1;//系统异常
						}catch (Exception e) {
							//e.printStackTrace();
							flag=-1;//系统异常
						}
					
					}
		
		return flag;
		
	}

}

完善中……..

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦