public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//<span>必须要设置在获得Printwrite之前,都则设置无效</span> PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); boolean flag=false; request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); Document dom =DocumentFactory.getDocument(); Element root=dom.getRootElement(); Iterator<Element> it=root.elementIterator(); while(it.hasNext()){ Element ele=it.next(); String nameR=ele.attributeValue("name");//这里传过来的值可能是null.所以我们必须在前台要预防一下,当然在这里也要考虑一下 String pwdR=ele.attributeValue("pwd"); if(name.equals(nameR)&&pwd.equals(pwdR)){ flag=true; break; } } if(flag){ out.print("此用户已注册!!"); out.print("<a href='https://www.jb51.net/jsps/Reg.jsp'>返回注册</a>"); }else{ Element ele=root.addElement("user"); ele.addAttribute("name", name); ele.addAttribute("pwd", pwd); DocumentFactory.Save(); out.print("注册成功!!"); out.print("<a href='https://www.jb51.net/index.jsp'>返回登录</a>"); } out.println(" </BODY>"); out.println("</HTML>"); }
3.验证码技术:同样的从后台获取图片,以及登录时候进行匹配
效果图:
1,首先是验证验证码的
2.密码匹配
3,用户注册
4.密码正确
5,查看user.xml文件
整个登录和注册的源代码下载地址:jsp基于XML实现用户登录与注册的实例解析