JavaEE利用I/O流实现简单的登录注册

JavaEE利用I/O流实现简单登录注册

一、题目要求

实现一个用户注册/登录模块,大致功能如下:
① 启动首页为登录页
② 如果用户还没有用户名和密码,则请他/她先进行注册,注册信息写入一个文本文件中,注册成功后直接回到登录页
③ 登录成功后则提示XXX登录成功
④ 登录不成功则提示究竟是用户名不存在还是密码错误

二、具体代码实现

register.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>注册页面</title>
        <meta charset="UTF-8">
    </head>
    <body bgcolor="#FFFFFF" >
        <h1 align="center"><b>注册</b></h1>
        <form action="RegisterServlet" method="post">
            <table border="1" style="margin:0 auto;width: 25%;height: 25%">
                <tr style="background-color:#FFFFCC;height:30px">
                    <td width="100px"><div align="center">用户名</div></td>
                    <td><div align="left"><input type="text" name="username" style="width:98.5%;height:30px"></div></td>
                </tr>
                 <tr style="background-color:#CCFF99;height:30px">
                    <td width="100px"><div align="center">密码</div></td>
                    <td><div align="left"><input type="password" name="upwd1" style="width:98.5%;height:30px"></div></td>
                </tr>
                <tr style="background-color:#CCFF99;height:30px">
                    <td width="100px"><div align="center">确认密码</div></td>
                    <td><div align="left"><input type="password" name="upwd2" style="width:98.5%;height:30px"></div></td>
                </tr>
                <tr style="height:30px;text-align: center">
                    <td colspan="2">
                        <input type="submit" value="注册" style="width:50px;height:30px;">&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="reset" value="重置" style="width:50px;height:30px;">
                    </td>
                </tr>
            </table>
        </form>
        <br/>
        <%
           if(request.getAttribute("ErrorMessage")!=null) {
        %>
        <div align="center">
           <font size="5" color="red"><%=request.getAttribute("ErrorMessage") %></font>
        </div>
        <%
           }
        %>
    </body>
</html>

registersuccess.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>注册成功</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="refresh" content="3; URL=login.jsp">
    </head>
    <body>
        您已经成功注册!<br>
        
    </body>
</html>

login.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>登录页面</title>
        <meta charset="UTF-8">
    </head>
    <body bgcolor="#FFFFFF" >
        <h1 align="center"><b>登录</b></h1>
        <form action="LoginServlet" method="post">
            <table border="1" style="margin:0 auto;width: 25%;height: 25%">
                <tr style="background-color:#FFFFCC;height:30px">
                    <td width="100px"><div align="center">用户名</div></td>
                    <td><div align="left"><input type="text" name="username" style="width:98.5%;height:30px"></div></td>
                </tr>
                 <tr style="background-color:#CCFF99;height:30px">
                    <td width="100px"><div align="center">密码</div></td>
                    <td><div align="left"><input type="password" name="upwd" style="width:98.5%;height:30px"></div></td>
                </tr>
                <tr style="height:30px;text-align: center">
                    <td colspan="2">
                        <input type="submit" value="登录" style="width:50px;height:30px;">&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="reset" value="重置" style="width:50px;height:30px;">
                    </td>
                </tr>
            </table>
        </form>
        如果你还没有账号,请先<a href="register.jsp">注册</a>
         <br/>
        <%
           if(request.getAttribute("ErrorMessage")!=null) {
        %>
        <div align="center">
           <font size="5" color="red"><%=request.getAttribute("ErrorMessage") %></font>
        </div>
        <%
           }
        %>
    </body>
</html>

loginsuccess.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>登录成功</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        您已经成功登录!欢迎您,
        <%=request.getParameter("username")%>
    </body>
</html>

FileUtil.java :

public class FileUtil {
	//注册功能
	public static boolean isRegister(String username,String upwd) {
		boolean flag = false;
		try {
			//创建user.txt文件用于存放用户注册信息
			FileWriter fw = new FileWriter("D:user.txt",true);
			
			//写入用户名
			fw.write(username);
			fw.write("\r\n");
			//写入密码
			fw.write(upwd);
			fw.write("\r\n");
			fw.close();
			flag = true;
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	//登录功能
	public static int Login(String username,String upwd,HttpServletRequest request) {
		int flag = -1;
		String uname = null;
		String pwd = null;
		System.out.println(username);
		System.out.println(upwd);
		try {
			//以只读的方式读取文本数据
			RandomAccessFile raf = new RandomAccessFile("D:user.txt","r");
			long length = raf.length();//文本的长度
			long position = 0;
			while(position < length) {
				uname = raf.readLine();
				pwd = raf.readLine();
				System.out.println(uname);
				System.out.println(pwd);
				if((uname.equals(username)) && (pwd.equals(upwd))) {
					flag = 0;
					break;
				}
				else if((uname.equals(username)) && (!pwd.equals(upwd))) {
					
					flag = 1;
					break;
				}
				else {
					flag = 2;
				}
				position = raf.getFilePointer();
			}
			raf.close();
			
			
		}catch(Exception e) {
			
		}
		
		return flag;
	}
	

}

RegisterServlet.java:

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符编码
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获取表单数据
		String username = request.getParameter("username");
		String upwd1 = request.getParameter("upwd1");
		String upwd2 = request.getParameter("upwd2");
		
		//判断表单是否提交了空数据
		if(username == "" || username == null || upwd1 == "" || upwd1 == null || upwd2 == "" || upwd2 == null) {
			request.setAttribute("ErrorMessage", "用户名或密码不能为空!");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		else if(!upwd1.equals(upwd2)) {
			request.setAttribute("ErrorMessage", "两次密码不一致!");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		else {
			if(FileUtil.isRegister(username, upwd1) == true) {
				response.sendRedirect("registersuccess.jsp");
			}
			else {
				response.sendRedirect("registerfail.jsp");
			}
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

LoginServlet.java:

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符编码
		response.setCharacterEncoding("UTF-8");
	    request.setCharacterEncoding("UTF-8");
	    //获取表单数据
	    String username = request.getParameter("username");
	    String upwd = request.getParameter("upwd");
	    
	    //判断输入框是否为空
	    if(username == null || username == "" || upwd == null || upwd == "") {
	    	request.setAttribute("ErrorMessage", "用户名和密码不能为空!");
	    	request.getRequestDispatcher("login.jsp").forward(request, response);
	    }
	    else if(FileUtil.Login(username, upwd, request) == 0) {
	    	request.getRequestDispatcher("loginsuccess.jsp").forward(request, response);
	    }
	    else if(FileUtil.Login(username, upwd, request) == 1){
	    	request.setAttribute("ErrorMessage","密码有误,请重新输入!");
	    	request.getRequestDispatcher("login.jsp").forward(request, response);
	    }
	    else if(FileUtil.Login(username, upwd, request) == 2) {
	    	request.setAttribute("ErrorMessage","用户名有误或者该用户不存在!");
	    	request.getRequestDispatcher("login.jsp").forward(request, response);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值