An example of development using JSP + JAVABEAN + XML
Author:Eve Cole
Update Time:2009-07-02 17:12:18
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 I had with colleagues at my old 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/ -----Home directory
/tomcat/webapps/canyin/jsp/ -----JSP file directory
/tomcat/webapps/canyin/jsp/admin/ -----The storage directory for JSP files that implement background management
/tomcat/webapps/canyin/WEB-INF/classes/canyin/ ------The storage directory of javabean files
/tomcat/webapps/canyin/data/ -----xml file storage directory
/tomcat/webapps/ROOT/ -----Tomcat startup file storage folder, only the index.html file is stored
Brief description of the file:
/tomcat/webapps/canyin/data/users.xml -----Record user information
/tomcat/webapps/canyin/data/restaurants.xml -----Record basic information of restaurants
/tomcat/webapps/ROOT/index.html -----Home page, an input box appears on the page, requiring the user to enter a username 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, manages 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 indicates the user's identity. If it is not an 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 to the file and accompanying code description.
/tomcat/webapps/canyin/data/users.xml
Code:
<?xml version="1.0" encoding="UTF-8" ?>
- <users>
<user name="joard" password="joard" roles="admin" />
<user name="joard01" password="joard01" roles="user" />
<user name="joard02" password="joard02" roles="user" />
</users>
Description: The meaning of the fields is user name, password and user’s identity
/tomcat/webapps/canyin/data/restaurants.xml
Code:
<?xml version="1.0" encoding="UTF-8" ?>
- <restaurants num="10">
- <restaurant id="1">
<name>Shanghai Ting Fast Food Restaurant</name>
<phone>021-76546726</phone>
<address>Broadway Plaza Tower B</address>
</restaurant>
- <restaurant id="8">
<name>Shangri-La Hotel</name>
<phone>021-2312134</phone>
<address>No. 1023, Nanjing Road</address>
</restaurant>
</restaurants>
Note: The <num> attribute records the total number of records in the restaurants.xml file. Every time a new record is added, the value will increase by 1 regardless of whether it is deleted later, just like the id that is used to automatically increase by 1 in the database. item. Used to assign a unique value to the attribute <id> of the newly added <restaurant>. The meaning of other fields is more obvious.
/tomcat/webapps/ROOT/index.html (pure HTML code)
Code:
<html>
<head>
<title>oddWorld Catering System</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body onload="javascript:dataform.username.focus()">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> Catering system login </td>
<td><img src="images/top_r2.GIF" width="294" height="22"></td>
</tr>
</table>
<br>
<br>
<table width="300" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td height="200" valign="top" align="center">
<p align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor=#999999 class=a9px>
<tr>
<td bgcolor="#efefef">Catering system login</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form name=dataform method=post action=''canyin/jsp/loginjudge.jsp''>
<tr>
<td width="100"><b>Login name:</b></td>
<td>
<input maxlength=16
name="username" class=stedit value="joard">
</td>
</tr>
<tr>
<td width="100"><b>Password:</b></td>
<td>
<input class=stedit maxlength=16
name="userpass" type=password value="oddworld">
</td>
</tr>
</form>
</table>
<br>
<table border=0 cellpadding=0 cellspacing=0>
<tbody>
<tr>
<td>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value="Login">
</td>
<td> </td>
<td>
<input class=stbtm name=Submit onClick="javascript:window.location.href=''index.asp?myjoke=1'';" type=button value="Change password">
</td>
<td> </td>
</tr>
</tbody>
</table>
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
<SCRIPT language=javascript>
<!--
function checkform()
{
var Checkblank = /^(s*|( )|(.))*$/;
if (Checkblank.test(dataform.username.value))
{
alert("Login name cannot be empty!");
return false;
}
if (Checkblank.test(dataform.userpass.value))
{
alert("Password cannot be empty!");
return false;
}
window.dataform.submit();
}
-->
</SCRIPT>
Instructions: Submit the user name and user password to /tomcat/webapps/canyin/jsp/loginjudge.jsp
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class (the code is the corresponding java file)
package canyin;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
public class checkSessionBean {
private boolean bolCheckPass=false;
private HttpServletRequest request = null;
public boolean checkSessionBean(HttpServletRequest request,String strSessionName,String strCheckValue){
public boolean checkSessionBean(HttpServletRequest request){
HttpSession session = request.getSession(false);
return(bolCheckPass);
if (strSessionName==null || strCheckValue==null){
return(bolCheckPass);
}else{
if (session!=null && session.getValue(strSessionName)!=null){
bolCheckPass=session.getValue(strSessionName).equals(strCheckValue);
}
return(bolCheckPass);
}
}
}
Description: Check whether the value of the session name passed in as a parameter and the value of the field passed in as a parameter are equal.
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class
Code:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class connXmlBean {
private DocumentBuilderFactory factory=null;
private DocumentBuilder builder=null;
private Document doc=null;
public connXmlBean(){}
public String connXml(String xmlFileName){
String strExc="";
try{
factory = DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
doc=builder.parse(xmlFileName);
doc.normalize();
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
public Document getXmlDoc(){
return(doc);
}
}
Description: Open a specified xml file
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class
Code:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import org.w3c.dom.*;
public class writeXmlBean {
public writeXmlBean(){}
public String writeXml(Document doc,String xmlFileName){
String strExc="";
try{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(xmlFileName));
transformer.transform(source,result);
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
}
Description: Write the content of dom to a specified xml file.
/tomcat/webapps/canyin/jsp/loginjudge.jsp
Code:
<%-- oddWorld Catering Management System (Simplified Chinese version) December 1, 2002
copy right by joard ast
loginjudge.jsp function: user identity verification, based on the different identities of the users marked in the /data/user.xml file
Decide to go to the backend management page or the customer ordering page.
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<%
session.setMaxInactiveInterval(1800);
Document doc;
NodeList users;
String strExc="";
String strUsername,strPassword;
strUsername=(String)request.getParameter("username");
strPassword=(String)request.getParameter("userpass");
//Check whether the data is empty
if (strUsername=="" || strPassword=="" ){
out.println("<script language=''javascript''>");
out.println("alert(''Username or password has a null value!'');");
out.println("window.location.href=''/index.html'';");
out.println("</script>");
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("<script language=''javascript''>");
out.println("alert(''Welcome administrator to log in to the system!'');");
out.println("</script>");
//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, and the administrator's identity is user
session.setAttribute("sesUserRole","user");
//Jump to normal user page
response.sendRedirect("index.jsp");
return;
}
}else{
out.println("<script language=''javascript''>");
out.println("alert(''Wrong username or password!'');");
out.println("history.go(-1);");
out.println("</script>");
return;
}
}
}catch(Exception e){
strExc=e.toString();
}
%>
illustrate:.......
/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.
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<%@ include file="../../include/sys_dialog.jsp" %>
<jsp:useBean id="checkSessionBean" class="canyin.checkSessionBean" scope="page" />
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<jsp:useBean id="writeXmlBean" class="canyin.writeXmlBean" scope="page" />
<%//Verify the identity of the user to determine whether he or she is an administrator
if(!checkSessionBean.checkSessionBean(request,"sesUserRole","admin")){
out.print(showDialog("You do not have administrative rights!","/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 passed in
strAct=(String)request.getParameter("act");
xmlBean.connXml("webapps/canyin/data/restaurants.xml");
doc=xmlBean.getXmlDoc();
restaurants =doc.getElementsByTagName("restaurant");
//Determine the operation of the restaurant.xml file based on external parameters passed in
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("The restaurant 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("Repeat 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));
//Add 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 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("Repeat restaurant phone number!"));
return;
}
}
}
if(!recordExist){
out.print(showDialog("The record of the restaurant you want to modify does not exist!"));
return;
}else{
//Perform record changes
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 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())==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 status of the page is determined by external parameters.
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 the record is empty, change the page status to "New"
if (restaurants.getLength()==0){
strOperation="addnew";
}
%>
<html>
<head>
<title>oddWorld Catering System</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" href="../../include/itsp.css" type="text/css">
</head>
<body>
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="../../images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> Catering system management--restaurant management</td>
<td><img src="../../images/top_r2.GIF" width="294" height="22"></td>
<td width=100 align="center"><a href="/index.html">[Exit system]</a></td>
</tr>
</table>
<br>
<br>
<table bgcolor="#999999" align=center border=0 cellpadding=1 cellspacing=1
width="90%">
<tbody>
<tr bgcolor="#efefef" align="center" valign="middle">
<td class=ttTable height=30 width="20"> </td>
<td class=ttTable height=30 width="0">Restaurant name</td>
<td class=ttTable height=30 width="0">Restaurant phone number</td>
<td class=ttTable height=30 width="0">
<div align="center">Restaurant address</div>
</td>
<td class=ttTable height=30 width="30">
<div align="center">Modify</div>
</td>
<td class=ttTable height=30 width="30">
<div align="center">Delete</div>
</td>
</tr>
<%
for(int i=0;i<restaurants.getLength();i++)
{
Element restaurant=(Element) restaurants.item(i);
if (strOperation=="modi" && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==intId){
%>
<%//Show modified format%>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform action="<%=request.getRequestURI()%>?act=modiDo" method="post" onSubmit=''return checkform(this);'' >
<td class=tdsmall height=25 width="20">
<input type="hidden" name="recordId" value="<%=restaurant.getAttributeNode("id").getNodeValue()%>">
<%=(i+1)%></td>
<td class=tdsmall height=25>
<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" >
</td>
<td class=tdsmall height=25>
<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" >
</td>
<td class=tdsmall height=25>
<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" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform()==false);"><img border=0
height=15 src="../../images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% }else{
//Display normal format %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<td class=tdsmall height=25 width="20"><%=(i+1)%></td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
}%></td>
<td class=tdsmall height=25 width="0">
<%
if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="30"><a href="<%=request.getRequestURI()%>?act=modi&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()% >"><img border=0
height=15 src="../../images/edit.gif" width=15></a></td>
<td class=tdsmall height=25 width="30"><img border=0
height=15
onClick="javascript:if(confirm(''Are you sure to delete this record? Deletion will make the record unusable?'')){window.location.href=''<%=request.getRequestURI()%> ?act=del&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()%>'';}"
src="../../images/delete.gif" style="CURSOR: hand" width=15> </td>
</tr>
<%}
}%>
<% if (strOperation=="addnew"){
//Show the new format%>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform2 action="<%=request.getRequestURI()%>?act=addnewDo" method="post" onSubmit=''return checkform2(this);'' >
<td class=tdsmall height=25 width="20"></td>
<td class=tdsmall height=25>
<input name="name" class=stedit
style="HEIGHT: 22px; WIDTH: 150px" value="" maxlength="40" >
</td>
<td class=tdsmall height=25>
<input name="phone" class=stedit
style="HEIGHT: 22px; WIDTH: 100px" value="" maxlength="20" >
</td>
<td class=tdsmall height=25>
<input name="address" class=stedit
style="HEIGHT: 22px; WIDTH: 200px" value="" maxlength="100" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform2()==false);"><img border=0
height=15 src="../../images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% } %>
</tbody>
</table>
<br>
<table align=center border=0 cellpadding=0 cellspacing=2 width="95%">
<tbody>
<tr valign=center>
<td align=middle> <br>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td>
<% if (strOperation=="addnew"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform2()==false);" type=button value="Update record">
<% }else{
if(strOperation=="modi"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value="Update record">
<%
}else{
%>
<input class=stbtm type="button" name="Button" value="New" onClick="javascript:window.location.href=''<%=request.getRequestURI()%>?act=addnew'' ;"><%
}
} %>
</td>
<td>
<input class=stbtm type="button" name="Button" value="Return" onClick="javascript:window.location.href=''index.jsp'';">
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
</div>
</body>
</html>
<SCRIPT LANGUAGE=javascript>
<!--
function checkform2()
{
var Checkblank = /^(s*|( )|(.))*$/;
if (Checkblank.test(dataform2.name.value))
{
alert("The restaurant name cannot be empty!");
dataform2.name.focus();
return false;
}
if (Checkblank.test(dataform2.phone.value))
{
alert("The restaurant phone number cannot be empty!");
dataform2.phone.focus();
return false;
}
window.dataform2.submit();
}
function checkform()
{
var Checkblank = /^(s*|( )|(.))*$/;
if (Checkblank.test(dataform.name.value))
{
alert("The restaurant name cannot be empty!");
dataform.name.focus();
return false;
}
if (Checkblank.test(dataform.phone.value))
{
alert("The restaurant phone number cannot be empty!");
dataform.phone.focus();
return false;
}
window.dataform.submit();
}
-->
</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 of node type. If you do not want to call its attribute value, there is no need to cast it 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 accept null values.
The left and right files under tomcat are directories starting from TOMCAT. For details, please refer to webappscanyinjspuserjudge.jsp for the writing of xml path.
Friends who are interested in the original code please contact me through the following email address, [email protected]