Jsp parses user login and registration instances (with source code) based on XML, jspxml

Source: Internet
Author: User

Jsp parses user login and registration instances (with source code) based on XML, jspxml

Simple xml-based database login and Registration
Topic introduction:
1. xml reading and storage, mainly using dom4j Technology, (The network file storage PATH uses classLoader)
File Reading and storage, write a factory class

Public class DocumentFactory {private static Document dom = null; // you need to share a dom, so you need to set it to static private static String name = "user. xml "; private static String filename; // write a static block to read the dom tree. static {// dom4j technology SAXReader read = new SAXReader (); filename = DocumentFactory. class. getClassLoader (). getResource (name ). getPath (); // use the class loader to read files. try {dom = read. read (filename);} catch (incluentexception e) {<span style = "font-f Amily: Arial, Helvetica, sans-serif; "> e. printStackTrace () ;}</span> // mainly obtains and stores two functions (in single-sample mode) (One dom must be shared) public static Document getDocument () {// get the dom tree return dom in xml;} // after registration, Save public static void Save () {XMLWriter wr; try {wr = new XMLWriter (new FileOutputStream (filename);} catch (Exception e1) {throw new RuntimeException ("An error occurred while reading the file");} try {wr. write (dom);} catch (IOException e) {thro W new RuntimeException ("failed to write the file" + e. getMessage ();} finally {try {if (wr! = Null) {wr. close () ;}} catch (IOException e) {throw new RuntimeException ("failed to close the stream" + e. getMessage ());}}}}

2. Front-end technology: It is basically the construction of the interface and the data is uploaded to the background for processing. And some required options.
Code for two pages:
// Log on

<Body> <form action = 'login' method = "post"> Username: <input type = "text" name = "name"/> <br/> password: <input type = "text" name = "pwd"/> <br/> Verification Code: <input type = "text" name = "checkCode">  <a href = "javascript: flush () "> cannot see </a> // rewrite a js file to refresh it <br/> <input type =" submit "value =" Logon "/> <input type =" reset" value = "reset"/> <a href = 'jsps/Reg. jsp '> Registration </a> </form>

// Log on to the background

Public class Login extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); // set the UTF-8 encoding format to receive response. setContentType ("text/html; charset = U TF-8 "); // <span style =" color: # ff0000; "> set the page display mode, which must be set before getting the output stream, or the setting is useless, garbled characters will still appear </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"); // obtain the value Im entered by the Verification code from the interface GDemo id = new ImgDemo (); String str = id. getStr (); if (! Check. equals (str) {out. println ("Logon Failed, incorrect verification code !! "); // If the verification code does not match, return directly to the logon interface out. print ("<a href = 'index. jsp '> return to logon </a> "); return;} // System. out. println ("11" + check); // System. out. println ("22" + str); // obtain all objects before logon. 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 = el E. attributeValue ("pwd"); if (name. trim (). equals (nameC) & pwdC. equals (pwdC) {flag = true; break ;}} if (flag) {out. print ("<font color = 'red' size = '8px '> congratulations! login successful! </Font> "); out. println ("<a href = 'index. jsp '> return to logon </a> ");} else {out. print ("the user name and password do not match. Login Failed... "); Out. println ("<a href = 'index. jsp '> return to logon </a> ");} out. println ("</BODY>"); out. println ("</HTML>"); out. flush (); out. close ();}}

// Register

<Body> <form action = 'reg 'method = "post"> User name: <input type = "text" name = "name" onblur = "check () "id =" name "/> <span id =" spanid "> </span> <br/> password: <input type = "text" name = "pwd" id = "pwd" onblur = "check1 () "/> <span id =" spanid1 "> </span> <br/> confirm the password: <input type = "text" name = "pwd2" id = "pwd2" onblur = "check2 () "/> <span id =" spanid2 "> </span> <br/> <input type =" submit "value =" register "/> <input type =" reset" value = "reset"/> </form> </body>

// Registration background processing

Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); // <span style = "color: # ff0000;"> before obtaining Printwrite, the setting is invalid </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 <El Ement> it = root. elementIterator (); while (it. hasNext () {Element ele = it. next (); String nameR = ele. attributeValue ("name"); // The value passed here may be null. so we have to prevent it at the front-end. Of course, here we also need to consider String pwdR = ele. attributeValue ("pwd"); if (name. equals (nameR) & pwd. equals (pwdR) {flag = true; break ;}} if (flag) {out. print ("this user has been registered !! "); Out. print ("<a href = 'jsps/Reg. jsp '> return registration </a> ");} else {Element ele = root. addElement ("user"); ele. addAttribute ("name", name); ele. addAttribute ("pwd", pwd); DocumentFactory. save (); out. print ("registration successful !! "); Out. print ("<a href = 'index. jsp '> return to logon </a> ");} out. println ("</BODY>"); out. println ("</HTML> ");}

3. Verification code technology: the same image is obtained from the background and matched upon login
:

1. Verify the verification code first.

2. Password matching

3. User Registration

4. The password is correct.

5. view the user. xml file

Source code for the entire login and registration: jsp parses user login and registration instances Based on XML

The above is all the content of this article, hoping to help you learn.

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.