[JavaEE] Three-tier architecture case study-User Module (I), javaee three-tier

Source: Internet
Author: User

[JavaEE] Three-tier architecture case study-User Module (I), javaee three-tier

User Registration and login logout

Servlet + JSP + javaBean + dom4j

 

Layered 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 ----------- simulate a database

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

 

XPATH:

The method provided by dom4j allows you to find the specified node in the xml file, similar to regular expressions in the text file to find the specified text

 

 

Com. tsh. dao. XmlUserDao. java

Package com. tsh. dao; import java. util. list; import org. dom4j. document; import org. dom4j. export enthelper; import org. dom4j. element; import com. tsh. domain. user; import com. tsh. util. xmlDaoUtil; public class XmlUserDao {/*** Search user * @ param username * @ return */public User findUserByUsername (String username) {Document dom = XmlDaoUtil based on the user name. getDom (); Element root = dom. getRootElement (); // use the XPATH syntax List <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"); // encapsulate User information to bean user User = new user (); User. setUsername (uname); user. setPassword (password); user. setPassword_confirm (password); return user;} return null;}/*** Add user * @ param User */public void addUser (user) {Document dom = XmlDaoUtil. getDom (); Element root = dom. getRootElement (); // create the <user> Element userElement = incluenthelper. createElement ("user"); userElement. setAttributeValue ("username", user. getUsername (); userElement. setAttributeValue ("password", user. getPassword (); // mount it to <users> root. add (userElement); // write back to xml file }}

 

 

Com. tsh. util. XmlDaoUtil. java

Package com. tsh. util; import java.net. URL; import org. dom4j. document; import org. dom4j. io. SAXReader; public class XmlDaoUtil {private static URL path = XmlDaoUtil. class. getClassLoader (). getResource ("user. xml ");/*** use a static code block. The object is instantiated only once */private static Document dom; static {SAXReader reader = new SAXReader (); try {// The class loader finds the physical path dom = reader. read (path);} catch (Exception e) {e. printStackTrace () ;}/ *** get xml * @ return */public static Document getDom () {return dom ;} /*** write xml */public static void writeXml (){}}

 

 

Com. tsh. domain. User. java

package com.tsh.domain;import java.io.Serializable;public class User implements Serializable{    private String username;    private String password;    private String password_confirm;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getPassword_confirm() {        return password_confirm;    }    public void setPassword_confirm(String password_confirm) {        this.password_confirm = password_confirm;    }    }

 

 

User. xml

<?xml version="1.0" encoding="UTF-8"?><users><user username="admin" password="admin"></user></users>

 

 

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.