1024programmer Java An example of development using JSP+JAVABEAN+XML_MySQL-mysql tutorial

An example of development using JSP+JAVABEAN+XML_MySQL-mysql tutorial

This example refers to relevant documents on JSP operations on XML on some websites, and combines some personal experiences. The example involves the development of part of the code for the backend management of an enterprise’s internal ordering system. The function is mainly focused on the management of basic information about restaurants.

The example itself was developed as part of a joke between me and my colleagues at my original company. I would also like to express my longing for those friends who have worked together.

The example itself is a B/S structured program running under the TOMCAT4.01 platform. The configuration of TOMCAT will not be explained here. Just explain the directory structure of related files and folders.

Directory structure description:
/tomcat/webapps/canyin/ —–main directory
/tomcat/webapps/canyin/jsp/ —–JSP file directory
/tomcat/webapps/canyin/jsp/admin/ —–The storage directory of JSP files for background management
/tomcat/webapps/canyin/WEB-INF/classes/canyin/ ——javabean File storage directory
/tomcat/webapps/canyin/data/ —–xml file storage directory
/tomcat/webapps/ROOT/ —–tomcat startup file storage folder, only index.html file

File brief description:
/tomcat/webapps/canyin/data/users.xml —–Record user information
/tomcat/webapps/canyin/data/restaurants .xml —–Record the basic information of the restaurant

/tomcat/webapps/ROOT/index.html —–Home page, an input box appears on the page, requiring the user to enter a user name and password

/tomcat/webapps/canyin/jsp/loginjudge.jsp —–User identity judgment page, based on the user name and password, it is decided whether the page will be transferred to the background management terminal or the front-end client. In this example, once the user’s identity is confirmed to have management rights, he can enter the backend management terminal and jump directly to the restaurant’s basic information management page, simplifying the explanation process.
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp —–Basic restaurant information management page, managing the name, phone number, address and other information of the restaurant

/tomcat/webapps/canyin /WEB-INF/classes/canyin/checkSessionBean.class —– The background management terminal detects the session value that marks the user’s identity. If it is not the administrator, it will jump back to the login page.
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class —–Connect xml file
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class —–Write xml file

Detailed introduction of the file and accompanying code description.

/tomcat/webapps/canyin/data/users.xml

Code:

Description: The meaning of the fields is the user name, password and user’s identity
>
/tomcat/webapps/canyin/data/restaurants.xml

Code:



Shanghai Ting Fast Food Restaurant
021-76546726
Broadway Plaza Tower B


Shangri-La Hotel
021-2312134
No. 1023, Nanjing Road

Description: The attribute records the total number of records in the restaurants.xml file. Every time a new item is added, the value will increase by 1 regardless of whether it is deleted later, just like the id item that is used to automatically increase by 1 in the database. Used to assign a unique value to the attribute of the newly added . The meaning of other fields is more obvious.
/tomcat/webapps/ROOT/index.html (simple HTML code)

Code:

Catering system Login

Catering system login

Login name: <input maxlength=16
name =”username” class=stedit value=”joard”>
Password: <input class=stedit maxlength=16
name=”userpass” type=password value=”oddworld”>

“);
return;
}

xmlBean.connXml(“webapps/canyin/data/users.xml”);
doc=xmlBean.getXmlDoc();

try{
users =doc.getElementsByTagName(“user”);

for (int i=0;i<users.getLength();i++){
Element user=(Element ) users.item(i);

String strAtrNameValue=user.getAttributeNode(“name”).getNodeValue();
String strAtrPassWordValue=user.getAttributeNode(“password”).getNodeValue();
String strAtrRoleValue=user.getAttributeNode(“roles”).getNodeValue();

if (strAtrNameValue.equals(strUsername) && strAtrPassWordValue.equals(strPassword)){

if (strAtrRoleValue.equals(“admin”)){
out.println(“”);

//Set the session (sesUserRole) indicating the user identity, and the administrator identity is admin
session.setAttribute(“sesUserRole”,”admin”);

//Jump to the management page
response.sendRedirect(“admin/admin_rest.jsp”);
return;

}else{
//Set the session (sesUserRole) indicating the user’s identity. The administrator’s identity is user
session.setAttribute(“sesUserRole”,”user”);

//Jump to the normal user page
response.sendRedirect(“index.jsp”);
return;
}

}else{
out.println(“”) ;
return;
}

}
}catch(Exception e){
strExc=e.toString();
}
%>
Description: …….

/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp

Code:
<%– oddWorld Catering Management System (Simplified) Chinese version) December 1, 2002
copy right by joard ast

admin_rest.jsp Function: Backend management page, restaurant management page.
–%>

<%//Verify the identity of the canyin user to determine whether he is an administrator
if(!checkSessionBean.checkSessionBean( request,”sesUserRole”,”admin”)){
out.print(showDialog(“You do not have administrative permission!”,”/index.html”));
return;
}

//Get relevant data from the restaurant information file rest.xml
Document doc;
NodeList restaurants;

String strAct;
int intId=0;
String strOperation=”show”;

//Accept external parameters
strAct=(String)request.getParameter(“act”);

xmlBean.connXml(“webapps/ canyin/data/restaurants.xml”);
doc=xmlBean.getXmlDoc();
restaurants =doc.getElementsByTagName(“restaurant”);

//According to the external parameters passed in Determine the operation on the restaurant.xml file
if (strAct!=null){
if(strAct.equals(“addnewDo”)){

String strName;
String strPhone;
String strAddress;
Text textseg;

strName=(String)request.getParameter(“name”).trim();
strPhOne=(String)request.getParameter(“phone” ).trim();
strAddress=(String)request.getParameter(“address”).trim();

//Data verification
if(strName==null){
out.print(showDialog(“The restaurant name cannot be empty!”));
return;
}
if(strPhOne==null){
out.print(showDialog(“Restaurant The phone number cannot be empty!”));
return;
}
/*if(strAddress==null){
out.print(showDialog(“The restaurant address cannot be empty!”)) ;
return;
}*/

//Verify the uniqueness of data
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(((String)restaurant.getElementsByTagName(“name”).item(0).getFirstChild().getNodeValue()).equals(strName)) {
out.print(showDialog(“Duplicate restaurant name!”));
return;
}else{
if(((String)restaurant.getElementsByTagName(“name”).item( 0).getFirstChild().getNodeValue()).equals(strPhone)){
out.print(showDialog(“Duplicate restaurant phone number!”));
return;
}
}

}

//Get the number of existing records and set a unique incremental id attribute for the new restaurant record
int intNum=0;
Element restNum=(Element)doc.getElementsByTagName(“restaurants”).item(0);
intNum=Integer.parseInt(restNum.getAttributeNode(“num”).getNodeValue());

intNum+=1;

//Add 1 to the value of the restaurant attribute num
restNum.getAttributeNode(“num”).setNodeValue(String.valueOf(intNum));

//New node
Element newRestaurant=doc.createElement(“restaurant”);

Attr newArrId=doc.createAttribute(“id”);
//Attribute newArrId = new Attribute(” id”,String.valueOf(intNum));
textseg=doc.createTextNode(String.valueOf(intNum));
newArrId.setValue(String.valueOf(intNum));
newRestaurant.setAttributeNode( newArrId);

Element newName=doc.createElement(“name”);
textseg=doc.createTextNode(strName);
newName.appendChild(textseg);
newRestaurant.appendChild( newName);

Element newPhOne=doc.createElement(“phone”);
textseg=doc.createTextNode(strPhone);
newPhone.appendChild(textseg);
newRestaurant.appendChild( newPhone);

Element newAddress=doc.createElement(“address”);
textseg=doc.createTextNode(strAddress);
newAddress.appendChild(textseg);
newRestaurant.appendChild( newAddress);

doc.getDocumentElement().appendChild(newRestaurant);

//Call the bean to write the corresponding xml file
writeXmlBean.writeXml(doc,”webapps/canyin/ data/restaurants.xml”);

response.sendRedirect(request.getRequestURI());
return;
}
if(strAct.equals(“modiDo”)){
String strName;
String strPhone;
String strAddress;
Text textseg;
int modiId;
//Record which item(i) the record to be modified is
> int intI=0;

strName=(String)request.getParameter(“name”).trim();
strPhOne=(String)request.getParameter(“phone”).trim() ;
strAddress=(String)request.getParameter(“address”).trim();
modiId=Integer.parseInt(request.getParameter(“recordId”).trim());

//Data verification
if(strName==null){
out.print(showDialog(“The restaurant name cannot be empty!”));
return;
}
if (strPhOne==null){
out.print(showDialog(“The restaurant phone number cannot be empty!”));
return;
}
if(modiId==0){
out.print(showDialog(“The record of the restaurant you want to modify does not exist!”));
return;
}
/*if(strAddress==null){
out.print(showDialog (“The restaurant address cannot be empty!”));
return;
}*/

//Flag shows that the record exists
boolean recordExist=false;

/ /Verify the uniqueness of data
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);

if(Integer.parseInt(restaurant.getAttributeNode(“id”).getNodeValue())==modiId){
recordExist=true;
intI=i;

}

if(((String)restaurant.getElementsByTagName(“name”).item(0).getFirstChild().getNodeValue()).equals(strName) && Integer.parseInt(restaurant.getAttributeNode(“id”).getNodeValue( ))!=modiId ){
out.print(showDialog(“Duplicate restaurant name!”));
return;
}else{
if(((String)restaurant.getElementsByTagName(” name”).item(0).getFirstChild().getNodeValue()).equals(strPhone) && Integer.parseInt(restaurant.getAttributeNode(“id”).getNodeValue())!=modiId ){
out. print(showDialog(“Duplicate restaurant phone number!”));
return;
}
}

}

if(!recordExist) {
out.print(showDialog(“The record of the restaurant you want to modify does not exist!”));
return;
}else{
//Change the record
try{
Element modiRestaurant=(Element) restaurants.item(intI);
modiRestaurant.getElementsByTagName(“name”).item(0).getFirstChild().setNodeValue(strName);
modiRestaurant.getElementsByTagName(” phone”).item(0).getFirstChild().setNodeValue(strPhone);
modiRestaurant.getElementsByTagName(“address”).item(0).getFirstChild().setNodeValue(strAddress);

//Call the bean to write the corresponding xml file
writeXmlBean.writeXml(doc,”webapps/canyin/data/restaurants.xml”);

response.sendRedirect(request.getRequestURI());
return;

}catch(Exception e){}
}
}
//Perform deletion operation
if(strAct.equals(“del”)){
int delId;
//Record which item(i) the record to be modified is
int intI=0;

delId=Integer.parseInt(request.getParameter(“recordId “).trim());

if(delId==0){
out.print(showDialog(“The record of the restaurant you want to modify does not exist!”));
return;
}

The file:// flag shows the existence of the record
boolean recordExist=false;

//Verify the uniqueness of the data
br> for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);

if(Integer.parseInt(restaurant. getAttributeNode(“id”).getNodeValue())==delId){
recordExist=true;
intI=i;

}
}

if(! recordExist){
out.print(showDialog(“The record of the restaurant you want to delete does not exist!”));
return;
}else{
//Perform record deletion operation
try{
Node delNode=(Node)restaurants.item(intI);

doc.getElementsByTagName(“restaurants”).item(0).removeChild(delNode);

/ /Call the bean to write the corresponding xml file
writeXmlBean.writeXml(doc,”webapps/canyin/data/restaurants.xml”);

response.sendRedirect(request.getRequestURI());
return;

}catch(Exception e){}
}

}
}

//The corresponding processing of the page is determined by the external parameters passed in Status
if (strAct==null){
strOperation=”show”;
}else{
if (strAct.equals(“modi”)){
strOperation=”modi” ;
intId=Integer.parseInt(request.getParameter(“recordId”));
}else{
if(strAct.equals(“addnew”)){
strOperation=”addnew”;
}else{
strOperation=”show”;
}
}
}

//If it is an empty record, change the page status to “New Add”
if (restaurants.getLength()==0){
strOperation=”addnew”;
}
%>

Catering system management–restaurant management [Exit system]

<table bgcolor="#999999" align=center border=0 cellpadding=1 cellspacing=1
width=”90%” >

Restaurant name

Restaurant phone number

Restaurant address

Modify

Delete

<%
for(int i=0;i<restaurants.getLength();i++)
{
Element restaurant=(Element) restaurants.item(i);

if (strOperation==”modi” && Integer.parseInt(restaurant.getAttributeNode(“id”).getNodeValue() )==intId){
%>

? act=modiDo” method=”post” OnSubmit=”return checkform(this);” >

<input type="hidden" name ="recordId" value="”>

<input name="name" class=stedit
style=”HEIGHT: 22px; WIDTH: 150px” value=”<%if(restaurant.getElementsByTagName("name").item( 0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“name”).item(0).getFirstChild().getNodeValue());

}%>
” maxlength=”40″ >

<input name="phone" class=stedit
style=”HEIGHT: 22px; WIDTH : 100px” value=”<%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“phone”).item(0). getFirstChild().getNodeValue());

}%>” maxlength=”20″ >

<input name ="address" class=stedit
style=”HEIGHT: 22px; WIDTH: 200px” value=”<%

if(restaurant.getElementsByTagName(“address”).item(0).hasChildNodes( )){
out.print(restaurant.getElementsByTagName(“address”).item(0).getFirstChild().getNodeValue());

}%>” maxlength=”100″ >

<img border=0
height=15 src=”../../images/editok.gif” width =15>

<% }else{
/ /Show normal format%>

<%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“name”).item(0).getFirstChild().getNodeValue());

}%>

<%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“phone” ).item(0).getFirstChild().getNodeValue());

}%>

< %
if(restaurant.getElementsByTagName(“address”).item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“address”).item(0).getFirstChild(). getNodeValue());

}%>

?act=modi&recordId=”><img border=0
height=15 src=”../../images/edit.gif” width=15>

<img border=0
height=15
OnClick=”Javascript:if(confirm(”Are you sure to delete this record? After deletion, it will lead to Record unavailable? ”)){window.location.href=”?act=del&recordId=”;}”
src=”../../images/delete.gif” style=”CURSOR: hand” width=15>

<% }
} %>
<% if (strOperation=="addnew"){
//Show the new format%>

?act=addnewDo” method=”post” OnSubmit=”return checkform2(this);” >

<input name="name" class=stedit
style=”HEIGHT: 22px; WIDTH: 150px” value=”” maxlength=”40″ >

<input name="phone" class=stedit
style=”HEIGHT: 22px; WIDTH: 100px” value=”” maxlength=” 20″ >

<input name="address" class=stedit
style=”HEIGHT: 22px; WIDTH: 200px” value =”” maxlength=”100″ >

<img border=0
height=15 src=”../%20%20../images/editok.gif” width=15>

<% if (strOperation=="addnew"){
%>

<% }else{
if(strOperation==”modi”){
%>

<%
}else{
%>
<input class=stbtm type= "button" name="Button" value="New" OnClick="Javascript:window.location.href=''?act=addnew”;”><%
}
} %>

《SCRIPT》
Description: The content of this document There are many places where the writing is not concise, because in the process of program development, overly concise programs often bring difficulties in later maintenance.

Development experience:

The return value of doc.getElementsByTagName(“restaurants”).item(int i) is node type. If you do not want to call its attribute value, there is no need to force conversion to Element type. Can be operated directly. Due to errors in the development reference materials, this system uses forced transformation. You can consider using node to operate directly in future development.

Neither trim() nor Interger.parseInt() functions can accept null values

The left and right files under tomcat are directories starting from TOMCAT. For details, please refer to \webapps How to write xml path in \canyin\jsp\userjudge.jsp.

For those who are interested in the original code, please contact me via the following email address, [email protected]
r>

<%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName( “phone”).item(0).getFirstChild().getNodeValue());

}%>

<%
if(restaurant.getElementsByTagName(“address”).item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName(“address”).item(0).getFirstChild ().getNodeValue());

}%>

?act=modi&recordId=”><img border=0
height=15 src=”../../images/edit.gif” width=15>

<img border=0
height=15
OnClick=”Javascript:if(confirm(”Are you sure to delete this record? After deletion Will the record be directed to unavailable?”)){window.location.href=”?act=del&recordId=”;}”
src=”../../images/delete.gif” style=”CURSOR: hand” width=15>

<% }
}%>
<% if (strOperation=="addnew"){
//Show the new format%>

?act=addnewDo” method=”post” OnSubmit=”return checkform2(this);” >

<input name="name" class=stedit
style=”HEIGHT: 22px; WIDTH: 150px” value=”” maxlength= “40” >

<input name="phone" class=stedit
style=”HEIGHT: 22px; WIDTH: 100px” value=”” maxlength=”20″ >

<input name="address" class=stedit
style=”HEIGHT: 22px; WIDTH: 200px” value=”” maxlength=”100″ >

<img border=0
height= 15 src=”../../images/editok.gif” width=15>

<% if (strOperation=="addnew"){
%>

<% }else{
if(strOperation==”modi”){
%>

<%
}else{
%>
<input class=stbtm type="button" name="Button" value="New" OnClick="Javascript:window.location.href=''?act=addnew” ;”><%
}
} %>

《SCRIPT》
Note: The writing of this document is not concise in many places, because in the process of program development, overly concise programs often bring difficulties in later maintenance.

Development experience:

The return value of doc.getElementsByTagName(“restaurants”).item(int i) is node type. If you do not want to call its attribute value, there is no need to force conversion to Element type. Can be operated directly. Due to errors in the development reference materials, this system uses forced transformation. You can consider using node to operate directly in future development.

Neither trim() nor Interger.parseInt() functions can accept null values

The left and right files under tomcat are directories starting from TOMCAT. For details, please refer to \webapps How to write xml path in \canyin\jsp\userjudge.jsp.

For those who are interested in the original code, please contact me via the following email address, [email protected]

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/719568

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索