Business logic: dynamically add employee information to the list. The list dynamically deletes employee information.
Page: employeeList. jsp
Employee list
Employee list
Employees:
Server: EmployeeListServlet. java
Package ajaxbook. chap4;
Import java. io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class EmployeeListServlet
Extends HttpServlet {
Private static final String CONTENT_TYPE = "text/html; charset = GBK ";
// Initialize global variables
Public void init () throws ServletException {
}
// Process the HTTP Get request
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Processing method parameters
String action = request. getParameter ("action ");
If (action. equals ("add ")){
AddEmployee (request, response );
} Else if (action. equals ("delete ")){
DeleteEmployee (request, response );
}
}
// Add employees
Protected void addEmployee (HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
// Obtain the primary key id
String uniqueID = storeEmployee ();
// Create a response string
StringBuffer xml = new StringBuffer (" ");
Xml. append (uniqueID );
Xml. append (" ");
Xml. append (" 1 ");
Xml. append (" ");
// Send
SendResponse (response, xml. toString ());
}
// Delete an employee
Protected void deleteEmployee (HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
// Obtain the parameter id.
String id = request. getParameter ("id ");
// Create a response string
StringBuffer xml = new StringBuffer (" > ");
Xml. append (" 1 ");
Xml. append (" ");
// Send
SendResponse (response, xml. toString ());
}
// Send response strings
Private void sendResponse (HttpServletResponse response, String responseText) throws IOException {
Response. setContentType ("text/xml ");
Response. getWriter (). write (responseText );
}
// Simulate the database to obtain the primary key id
Private String storeEmployee (){
String uniqueID = "";
Random randomizer = new Random (System. currentTimeMillis ());
For (int I = 0; I <8; I ++ ){
UniqueID + = randomizer. nextInt (9 );
}
Return uniqueID;
}
}