不需要单独不熟web.xml
Customer类
package app02a.hiddenfields;
public class Customer {
private int id;
private String name;
private String city;
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
}
CustomerServlet类
package app02a.hiddenfields;
import org.omg.CORBA.CurrentOperations;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
@WebServlet(name="CustomerServlet",urlPatterns = {
"/customer","/editCustomer","/updateCustomer"
})
public class CustomerServlet extends HttpServlet {
private static final long servialVersionID = 22L;
private List<Customer> customers = new ArrayList<Customer>();
@Override
public void init() throws ServletException{
Customer customerA = new Customer();
customerA.setId(1);
customerA.setName("David");
customerA.setCity("Xian");
customers.add(customerA);
Customer customerB = new Customer();
customerB.setId(2);
customerB.setName("Green");
customerB.setCity("shanghai");
customers.add(customerB);
}
private void sendCustomerList(HttpServletResponse response)
throws IOException{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html><head><title>Customer</title></head>"
+ "<body><h2>Customer</h2>");
writer.println("<ul>");
for(Customer customer:customers){
writer.println("<li>" + customer.getName() +
"("+customer.getCity()+")"+
"("+"<a href=editCustomer?id="+customer.getId()+">edit</a>" +")");
}
writer.println("</ul>");
writer.println("</body></html>");
}
private Customer getCustomer(int customerId){
for(Customer customer : customers) {
if (customer.getId() == customerId) {
return customer;
}
}
return null;
}
private void sendEditCustomerForm(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
int customerId = 0;
try {
customerId =
Integer.parseInt(request.getParameter("id"));
} catch (NumberFormatException e) {
}
Customer customer = getCustomer(customerId);
if (customer != null) {
writer.println("<html><head>"
+ "<title>Edit Customer</title></head>"
+ "<body><h2>Edit Customer</h2>"
+ "<form method='post' " + "action='updateCustomer'>"
);
writer.println("<input type='hidden' name='id' value='"
+ customerId + "'/>");
writer.println("<table>");
writer.println("<tr><td>Name:</td><td>"
+ "<input name='name' value='"
+ customer.getName().replaceAll("'", "'")
+ "'/></td></tr>");
writer.println("<tr><td>City:</td><td>"
+ "<input name='city' value='"
+ customer.getCity().replaceAll("'", "'")
+ "'/></td></tr>");
writer.println("<tr>"
+ "<td colspan='2' style='text-align:right'>"
+ "<input type='submit' value='Update' /></td>"
+ "</td></tr>");
writer.println("<tr><td colspan='2'>"
+ "<a href='customer'>Customer List</a>"
+ "</td></tr>");
writer.println("</table>");
writer.println("</form></body>");
} else {
writer.println("No customer found");
}
}
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
String uri = request.getRequestURI();
if (uri.endsWith("/customer")){
sendCustomerList(response);
}else if(uri.endsWith("/editCustomer")){
sendEditCustomerForm(request,response);
}
}
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
int customerId = 0;
try {
customerId = Integer.parseInt(request.getParameter("id"));
}catch (NumberFormatException e){
}
Customer customer =getCustomer( customerId);
if(customer != null){
customer.setName(request.getParameter("name"));
customer.setCity(request.getParameter("city"));
}
sendCustomerList(response);
}
}