Servlet学习(二)__session管理__隐藏域

Servlet学习(二)__session管理__隐藏域

下面的示例通过隐藏域传递一个id值,修改某个值。
一.javabean代码.
package com.cdc.servlettest.bean;
public class Customer {
    private int id;
    private String name;
    private String city;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
}

二.servlet代码。
package com.cdc.servlettest.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cdc.servlettest.bean.Customer;

/*
 * Not thread-safe. For illustration purpose only
 */
@WebServlet(name = "CustomerServlet", urlPatterns = { 
        "/customer", "/editCustomer", "/updateCustomer"})
public class CustomerServlet extends HttpServlet {
    private static final long serialVersionUID = -20L;

    private List<Customer> customers = new ArrayList<Customer>();
    
    @Override
    public void init() throws ServletException {
        Customer customer1 = new Customer();
        customer1.setId(1);
        customer1.setName("张飞");
        customer1.setCity("成都");
        customers.add(customer1);
        
        Customer customer2 = new Customer();
        customer2.setId(2);
        customer2.setName("刘备");
        customer2.setCity("西安");
        customers.add(customer2);       
    }
    
    private void sendCustomerList(HttpServletResponse response)
            throws IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<html><head><title>客户</title></head>"
                + "<body><h2>客户 </h2>");
        writer.println("<ul>");
        /**从初始化的集合里取出数据,显示在界面上。*/
        for (Customer customer : customers) {
            writer.println("<li>" + customer.getName()
                   + "(" + customer.getCity() + ") (" 
                   + "<a href='editCustomer?id=" + customer.getId()
                   + "'>修改</a>)");
        }
        writer.println("</ul>");
        writer.println("</body></html>");
        
    }
    /****通过id值获取相应的记录*/
    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");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        int customerId = 0;
        try {
        	/**接收通过url重写传递过来的参数id的值**/
            customerId = 
                    Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        Customer customer = getCustomer(customerId);
        /***此表单通过post提交后,将调用此Servlet的doPost方法*/
        if (customer != null) {
            writer.println("<html><head>"
                    + "<title>修改客户资料</title></head>"
                    + "<body><h2>修改客户资料</h2>"
                    + "<form method='post' "
                    + "action='updateCustomer'>");
            /**隐藏域,传递id**/
            writer.println("<input type='hidden' name='id' value='" 
                    + customerId + "'/>");
            writer.println("<table>");
            writer.println("<tr><td>姓名:</td><td>" 
                    + "<input name='name' value='" + 
                    customer.getName().replaceAll("'", "'") 
                    + "'/></td></tr>");
            writer.println("<tr><td>城市:</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>" 
                    + "</tr>");
            writer.println("<tr><td colspan='2'>"
                    + "<a href='customer'>客户列表</a>" 
                    + "</td></tr>");
            writer.println("</table>");
            writer.println("</form></body>");
        } else {
            writer.println("没有此客户");
        }
        
    }
    @Override
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
        String uri = request.getRequestURI();
        response.setCharacterEncoding("utf-8");
        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 {
    	
        // update customer
    	response.setCharacterEncoding("utf-8");
    	request.setCharacterEncoding("utf-8");
        int customerId = 0;
        try {
        	/***接收隐藏域传递过来的id值*/
            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);
    }
}



阅读更多
个人分类: Servlet
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭