javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册用户登录注册流程图老版:新版:登陆界面注册界面登陆成功界面LoginServletimportjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servle……

大家好,欢迎来到IT知识分享网。

用户登录注册流程图

老版:

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

新版:

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

登陆界面

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

注册界面

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

登陆成功界面

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

LoginServlet

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.wzc.login.dao.UserDao;
import com.wzc.login.domain.User;

/**
 * @description 登录请求处理类
 * @author WANGZIC
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//接收表单信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String verifyc  = request.getParameter("verifycode");
		//设置回显
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		request.setAttribute("verifycode", verifyc);
		//获取验证码
		String svc =(String) request.getSession().getAttribute("sessionverify");
		//根据用户名查询用户
		User user =new UserDao().findUser(username);
		if(!svc.equalsIgnoreCase(verifyc)){
			request.setAttribute("loginError", "* 验证码错误");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
		if(user!=null){
			if(user.getPassword().equals(password)){
				request.getSession().setAttribute("user", user);
				response.sendRedirect("index.jsp");
			}else {
				request.setAttribute("loginError", "* 密码错误");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		}else {
			request.setAttribute("loginError", "* 用户不存在");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

}

RegistServlet

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.wzc.login.dao.UserDao;

/**
 * @description 注册请求处理类
 * @author WANGZIC
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String rpsw = request.getParameter("rpsw");
		if(username==null||username.trim().isEmpty()){
			request.setAttribute("registError", "用户名不能为空");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
			return;
		}
		if(password==null||password.trim().isEmpty()){
			request.setAttribute("registError", "密码不能为空");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
			return;
		}
		if(!password.equals(rpsw)){
			request.setAttribute("registError", "密码不一致");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
			return;
		}
		UserDao u = new UserDao();
		boolean res = u.addUser(username,password);
		if(res){
			response.sendRedirect("index.jsp");
		}else {
			request.setAttribute("registError", "注册失败,该用户名已存在");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
}

LogoutServlet

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 implementation class LogoutServlet
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LogoutServlet() {
        super();
    }
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("user");
		response.sendRedirect("login.jsp");
	}

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

}

VerifyCodeServlet

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description 验证码请求生成处理类
 * @author WANGZIC
 */
@WebServlet("/VerifyCodeServlet")
public class VerifyCodeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		VerifyCode vc = new VerifyCode();
		request.getSession().setAttribute("sessionverify", vc.getText());
		vc.outputImage(90,35,response.getOutputStream());
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	/**
	 * 验证码生成工具内部类
	 * @author WANGZIC
	 *
	 */
	static class VerifyCode {

		private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8','9',
				'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
				'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

		private static Random random = new Random();

		private String text ;
		
		public String getText() {
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < 4; i++) {
				buffer.append(CHARS[random.nextInt(CHARS.length)]);
			}
			text = buffer.toString();
			return text;
		}

		public void outputImage(int width,int height,OutputStream out) throws IOException{
			Color color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
			Color reverse = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
			BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bi.createGraphics();
			g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
			g.setColor(color);
			g.fillRect(0, 0, width, height);
			g.setColor(reverse);
			g.drawString(text, 10, 26);
			for (int i = 0, n = random.nextInt(80); i < n; i++) {
				g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
			}
			ImageIO.write(bi, "JPEG", out);
		}
	}
}

User

public class User {
	
	private String username;
	
	private String password;

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

UserDao

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wzc.login.domain.User;
/**
 * @description 数据库连接与操作类用于增删改查数据并返回给servlet使用
 * @author WANGZIC
 *
 */
public class UserDao {
	/*
	 * 数据库中运行下面的语句
	  	CREATE TABLE `user` (
		  `username` varchar(255) NOT NULL,
		  `password` varchar(255) DEFAULT NULL,
		  PRIMARY KEY (`username`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;
	*/
	
	//根据用户名查找用户密码
	public User findUser(String username){
		String sql = "select * from user where username=?";
		Connection con =getConnection();
		PreparedStatement pstmt =null;
		ResultSet rs = null;
		User user = new User();
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, username);
			rs = pstmt.executeQuery();
			if(rs.next()){
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null)pstmt.close();
				if(con!=null)con.close();
			} catch (SQLException e) {		
				e.printStackTrace();
			}
		}
		return user;
	}
	//添加用户
	public boolean addUser(String username,String psw){
		Connection con = getConnection();
		PreparedStatement pstmt =null;
		String sql = "INSERT INTO user(username,password) VALUES(?,?)";
		boolean res = false;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, psw);
			res = (pstmt.executeUpdate()==1);
		}catch (SQLException e) {
			if(!e.getMessage().contains("PRIMARY")){
				e.printStackTrace();
			}
		}finally {
			try {
				if(pstmt!=null)pstmt.close();
				if(con!=null)con.close();
			}catch (SQLException e) {	
				e.printStackTrace();
			}
		}
		return res;
	}
	//获得连接
	public static Connection getConnection(){
		String driver ="com.mysql.jdbc.Driver";//记得依赖mysql-jdbc驱动包
		String url ="jdbc:mysql://localhost:3306/mytest";//修改为自己的数据库
		String user ="root";//修改未自己数据库的用户名密码
		String password ="root";//修改未自己数据库的名密码
		Connection connection =null;
		try {
			Class.forName(driver);
			connection =DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	public static void main(String[] args) {
		//测试方法
//		System.out.println(new UserDao().findUser("123"));
//		new UserDao().addUser("1345", "1345");
	}


	
}

新增了用户管理功能(对用户的增删改查)
源码仓库地址https://gitee.com/wangzic/login源码下载步骤如下图:

javaweb登录注册功能实现 javaweb 登陆注册 入门 mysql数据库交互 web前后台交互 用户管理增删改查 实现登录 注册 登陆 JavaWeb 简单登陆注册

下载完源码之后请仔细阅读README.md文件,导入运行步骤和数据库表结构都在里面

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10542.html

(0)
上一篇 2024-03-31 08:00
下一篇 2024-03-31 15:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信