Simple XML Database login and Registration
Topic Description:
1.xml Read and save, mainly to dom4j technology , (the file storage path in the network using ClassLoader)
Read and store files, write a factory class
public class Documentfactory {private static Document dom=null;//needs to share a DOM, so it needs to be set to static private static String Name=
"User.xml";
private static String filename;
Write a static block to implement the reading of the DOM tree static{//dom4j technology Saxreader read=new Saxreader (); Filename=documentfactory.class.getclassloader (). GetResource (name). GetPath ();//using the class loader to read the file try {dom=read.read (f
Ilename); catch (Documentexception e) {<span style= "font-family:arial, Helvetica, Sans-serif;" > E.printstacktrace ();}
</span>//main acquired and stored two functions (in single case mode) (must share a DOM number) public static Document GetDocument () {//Get the DOM tree return DOM in XML;
You need to save the public static void Save () {XMLWriter WR after registering;
try {wr = new XMLWriter (new FileOutputStream (filename));
}catch (Exception E1) {throw new RuntimeException ("failed to read files while storing files");
try {wr.write (DOM);
catch (IOException e) {throw new RuntimeException ("Write file Failed" +e.getmessage ());
}finally{try {if (wr!=null) {wr.close (); } catch (IOException e) {throw new RuntimeException ("Off stream failed" +e.getmessage ());}}
}
2. Foreground technology: Basically is the construction of the interface and the data to the background for processing. And part of the required option requirements.
Two-page code:
//Login
<body>
<form action= ' login ' method= ' post ' >
username: <input type= "text" name= "name"/><br/ >
Password: <input type= "text" name= "pwd"/><br/>
Authentication code: <input type= "text" name= "Checkcode" > <a href= "Javascript:flush ()" > can't see the </a>//need to rewrite a JS to refresh
<br />
<input type= "Submit" value= "Login"/> <input type=
"reset" value= "reset"/> <a
' href= ' JSPs /reg.jsp ' > Registration </a>
Log in background processing
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, Ioexcep tion {request.setcharacterencoding ("utf-8");//Set UTF-8 encoding format to receive Response.setcontenttype ("Text/html;charset=utf-8" );//<span style= "color: #ff0000;"
> Set the page display mode, this setting must be set before getting output stream, otherwise the settings are not used, so will appear garbled </span> printwriter out = Response.getwriter (); Out.println ("<!
DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println ("
Registered
<body>
<form action= ' reg ' method= "POST" >
username: <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 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>
Background processing for registration
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("Text/html;charset=utf-8");//<span style= "color: #ff0000;"
> must be set to set invalid </span> printwriter out = Response.getwriter () before obtaining printwrite; Out.println ("<!
DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println ("
3. Verification code technology: the same from the background to get pictures, and login time to match
Effect Chart:
1, the first is to verify the authentication code
2. Password Matching
3, User Registration
4. Correct password
5, view User.xml file
Entire login and registered source code download address: JSP implementation of user login and registration based on XML example analysis
The above is the entire content of this article, I hope to help you learn.