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

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

大家好,又见面了,我是你们的朋友全栈君。

用户登录注册流程图

老版:

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


	
}

新增了用户管理功能(对用户的增删改查)

有问题可以加QQ群:483411192
源码下载 下载完源码之后请仔细阅读README.md文件,导入运行步骤和数据库表结构都在里面

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136600.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 计算机修改用户名密码,怎么修改电脑用户名呢_电脑的登录名和密码在哪里

    计算机修改用户名密码,怎么修改电脑用户名呢_电脑的登录名和密码在哪里曲谱自学网今天精心准备的是《怎么修改电脑用户名》,下面是详解!如何改计算机用户名(administrator)如何改计算机用户名(administrator)…如何改计算机用户名(administrator)1、按“win+R”打开运行,在运行输入框里面输入“gpedit.msc”,如下图所示:2、进去本地组策略编辑器之后,点击“计算机配置-windows设置-安全设置-本地策略-安全选项”,…

    2022年10月14日
  • BP神经网络预测(人口)程序(matlab)

    BP神经网络预测(人口)程序(matlab)自己测试人口预测的matlab实现:x=[54167  55196  56300  57482  58796  60266  61465  62828  64653  65994  67207  66207  65859  67295  69172  70499  72538  74542…

  • Mybatis中JdbcType的使用[通俗易懂]

    Mybatis中JdbcType的使用[通俗易懂]MyBatis插入空值时,需要指定JdbcTypemybatisinsert空值报空值异常,但是在PL/SQL不会提示错误,主要原因是mybatis无法进行转换jdbcType的使用场合,只有当在insert,update和delete中有空字段时,需要使用jdbcType。MyBatis包含的jdbcType类型BIT、FLOAT、CHAR、TIMESTAMP、OTHE…

    2022年10月20日
  • Microsoft Office 2007 中文专业版密钥

    Microsoft Office 2007 中文专业版密钥MicrosoftOffice2007中文专业版(微软原版)正版密钥MicrosoftOfficeVisio2007简体中文专业版:简介:    便于IT和商务专业人员就复杂信息、系统和流程进行可视化处理、分析和交流。使用具有专业外观的OfficeVisio2007图表,可以促进对系统和流程的了解,深入了解复杂信息并利用这些知识做出更好的业务决策。迅雷下载    …

  • .net pdf转word_pdf to word

    .net pdf转word_pdf to wordAsposewordpdf相互转换工具类文件的详细路径:pdfToDoc(StringpdfPath,StringdocPath)输入流:pdfToDoc(InputStreampdfPathInputStream,StringdocPath)//Anhighlightedblockpackagecom.example.wordpdf.utils;importcom.aspose.pdf.License;importcom.aspose.pdf.SaveF

    2022年10月26日
  • WinHttp应用demo

    WinHttp应用demo#include#include#include#pragmacomment(lib,”winhttp”)structcallback_param_t{HINTERNEThInet;DWORDdwErrCert;};staticVOIDCALLBACKSyncCallback(HINTERNET,DWORD_PTR,DWORD,

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号