我在描述以下我的问题:
步骤:
1.用Servlet访问有状态会话Been给绘画Been的Vector里写入值并且已经写入正确
2.用另外一个Servlet访问刚才的会话Been可是为什么Vector里为空

解决方案 »

  1.   

    我可以给你指出不对处
    在GetCarServlet中
    myCarHomeObject = (MyCarHome) PortableRemoteObject.narrow(ref, MyCarHome.class);
    myCarObject = myCarHomeObject.create();
    不正确;
    改为
    MyCarServlet 中加入
    session.setAttribute("shoppingCart", myCarObject);
    在GetCarServlet中使用
    myCarObject = (MyCar)session.getAttribute("shoppingCart");
    try{
    if(myCarObject!=null || myCarObject.getCarInfo()){
      System.out.println("stored goods in this cart is exist!!!");
    }
    catch(Exception e){
      //get a new cart without goods
      myCarHomeObject = (MyCarHome) PortableRemoteObject.narrow(ref, MyCarHome.class);
      myCarObject = myCarHomeObject.create();
    }
    java.util.Vector bookInfo;
          bookInfo = myCarObject.getCarInfo();
          //Object ob[];
          //ob = bookInfo.toArray();
          Book carBook;
          carBook = (Book)bookInfo.get(0);
          out.println(carBook.BOOKNAME);//打印不出来
          out.println(carBook.ID);)//打印不出来
          //for(int i=0;i<ob.length;i++){
            //carBook = (Book)ob[i];
            //out.println(carBook.BOOKNAME);
         // }
    ok le
      

  2.   

    有几次错误,无法通过编译,其它还不知道,下面是改正后的
    package bookstoreejb;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import javax.naming.*;
    import javax.rmi.*;public class MyCarServlet 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 {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        String ls_id = "1";
        String url = "t3://127.0.0.1:7001";
        MyCarHome myCarHomeObject = null;
        MyCar myCarObject = null;
        java.util.Vector lc_vectorBook;
        Book lc_Book;
        Properties properties = null;
        Context context = null;
        properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,
                       "weblogic.jndi.WLInitialContextFactory");
        properties.put(Context.PROVIDER_URL, url);    try{
          context = new InitialContext(properties);
          Object ref = context.lookup("MyCar");
          myCarHomeObject = (MyCarHome) PortableRemoteObject.narrow(ref, MyCarHome.class);
          myCarObject = myCarHomeObject.create();
          boolean good = myCarObject.addBook(ls_id);//添加到Vector
          lc_vectorBook = myCarObject.getCarInfo();//返回添加后的Vector
          lc_Book = (Book)lc_vectorBook.get(0);
          out.println("outname="+lc_Book.BOOKNAME);//信息已经打出
          out.println("outid="+lc_Book.ID);//信息已经打出
          if (good){
            out.println("已经将书名为:《"+lc_Book.BOOKNAME+"》书籍,加入购物车!");
            out.println("<meta http-equiv=refresh content=\"5;url=./tsxx.jsp?ifquest=false=false\">");
          }else{
            out.println("书名为:《"+lc_Book.BOOKNAME+"》书籍,您已经加入了购物车,不需要在加入!");
            out.println("<meta http-equiv=refresh content=\"5;url=./tsxx.jsp?ifquest=false=/\">");
          }    }catch(Exception e){
          e.printStackTrace();
        }
      }
      //Clean up resources
      public void destroy() {
      }
    }package bookstoreejb;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import javax.naming.*;
    import javax.rmi.*;public class GetCarServlet 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 {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        String url = "t3://127.0.0.1:7001";
        MyCarHome myCarHomeObject = null;
        MyCar myCarObject = null;
        Properties properties = null;
        Context context = null;
        properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,
                       "weblogic.jndi.WLInitialContextFactory");
        properties.put(Context.PROVIDER_URL, url);
        try{
          context = new InitialContext(properties);
          Object ref = context.lookup("MyCar");
          myCarHomeObject = (MyCarHome) PortableRemoteObject.narrow(ref, MyCarHome.class);
          myCarObject = myCarHomeObject.create();
          java.util.Vector bookInfo;
          bookInfo = myCarObject.getCarInfo();
          //Object ob[];
          //ob = bookInfo.toArray();
          Book carBook;
          carBook = (Book)bookInfo.get(0);
          out.println(carBook.BOOKNAME);//打印不出来
          out.println(carBook.ID);//打印不出来
          //for(int i=0;i<ob.length;i++){
            //carBook = (Book)ob[i];
            //out.println(carBook.BOOKNAME);
         // }    }catch(Exception e){
          e.printStackTrace();
        }  }
      //Clean up resources
      public void destroy() {
      }
    }