一只倔强的笨猪

【JAVAEE】实现账号登陆功能

1.先编写jsp页面,一个登陆界面,一个登陆成功页面,一个登陆失败页面。
byname/login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆页面</title>
</head>
<body>
<form action="../userCheckServlet" method="post">
       请输入用户名:<input type="text" name="username"/><br/>
       请输入密码:<input type="password" name="userpwd"/><br/>
       <input type="submit" value="登录"/>
       <input type="reset"/>
   </form>
</body>
</html>

byname/success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆成功页面</title>
</head>
<body>
${requestScope.u }你好!登陆成功!
</body>
</html>

byname/error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆失败页面</title>
</head>
<body>
${requestScope.u },用户不存在或者密码错误。
<a href="byname/login.jsp">单击转跳登陆页面</a>
</body>
</html>

2.创建用户和连接数据库的Bean类。
com/bean/UserDao.java

package com.bean;

public class User {
	private int id;
	private String name;
	private String pwd;
	private int power;
	public User() {}
	public User(String name,String pwd) {
		this.pwd=pwd;
		this.name=name;
	}
	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;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}	
}

com/bean/DbConnect.java

package com.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbConnect {
	public static Connection getDBconnection() {
		String shujuku = "benzhu";
		String url1 ="jdbc:mysql://localhost:3306/"+shujuku;
		String uname = "root";
		String upwd="123456";
		Connection conn=null;
		try{			
			Class.forName("com.mysql.jdbc.Driver");
			conn= DriverManager.getConnection(url1, uname, upwd);
			System.out.println("哈哈哈哈 连接成功");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	public static void Close(Connection conn,Statement stmt,ResultSet rs) {
		try{if(rs!=null) rs.close();}catch(Exception e){e.printStackTrace();}
		try{if(stmt!=null) stmt.close();}catch(Exception e){e.printStackTrace();}
		try{if(conn!=null) conn.close();}catch(Exception e){e.printStackTrace();}
	}
}

3.创建验证账号密码的Dao类。
com/dao/UserDao.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.bean.DbConnect;
import com.bean.User;

public class UserDao {
public User findUserByuser(User user) {
		User user1=null;
		try{
			Connection conn;
			conn = DbConnect.getDBconnection();
			String sql="select * from benzhu_user where user_name=? and user_pwd=?";
			PreparedStatement st1 = conn.prepareStatement(sql);
			st1.setString(1, user.getName());
			st1.setString(2, user.getPwd());
			ResultSet rs=st1.executeQuery();
			if(rs.next()) {
				int id = rs.getInt(1);
				String name1 = rs.getString(2);
				String pwd = rs.getString(3);
				int power = rs.getInt(4);
				user1 = new User(id,name1,pwd,power);			
			}
			
			DbConnect.Close(conn, st1, rs);
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return user1;
	
	}
}

4.创建处理jsp页面的servlet。
com/servlet/userCheckServlet

package com.servlet;

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;

import com.bean.User;
import com.dao.UserDao;

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

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String pwd = request.getParameter("userpwd");
		User user = new User(name,pwd);
		UserDao dao1 = new UserDao();
		request.setAttribute("u", name);
		if(dao1.findUserByuser(user)!=null) {
			request.getRequestDispatcher("byname/success.jsp").forward(request, response);
		}
		else {
			
			request.getRequestDispatcher("byname/error.jsp").forward(request, response);
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

数据库文件下载: 本地下载

效果图:
《【JAVAEE】实现账号登陆功能》

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注