[Java EE] Three-tier architecture case-user module (i)

Source: Internet
Author: User

User Registration Login Logout

servlet+jsp+javabean+dom4j

Hierarchical structure:

Com.tsh.web

Com.tsh.service

Com.tsh.dao

Com.tsh.domain

Com.tsh.util

Com.tsh.test

Com.tsh.exception

Com.tsh.factory

Packages used:

dom4j Jstl beanutils JUnit

Users.xml----------- Simulation Database

Config.properties------------The main configuration file

XPATH:

DOM4J provides a way to find the specified node in the XML file, similar to the regular lookup of the specified text in a text file

Com.tsh.dao.XmlUserDao.java

 PackageCom.tsh.dao;Importjava.util.List;Importorg.dom4j.Document;ImportOrg.dom4j.DocumentHelper;Importorg.dom4j.Element;ImportCom.tsh.domain.User;ImportCom.tsh.util.XmlDaoUtil; Public classXmluserdao {/*** Find User by username *@paramusername *@return     */     PublicUser Finduserbyusername (String username) {Document Dom=Xmldaoutil.getdom (); Element Root=dom.getrootelement (); //using the XPath syntaxList<element> list=root.selectnodes ("//user[@username = '" +username+ "']"); if(List.size () >0) {Element userelement=list.get (0); String uname=userelement.attributevalue ("username"); String Password=userelement.attributevalue ("Password"); //encapsulating user information into a beanUser user=NewUser ();            User.setusername (uname);            User.setpassword (password);            User.setpassword_confirm (password); returnuser; }        return NULL; }    /*** Add User *@paramUser*/     Public voidaddUser (user user) {Document Dom=Xmldaoutil.getdom (); Element Root=dom.getrootelement (); //Create <user> elementsElement userelement=documenthelper.createelement ("User"); Userelement.setattributevalue ("Username", User.getusername ()); Userelement.setattributevalue ("Password", User.getpassword ()); //Mount to <users>Root.add (userelement); //write back to XML file            }}

Com.tsh.util.XmlDaoUtil.java

 PackageCom.tsh.util;ImportJava.net.URL;Importorg.dom4j.Document;ImportOrg.dom4j.io.SAXReader; Public classXmldaoutil {Private StaticURL Path=xmldaoutil.class. getClassLoader (). GetResource ("User.xml"); /*** Using static code blocks, objects are instantiated only once*/    Private StaticDocument Dom; Static{Saxreader Reader=NewSaxreader (); Try {            //class loader finds physical pathDom=reader.read (path); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Get XML *@return     */     Public StaticDocument GetDOM () {returnDom; }    /*** Write XML*/     Public Static voidwriteXml () {}}

Com.tsh.domain.User.java

 PackageCom.tsh.domain;Importjava.io.Serializable; Public classUserImplementsserializable{PrivateString username; PrivateString password; PrivateString password_confirm;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString getpassword_confirm () {returnpassword_confirm; }     Public voidsetpassword_confirm (String password_confirm) { This. password_confirm =password_confirm; }    }

User.xml

<? XML version= "1.0" encoding= "UTF-8" ?> < Users > <  username= "admin"  password= "admin"></User  ></users>

[Java EE] Three-tier architecture case-user module (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.