Ajax Dynamic Update page

Source: Internet
Author: User

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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.