The example in this article describes the way JSP parses XML based on DOM. Share to everyone for your reference, specific as follows:
First-time learning to use the DOM to manipulate XML files, there are many deficiencies, the bulls give some advice, practice I did not do garbled processing, also did not verify Oh! O (∩_∩) o~
Entity class: User
public class User {
private String name;
Private String pwd;
private String Email;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String getpwd () {return
pwd;
}
public void SetPwd (String pwd) {
this.pwd = pwd;
}
Public String Getemail () {return
email;
}
public void Setemail (String email) {
This.email = email;
}
}
Data Access Layer Interface: Userdao
Public interface Userdao {
Boolean login (string name, string pwd);
void Insertuser (user user);
List<user> Selectuser ();
void UpdateUser (user user);
Boolean deleteuser (String name);
Public User findbyname (String name);
}
Interface Implementation class: Userdaoimpl
public class Userdaoimpl implements Userdao {private static final String path= "XML file path";
private void Build (Document dom) {try {//define converter Transformer F = transformerfactory.newinstance (). Newtransformer ();
Sets the encoding format of the output F.setoutputproperty (outputkeys.encoding, "UTF-8");
Build dom source Domsource sources = new Domsource (DOM);
Specifies the destination of the file to be stored streamresult sr = new Streamresult (new file (PATH));
Perform conversion operations F.transform (source, SR);
catch (Exception e) {e.printstacktrace ();
}//Login public boolean login (string name, string pwd) {Boolean flag = false; try {//Create a Document Object document DOM = Documentbuilderfactory.newinstance (). Newdocumentbuilder (). Parse (), based on an XML file (). Fil
E (PATH));
Gets the child nodes under the user node nodelist list = Dom.getelementsbytagname ("user");
Traversal list, data matching exit for (int i = 0; I<list.getlength (); i++) {element el = (Element) list.item (i);
if (Name.equals (El.getattribute ("name")) && pwd.equals (El.getattribute ("pwd"))) {flag = true;
Break
} The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
return flag; //Add Operations public void Insertuser (user user) {try {///Create a new Document Object document DOM = Documentbuilderfactory.newinsta
nCE (). Newdocumentbuilder (). NewDocument ();
Create root node users Element el = dom.createelement ("users");
Add the root node to the Dom Dom.appendchild (EL);
Create a child node Element e2 = dom.createelement ("user");
Adds a child node to the root node El.appendchild (E2);
Get the information existing in the XML file list<user> users = This.selectuser ();
for (int i = 0; i < users.size (); i++) {//Create node user Element El3 = dom.createelement ("user");
User US =users.get (i);
Sets the node's properties (Name,pwd,email) El3.setattribute ("name", Us.getname ());
El3.setattribute ("pwd", us.getpwd ());
El3.setattribute ("Email", us.getemail ());
Add to the root node El.appendchild (EL3);
} e2.setattribute ("Name", User.getname ());
E2.setattribute ("pwd", user.getpwd ());
E2.setattribute ("Email", user.getemail ());
Build (DOM); catch (ParsercOnfigurationexception e) {//TODO auto-generated catch block E.printstacktrace ();
}///Query Operations Public list<user> Selectuser () {list<user> userlist = new arraylist<user> (); try {//create DOM document DOM = Documentbuilderfactory.newinstance (). Newdocumentbuilder (). Parse (), based on an existing XML file (). new File (PATH)
);
Gets the child nodes under all user nodes nodelist list = dom.getelementsbytagname ("user");
for (int i = 0;i <list.getlength (); i++) {User User =new user ();
element element = (Element) list.item (i);
User.setname (Element.getattribute ("name"));
User.setpwd (Element.getattribute ("pwd"));
User.setemail (Element.getattribute ("email"));
Userlist.add (user);
The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
return userlist; }//Modify operation public void UpdateUser (user user) {try {//create DOM document DOM = Documentbuilderfactory.newinsta based on an XML file)
nCE (). Newdocumentbuilder (). Parse (new File (PATH)); Gets the child nodes under the user node nodelist list = Dom.getelemeNtsbytagname ("user");
Traversal list for (int i = 0;i < List.getlength (); i++) {element el = (Element) list.item (i);
Modifies if (User.getname (). Equals (El.getattribute ("name")) {El.setattribute ("pwd", User.getpwd ()) According to the Name property;
El.setattribute ("Email", user.getemail ());
Build (DOM);
(Saxexception e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (Parserconfigurationexception e) {//TODO auto-generated catch block E.printstacktrace (); }//Delete operations public boolean deleteuser (String name) {try {//////Based on XML file creation Domcumet Object Document DOM = Documentbuilderfa
Ctory.newinstance (). Newdocumentbuilder (). Parse (new File (PATH));
Gets the child nodes under the user node nodelist list = Dom.getelementsbytagname ("user");
Traversal list for (int i=0;i<list.getlength (); i++) {Element el = (Element) list.item (i); if (Name.equals (El.getattribute ("name"))) {El.getparentnode (). RemovEchild (EL);
Build (DOM);
return true;
The catch (Exception e) {//Todo:handle Exception} return false;
Find public user findbyname (String name) {User user = new user () based on name; try {//Create a Document Object document DOM = Documentbuilderfactory.newinstance (). Newdocumentbuilder (). Parse (), based on a later XML file ()-New Fil
E (PATH));
Gets the set of child nodes under the user node nodelist list = Dom.getelementsbytagname ("user");
for (int i=0;i<list.getlength (); i++) {Element el = (Element) list.item (i);
if (Name.equals (El.getattribute ("name"))) {User.setname (El.getattribute ("name"));
User.setpwd (El.getattribute ("pwd"));
User.setemail (El.getattribute ("email"));
Break
A catch (Exception e) {e.printstacktrace ());
return to user;
}
}
Business Logic Layer Interface: UserService
Boolean login (string name, string pwd);
void Insertuser (user user);
List<user> Selectuser ();
void UpdateUser (user user);
Boolean deleteuser (String name);
Public User findbyname (String name);
Interface Implementation class: Userserviceimpl
public class Userserviceimpl implements UserService {
Userdao dao = new Userdaoimpl ();
public boolean login (string name, string pwd) {return
dao.login (name, pwd);
}
public void Insertuser (user user) {
dao.insertuser (user);
}
Public list<user> Selectuser () {return
dao.selectuser ();
}
public void UpdateUser (user user) {
dao.updateuser (user);
}
public boolean deleteuser (String name) {return
dao.deleteuser (name);
}
Public User findbyname (String name) {return
dao.findbyname (name);
}
}
Control layer: Useraction
public class Useraction extends actionsupport{private user user;
Public user GetUser () {return user;
public void SetUser (user user) {this.user = user;
} userservice UserService = new Userserviceimpl ();
Public String Selectuser () {HttpServletRequest request = Servletactioncontext.getrequest ();
list<user> users = new arraylist<user> ();
Users = Userservice.selectuser ();
Request.setattribute ("USER", users);
return "SELECT"; /** * Login * @return/public String login () {if (user.getname ()!= null && user.getpwd ()!= null) {Boole
An flag = Userservice.login (User.getname (), user.getpwd ());
if (flag) {return SUCCESS;
} return ERROR;
/** * Modify * @return/Public String update () {userservice.updateuser (user);
return "Update";
/** * Edit * @return/Public String edit () {HttpServletRequest request = Servletactioncontext.getrequest ();
String name = Request.getparameter ("UName"); if (name!= null) {User U = UserservicE.findbyname (name);
Request.setattribute ("USER", u);
Return "edit";
/** * Delete * @return/public String Delete () {HttpServletRequest request = Servletactioncontext.getrequest ();
String name = Request.getparameter ("UName");
Boolean flag = Userservice.deleteuser (name);
SYSTEM.OUT.PRINTLN (flag);
return SUCCESS;
/** * Add * @return */public String Insert () {userservice.insertuser (user);
return "Insert";
}
}
Struts.xml configuration (struts2 for me):
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1.7//en" "Http://struts.apache.org/dt Ds/struts-2.1.7.dtd "> <struts> <package name=" file "extends=" Struts-default "> <action name=" List "C lass= "Com.jun.action.UserAction" method= "Selectuser" > <result name= "select" >/list.jsp</result> < /action> <action name= "Login" class= "com.jun.action.UserAction" method= "Login" > <result name= "Success" Type= "Redirectaction" >/list.action</result> <result name= "error" >/login.jsp</result> </ action> <action name= "Insert" class= "com.jun.action.UserAction" method= "Insert" > <result name= "Insert" Type= "Redirectaction" >/list.action</result> </action> <action name= "Delete" class= "
Com.jun.action.UserAction "method=" delete "> <result type=" redirect ">/list.action</result></action> <action name= "Update" class= "Com.jun.action.UserAction" method= "Update" > <result name= "upd" Ate "type=" redirectaction ">/list.action</result> </action> <action name=" edit "class=" Com.jun.ac tion. Useraction "method=" edit "> <result name=" edit ">/update.jsp</result> </action> </package > </struts>
Web.xml Configuration
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.5" xmlns= "http://java.sun.com/xml/ns/"
Java ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
< filter-name>struts2</filter-name>
<filter-class>
Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</ filter>
<filter-mapping>
<filter-name>struts2</filter-name>
< url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Four pages: login.jsp list.jsp insert.jsp,update.jsp
login.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
List.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <%@ taglib uri= "/struts-tags" prefix= "s"%&
Gt
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Insert.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Update.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <%@ taglib "uri=" http://java.sun.com/jsp/
Jstl/core "prefix=" C "%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The User.xml document I used to learn
<?xml version= "1.0" encoding= "UTF-8"?>
<users>
<user email=aaa@sina.com "BBB" name= " Aaaaaa "/>
<user email=bbb@sina.com name=" CCC "pwd=" aaaaaa "/>
</users>
I hope this article will help you with JSP program design.