参考代码来源:https://blog.csdn.net/aasd23/article/details/123722532
为完成实验目标,本人在网络上找到了一个基于jsp实现简单登录注册功能的程序,该项目功能基本完善,界面设计美观,值得我们学习。
但经过测试研究发现该项目存在如下缺陷:
①注册的账号不能保存,关闭该程序第二次使用时还需重新注册
②用户注册不存在限制,不符合实际要求
对此,本人对该项目进行了二次开发,对上述问题进行了初步的解决,方式如下:
①由于暂时还没有学习数据库有关的知识内容,参考网上知识使用文件进行有关数据的保存
②在用户注册阶段增加注册码,只有注册码正确才能进行账号的注册,正确注册码为:520shou
实验环境:window 10 eclipse
二次开发结果如下:
改进代码(主要内容截图):
register.jsp
<div class="center"> <p class="fon">注册界面</p> <p>请输入您的用户名和密码进行注册</p> <form method="post" action="registerservlet"> 账号:<input type="text"name="username" style="width: 200px;height: 40px" placeholder="请输入账号" > <br> 密码: <input type="text"name="password" style="width: 200px;height: 40px" placeholder="请输入密码" > <br> 注册码:<input type="text"name="unlockkey" style="width: 200px;height: 40px" placeholder="请输入注册码" > <br> <button type="submit" style="width:80px;height:40px; font-size: 20px" class="clear">注册</button> <button type="reset" style="width:80px;height:40px; font-size: 20px" class="clear">重置</button> <br> </form> </div>
resigterservlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html; charset=utf-8"); request.setCharacterEncoding("UTF-8"); String username =request.getParameter("username"); String password =request.getParameter("password"); String unlockkey=request.getParameter("unlockkey"); if(unlockkey.equals("520shou")) { File file=new File("D:/workspace/Logininterface/userinformation.txt"); try { BufferedWriter fileWriter=new BufferedWriter(new FileWriter(file,true)); fileWriter.write(username+"/"+password); fileWriter.newLine(); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { System.out.println("输入错误"); e.printStackTrace(); } } request.getRequestDispatcher("registersuccess.jsp").forward(request, response); }
checkservlet.java
package Logininterface; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; 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 checkservlet */ @WebServlet("/checkservlet") public class checkservlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final BufferedReader NULL = null; private String password; private String username; /** * @see HttpServlet#HttpServlet() */ public checkservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html; charset=utf-8"); request.setCharacterEncoding("UTF-8"); String username =request.getParameter("username"); String password =request.getParameter("password"); boolean a =checklogin(username,password); if(a) { request.getRequestDispatcher("loginsuccess.jsp").forward(request, response); } else request.getRequestDispatcher("loginfail.jsp").forward(request, response); } private boolean checklogin(String username, String password) throws IOException { // TODO Auto-generated method stub boolean flag = false; BufferedReader lbd; lbd = new BufferedReader(new FileReader("D:/workspace/Logininterface/userinformation.txt")); String line = null; while ((line = lbd.readLine()) != null) { String[] datas = line.split("/"); if (datas[0].equals(username) && datas[1].equals(password)) { flag = true; break; } } return flag; } /** * @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); } }
运行结果:
文件内容(可以直接对文件进行修改完成账号的增删改查)
实验感悟
①我们在未来的工作中,对公司原有项目进行二次开发的概率比从头开始进行开发的概率大的多,因此掌握如何实现二次开发的技能是十分重要的
②在研究他人的代码时,相比从上往下一字一句的研究代码,我们更应该先建立一个大致框架,然后在根据这个框架补足细节,这样才能更深刻的理解
③网上很多实现登录注册功能的程序都使用了数据库的知识,做出来的成果具有更广泛的应用性,功能也更多,希望在学完相关知识后能采用数据库进行进一步改进
标签:username,逆向,String,软件设计,request,实验,import,password,response From: https://www.cnblogs.com/xyx2003/p/17180553.html