jsp简单的登录实现

jsp简单的登录实现

首先要把index界面写出来
index界面
其次 我们要在web.xml 里面进行配置
loginServletwm.web.LoginServletloginServlet/login
编写Model
package wm.model;
model层
public class User {

private int id;
private String userName;
private String password;

public User() {
	super();
	// TODO Auto-generated constructor stub
}

public User(String userName, String password) {
	super();
	this.userName = userName;
	this.password = password;

}

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

}
编写 Dao 和servlet
Dao:
public class UserDao {
/**
* 登录验证
* @param con
* @param user
* @return
* @throws Exception
/
public User login(Connection con,User user) throws Exception{
User resultUser=null;
String sql=“select * from s_user where userName=? and password=?”;
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setUserName(rs.getString(“userName”));
resultUser.setPassword(rs.getString(“password”));
}
return resultUser;
}
}
Servlet:
public class LoginServlet extends HttpServlet{
DbUtil dbUtil=new DbUtil();
UserDao userDao=new UserDao();
/get提交放在在url里面长度有限,重要数据会显示/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/
post是form提交 放数据包里比较安全*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter(“userName”);
String password=request.getParameter(“password”);
request.setAttribute(“userName”, userName);
if(StringUtil.isEmpty(userName )||StringUtil.isEmpty(password)) {
request.setAttribute(“error”, “用户名或密码为空”);
request.getRequestDispatcher(“index.jsp”).forward(request, response);
return;
}
User user=new User(userName,password);
Connection con=null;
try {
con=dbUtil.getCon();
User currentUser=userDao.login(con, user);
if(currentUser==null) {
request.setAttribute(“error”, “用户名或密码错误”);
request.getRequestDispatcher(“index.jsp”).forward(request, response);
}
else {
//权限验证session
HttpSession session=request.getSession();
session.setAttribute(“currentUser”, currentUser);
response.sendRedirect(“teacher.jsp”);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
dbUtil.closeCon(con);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}

这里面用到了StringUtil 以及DbUtil工具 分享给大家

public class DbUtil {

private String dbUrl="jdbc:mysql://localhost:3306/bishe";
private String dbUserName="root";
private String dbPassword="123";
private String jdbcName="com.mysql.jdbc.Driver";

/**
 * 获取数据库连接
 * @return
 * @throws Exception
 */
public Connection getCon() throws Exception{
	Class.forName(jdbcName);
	Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
	return con;
}

/**
 * 关闭数据库连接
 * @param con
 * @throws Exception
 */
public void closeCon(Connection con) throws Exception{
	if(con!=null){
		con.close();
	}
}

public static void main(String[] args) {
	DbUtil dbUtil=new DbUtil();
	try {
		dbUtil.getCon();
		System.out.println("数据库连接成功");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}
public class StringUtil {

public static boolean isEmpty(String str){
	if("".equals(str)|| str==null){
		return true;
	}else{
		return false;
	}
}

public static boolean isNotEmpty(String str){
	if(!"".equals(str)&&str!=null){
		return true;
	}else{
		return false;
	}
}

}
好的 收工!!!

猜你喜欢

转载自blog.csdn.net/qq_41357948/article/details/89365627