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

原创 2013年09月07日 11:04:06

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]会话管理的概念、隐藏域、Cookie、URL重写

1. 会话管理的概念:     1) 背景:HTTP协议中,服务器无记忆,每次请求都是新的请求,是一种无状态通信协议,但是有些需求要求能记得前后请求之间的关系,比如用户在多个网页之间采购商品、网络笔试...

会话追踪--cookie/重写url/隐藏域/session

网络通信协议分类1.有状态的协议:TCP/IP,自从客户端与服务器连接上以后,这个连接会一直保持畅通,持续保持连接状态。 比如:打电话,从双方接通开始,一直到任何一方挂断电话位置,期间一直保持畅通。...

jsp网页session、cookie、隐藏域、url参数传递四种会话及跟踪方式

1.Session  HttpSession session = request.getSession();  session.setAttribute("name", "zhangsan"); ...

HttpServlet 会话管理(一)(URL重写 表单隐藏域 简单示例)

本文代码转载自 《Servlet、JSP和Spring MVC初学指南》 会话管理的目的是记录用户访问的状态, 使得之前的Http访问状态可以被之后的应用直接使用, 状态的记录即通过请求传值被...

ASP.NET状态管理之五:隐藏域、ViewState、ControlState

ASP.NET引入了ViewState(视图状态)的概念。从这个名字上我们大概可以体会出,ViewState主要是用来存放和视图有关的一些状态。比如,在用户注册时用户填写了一大堆数据,提交页面后系统返...
  • wn795
  • wn795
  • 2014年11月06日 16:25
  • 164

android 软键盘的隐藏域显示

  • 2012年12月29日 16:50
  • 3KB
  • 下载

servlet(二)--session管理---cookie

servlet--cookie import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet...

点击复选框出现隐藏域

  • 2012年10月03日 16:31
  • 1KB
  • 下载

js隐藏域显示与隐藏

  • 2014年02月17日 16:10
  • 3KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Servlet学习(二)__session管理__隐藏域
举报原因:
原因补充:

(最多只允许输入30个字)