欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析(附源碼)

 更新時(shí)間:2015年11月12日 15:06:17   投稿:lijiao  
這篇文章主要介紹了jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析,xml做數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶登錄與注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

簡(jiǎn)單的基于xml做數(shù)據(jù)庫(kù)的登錄與注冊(cè)

主題介紹:

1.xml的讀取和存儲(chǔ),主要是用到dom4j技術(shù),(網(wǎng)絡(luò)中的文件存儲(chǔ)路徑采用classLoader)

文件的讀取和存儲(chǔ),寫了一個(gè)工廠類

public class DocumentFactory { 
 private static Document dom=null;//需要共享一個(gè)dom,所以需要設(shè)置為static 
 private static String name="user.xml"; 
 private static String filename; 


//寫一個(gè)靜態(tài)塊實(shí)現(xiàn)對(duì)dom樹的讀取 
static{//dom4j技術(shù) 
 SAXReader read=new SAXReader(); 
 filename=DocumentFactory.class.getClassLoader().getResource(name).getPath();//采用類加載器進(jìn)行讀取文件 
  try { 
  dom=read.read(filename); 
  } catch (DocumentException e) {<span style="font-family: Arial, Helvetica, sans-serif;">  e.printStackTrace();}}</span> 

//主要獲得和存儲(chǔ)的兩個(gè)函數(shù)(采用單例模式)(必須共享一個(gè)dom數(shù)) 

public static Document getDocument(){ //獲得xml中的dom樹 
 return dom; 
} 

//注冊(cè)之后需要保存 
 public static void Save() { 
 XMLWriter wr; 
 try { 
  wr = new XMLWriter(new FileOutputStream(filename)); 
 }catch (Exception e1) { 
  throw new RuntimeException("存儲(chǔ)文件時(shí)讀文件失敗"); 
 } 
 try { 
  wr.write(dom); 
 } catch (IOException e) { 
  throw new RuntimeException("寫文件失敗"+e.getMessage()); 
 }finally{ 
  try { 
  if(wr!=null){ 
  wr.close(); 
  } 
  } catch (IOException e) { 
  throw new RuntimeException("關(guān)流失敗"+e.getMessage());}}} 
} 

2.前臺(tái)的技術(shù):基本上就是界面的搭建和將數(shù)據(jù)傳到后臺(tái)進(jìn)行處理。以及部分的必填選項(xiàng)要求。

兩個(gè)頁(yè)面的代碼:
//登錄

<body> 
  <form action='login' method="post"> 
  用戶名:<input type="text" name="name" /><br/> 
  密 碼 :<input type="text" name="pwd" /><br/> 
  驗(yàn)證碼:<input type="text" name="checkCode"><img src="/LOGIN/immg" id='imgid'><a href="javascript:flush()">看不清</a>//需要重寫一個(gè)js進(jìn)行刷新 
  <br/> 
  <input type="submit"value="登錄" /> 
  <input type="reset"value="重置" /> 
  <a href='jsps/Reg.jsp'>注冊(cè)</a> 
  
 </form> 

//登錄后臺(tái)的處理

public class Login extends HttpServlet { 
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException { 
 doPost(request, response); 
 } 
 
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException { 
  
 request.setCharacterEncoding("utf-8");//設(shè)置utf-8的編碼格式去接收 
 response.setContentType("text/html;charset=UTF-8");//<span style="color:#ff0000;">設(shè)置頁(yè)面顯示方式,這個(gè)設(shè)置必須要在獲得輸出流之前設(shè)置,不然設(shè)置都沒(méi)有用,照樣會(huì)出現(xiàn)亂碼</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>"); 
 out.println(" <meta http-equiv='content-type' content='text/html; charset=UTF-8'> </HEAD>"); 
 out.println(" <BODY>"); 
 String name=request.getParameter("name"); 
 String pwd=request.getParameter("pwd"); 
 String check=request.getParameter("checkCode");//從界面獲得驗(yàn)證碼輸入的值 
 ImgDemo id =new ImgDemo(); 
 String str=id.getStr(); 
 if(!check.equals(str)){ 
  out.println("登錄失敗,驗(yàn)證碼不正確?。?);//要是驗(yàn)證碼不符合,直接返回登錄界面 
  out.print("<a href='index.jsp'>返回登錄</a>"); 
  return; 
 } 
// System.out.println("11"+check); 
// System.out.println("22"+str); 
  
 //登錄前獲得所有的對(duì)象 
 Document dom=DocumentFactory.getDocument(); 
 boolean flag=false; 
 Element root=dom.getRootElement(); 
 Iterator<Element> it=root.elementIterator(); 
  
 while(it.hasNext()){ 
  Element ele =it.next(); 
  String nameC=ele.attributeValue("name"); 
  String pwdC=ele.attributeValue("pwd"); 
  if(name.trim().equals(nameC)&&pwdC.equals(pwdC)){ 
  flag=true; 
  break; 
  } 
 } 
 if(flag){ 
  out.print("<font color='red' size='8px'>恭喜您,登陸成功!</font>"); 
  out.println("<a href='index.jsp'>返回登錄</a>"); 
 }else{ 
  out.print("用戶名和密碼不匹配。登錄失敗。。。"); 
  out.println("<a href='index.jsp'>返回登錄</a>"); 
 } 
 out.println(" </BODY>"); 
 out.println("</HTML>"); 
 out.flush(); 
 out.close(); 
 } 
 
} 

//注冊(cè)

<body> 
 <form action='reg' method="post"> 
  用戶 名:<input type="text" name="name" onblur="check()" id="name"/><span id="spanid"></span><br/> 
  密 碼 : <input type="text" name="pwd" id="pwd" onblur="check1()"/><span id="spanid1"></span><br/> 
  確認(rèn)密碼 :<input type="text" name="pwd2" id="pwd2" onblur="check2()"/><span id="spanid2"></span><br/> 
  <input type="submit"value="注冊(cè)" /> 
  <input type="reset"value="重置" /> 
  
 </form> 
 </body> 

//注冊(cè)的后臺(tái)處理

public void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException { 
 
 response.setContentType("text/html;charset=utf-8");//<span style="color:#ff0000;">必須要設(shè)置在獲得Printwrite之前,都則設(shè)置無(wú)效</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");//這里傳過(guò)來(lái)的值可能是null.所以我們必須在前臺(tái)要預(yù)防一下,當(dāng)然在這里也要考慮一下 
  String pwdR=ele.attributeValue("pwd"); 
  if(name.equals(nameR)&&pwd.equals(pwdR)){ 
  flag=true; 
  break; 
  } 
 } 
 if(flag){ 
  out.print("此用戶已注冊(cè)!!"); 
  out.print("<a href='jsps/Reg.jsp'>返回注冊(cè)</a>"); 
 }else{ 
  Element ele=root.addElement("user"); 
  ele.addAttribute("name", name); 
  ele.addAttribute("pwd", pwd); 
  DocumentFactory.Save(); 
  out.print("注冊(cè)成功??!"); 
  out.print("<a href='index.jsp'>返回登錄</a>"); 
 } 
 out.println(" </BODY>"); 
 out.println("</HTML>"); 
} 

3.驗(yàn)證碼技術(shù):同樣的從后臺(tái)獲取圖片,以及登錄時(shí)候進(jìn)行匹配
效果圖:

1,首先是驗(yàn)證驗(yàn)證碼的

2.密碼匹配

3,用戶注冊(cè)

4.密碼正確

 5,查看user.xml文件

整個(gè)登錄和注冊(cè)的源代碼下載地址:jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論