白桦的天空

第一次的心动,永远的心痛!
  首页  :: 新随笔  :: 联系 :: 管理

用session保持一个数组

Posted on 2008-05-04 16:54  白桦的天空  阅读(458)  评论(0编辑  收藏  举报

public class Person {
 
   private  String empname="";
   private  String empno="";
  
  public void setEmpName(String empname)
  {
     this.empname=empname;
  }
 
  public String getEmpName()
  {
    return this.empname;
  }
 
 
  public void setEmpNo(String empno)
  {
      this.empno=empno;
  }
 
  public String getEmpNo()
  {
     return this.empno;
  }
 

}

public class Person {
 
   
private  String empname="";
   
private  String empno="";
   
  
public void setEmpName(String empname)
  
{
     
this.empname=empname;
  }

  
  
public String getEmpName()
  
{
    
return this.empname;
  }

  
  
  
public void setEmpNo(String empno)
  
{
      
this.empno=empno;
  }

  
  
public String getEmpNo()
  
{
     
return this.empno;
  }

 

}

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class listpage extends HttpServlet {
 private static final long serialVersionUID = 2L;
 /**
  * Constructor of the object.
  */
 public listpage() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is ");
  out.print(this.getClass());
  out.println(", using the GET method<br>");
  ArrayList resultlist = new ArrayList();
  Person person = new Person();
        person.setEmpNo("编码");
        person.setEmpName("名字");
        resultlist.add(person);
        Person person1=(Person)resultlist.get(0);
     out.println(person1.getEmpName());
     HttpSession session = request.getSession();
     session.setAttribute("Mysession",new ArrayList(resultlist));
     ArrayList all = (ArrayList) session.getAttribute("Mysession");
     Person person2=(Person)all.get(0);
     out.println("<br>");
     out.println(person2.getEmpNo());
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is ");
  out.print(this.getClass());
  out.println(", using the POST method");
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  // Put your code here
 

 }

}


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class listpage extends HttpServlet {
    
private static final long serialVersionUID = 2L;
    
/**
     * Constructor of the object.
     
*/

    
public listpage() {
        
super();
    }


    
/**
     * Destruction of the servlet. <br>
     
*/

    
public void destroy() {
        
super.destroy(); // Just puts "destroy" string in log
        
// Put your code here
    }


    
/**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

        response.setContentType(
"text/html;charset=gb2312");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(
"  <BODY>");
        out.print(
"    This is ");
        out.print(
this.getClass());
        out.println(
", using the GET method<br>");
        ArrayList resultlist 
= new ArrayList();
        Person person 
= new Person();
        person.setEmpNo(
"编码");
        person.setEmpName(
"名字");
        resultlist.add(person);
        Person person1
=(Person)resultlist.get(0);
        out.println(person1.getEmpName());
        HttpSession session 
= request.getSession();
        session.setAttribute(
"Mysession",new ArrayList(resultlist));
        ArrayList all 
= (ArrayList) session.getAttribute("Mysession");
        Person person2
=(Person)all.get(0);
        out.println(
"<br>");
        out.println(person2.getEmpNo());
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
    }


    
/**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

        response.setContentType(
"text/html;charset=gb2312");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(
"  <BODY>");
        out.print(
"    This is ");
        out.print(
this.getClass());
        out.println(
", using the POST method");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
    }


    
/**
     * Initialization of the servlet. <br>
     *
     * 
@throws ServletException if an error occure
     
*/

    
public void init() throws ServletException {
        
// Put your code here
    

    }


}