首页 > 代码库 > jsp+mysql+servlet的登录

jsp+mysql+servlet的登录

登录代码

  •  entity类
package com.wy.entity;public class User {	private int id;	private String username;	private String password;	private String email;	public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	public String getUsername() {		return username;	}	public void setUsername(String username) {		this.username = username;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public String getEmail() {		return email;	}	public void setEmail(String email) {		this.email = email;	}	}
  •  userDao接口
package com.wy.dao;import com.wy.entity.User;public interface UserDao {    //保存用户信息	public void save(User user);	//登录接口	public User login(String username,String password);	//根据用户名查询用户方法接口	public boolean findByName(String username);}
  •  userDaoImpl(接口的实现)
package com.wy.daoImpl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.wy.dao.UserDao;import com.wy.db.DBUtils;import com.wy.entity.User;public class UserDaoImpl implements UserDao {	@Override	//保存	public void save(User user) {		// TODO Auto-generated method stub		Connection conn = null;		PreparedStatement ps = null;		try {		//连接数据库			conn = DBUtils.getConnection();			//SQL语句			ps = conn					.prepareStatement("insert into user(username,password,email)values(?,?,?)");			//给用户属性动态设置			ps.setString(1, user.getUsername());			ps.setString(2, user.getPassword());			ps.setString(6, user.getEmail());			ps.executeUpdate();		} catch (Exception e) {			// TODO: handle exception			e.printStackTrace();		}finally{		//关闭数据库连接			DBUtils.close1(ps, conn);		}	}	@Override	public User login(String username, String password) {		// TODO Auto-generated method stub		Connection conn=null;		PreparedStatement ps=null;		ResultSet rs=null;		try {			conn=DBUtils.getConnection();			ps=conn.prepareStatement("select * from user where username=? and password=?");			ps.setString(1, username);			ps.setString(2, password);			rs=ps.executeQuery();			if(rs.next()){				User user=new User();				user.setId(rs.getInt("id"));				user.setUsername(rs.getString("username"));				user.setPassword(rs.getString("password"));				user.setEmail(rs.getString("email"));			}		} catch (Exception e) {			// TODO: handle exception			e.printStackTrace();		}finally{			DBUtils.close2(rs, ps, conn);		}		return null;	}	@Override	public boolean findByName(String username) {		// TODO Auto-generated method stub		Connection conn=null;		PreparedStatement ps=null;		ResultSet rs=null;		try {			conn=DBUtils.getConnection();			ps=conn.prepareStatement("select * from user where name=?");			ps.setString(1, username);			rs=ps.executeQuery();			if(rs.next()){				user.setUsername(rs.getString("username"));				user.setPassword(rs.getString("password"));			}		} catch (Exception e) {			// TODO: handle exception			e.printStackTrace();		}finally{			DBUtils.close2(rs, ps, conn);		}		return false;	}}
  •  数据库的设置
package com.wy.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DBUtils {    //数据库的连接设置	static String url="jdbc:mysql://localhost:3306/java";	static String user="root";	static String password="wy123";	static{		try {		//加载数据库驱动			Class.forName("com.mysql.jdbc.Driver");		} catch (ClassNotFoundException e) {			// TODO: handle exception			e.printStackTrace();		}	}	/**	 * 建立连接	 * @throws SQLException 	 * */	public static Connection getConnection() throws SQLException{		Connection conn=DriverManager.getConnection(url, user, password);		return conn;	}	/**	 * 数据库关闭操作1:PreparedStatement ps,Connection conn	 * */	public static void close1(PreparedStatement ps,Connection conn){		if(ps!=null){			try {				ps.close();			} catch (SQLException e) {				// TODO: handle exception				e.printStackTrace();			}		}		if(conn!=null){			try {				conn.close();			} catch (SQLException e) {				// TODO: handle exception				e.printStackTrace();			}		}	}	/**	 * 数据库关闭操作2:ResultSet rs,PreparedStatement ps,Connection conn	 * */	public static void close2(ResultSet rs,PreparedStatement ps,Connection conn){		if(rs!=null){			try {				rs.close();			} catch (SQLException e) {				// TODO: handle exception				e.printStackTrace();			}		}		close1(ps, conn);	}}
  •  登录Servlet(LoginServlet)
package com.wy.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.wy.daoImpl.UserDaoImpl;import com.wy.entity.User;public class LoginServlet extends HttpServlet{    //doPost方法	@Override	protected void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		//获取jsp页面参数		String username=request.getParameter("username");		String password=request.getParameter("password");		//实例化userDaoImpl,该类中为接口的实现方法		UserDaoImpl userDaoImpl=new UserDaoImpl();		//根据得到的参数信息查询数据库中的用户名以及密码是否匹配		User user=userDaoImpl.login(username, password);		//匹配结果是否为空,即该用户是否存在		if(user!=null){			request.getSession().setAttribute("user", user);			request.getRequestDispatcher("message.jsp").forward(request ,response);		}else {			request.setAttribute("message", "用户名或密码错误!!!");			request.getRequestDispatcher("message.jsp").forward(request, response);		}	}}

这次登录的心得体会:在最开始的时候看了一些基础的Servlet视频进行的学习,对于它的页面跳转有一些了解,但是并具体的功能实现。敲代码我觉得是最快的了解语言的方式,这次的登录做完后比刚开始要更加了解Servlet,就如同在刚开始的时候我都不太理解如何区分executeUpdate与executeQuery,所以在去敲的时候有专门去查它们的区别然后再去使用就知道为什么去用它,要去研究它的用法,个人认为比看书要好一些。代码肯定还需要去不断的完善,新人也还在摸索更适合自己的学习道路,希望大家多多指教

jsp+mysql+servlet的登录