有可能你的session变量在其它地方进行了置空的操作。

解决方案 »

  1.   

    把session的时间设的长一点啊。
      

  2.   

    cookie中可以保存需要长期保存的数据。
      

  3.   

    你的Session是什么类?前台的APP是什么类型的APP?---------------
    [email protected]
      

  4.   

    前台代码:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    class Frame2 extends JFrame {
      JPanel contentPane;
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();  /**Construct the frame*/
      public Frame2() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
      
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception  {
        jButton1.setText("happy");
        jButton1.setBounds(new Rectangle(135, 86, 79, 29));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
          }
        });
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(null);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        jButton2.setText("ok");
        jButton2.setBounds(new Rectangle(137, 152, 79, 29));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton2_actionPerformed(e);
          }
        });
        contentPane.add(jButton1, null);
        contentPane.add(jButton2, null);
      }
      /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  void jButton1_actionPerformed(ActionEvent e) {
      try
    {
         java.net.URL url = new java.net.URL(
      "http://localhost:8080/examples/servlet/DataStreamEcho");   
      java.net.URLConnection con = url.openConnection();
      con.setUseCaches(false);
      con.setDoOutput(true);
      con.setDoInput(true);
    ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
    System.out.println("Writing test data");
    out.writeObject(jButton1.getText());
    out.flush();
                out.close();
    ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    Vector stringValue = (Vector)in.readObject();
    in.close();
    for(int i=0;i<stringValue.size();i++)
    System.out.println((String)stringValue.elementAt(i));
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }  }  void jButton2_actionPerformed(ActionEvent e) {
      try
    {
      java.net.URL url = new java.net.URL(
      "http://localhost:8080/examples/servlet/DataStreamEcho");   
      java.net.URLConnection con = url.openConnection();
      con.setUseCaches(false);
      con.setDoOutput(true);
      con.setDoInput(true);
    ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
    System.out.println("Writing test data");
    out.writeObject(jButton2.getText());
    out.flush();
                out.close();
    ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    Vector stringValue = (Vector)in.readObject();
    in.close();
    for(int i=0;i<stringValue.size();i++)
    System.out.println((String)stringValue.elementAt(i));
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }  }
    }
    public class Frame1
    {
    public static void main(String[] args)
    {
    Frame frame = new Frame2();
    frame.setSize(400,300);
    frame.show();
    }
    }
    后台代码:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;public class DataStreamEcho extends HttpServlet{


    public void service(HttpServletRequest req,HttpServletResponse resp)
    throws ServletException,IOException
    {   Vector vec = new Vector();
    try{

    ObjectInputStream in = new ObjectInputStream(req.getInputStream());
    ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream());
    resp.setContentType("application/octet-stream"); //HttpSession session = req.getSession(true);

    String aa = (String)in.readObject();
        //session.setAttribute(aa,"hello hello");

    if(aa.equals("happy"))
    {
    vec.addElement("cc");
    vec.addElement("aa");
    String qqqq=(String)getServletContext().getAttribute(aa);
    if(qqqq == null)
    getServletContext().setAttribute(aa,"change is ok");
    String kkkk = (String)getServletContext().getAttribute(aa);
    vec.addElement(kkkk);
    out.writeObject(vec);
    out.flush();
    out.close();
    }
    else if(aa.equals("ok"))
    {
    vec.addElement("kk");
    vec.addElement("ss");
    String pppp = (String)getServletContext().getAttribute(aa);
    vec.addElement(pppp);
    out.writeObject(vec);
    out.flush();
    out.close();
    } }catch(Exception e)
    {
    }

    }
    }
    点击OK按钮时,取不出来点HAPPY按钮时存在SESSION中的值。
      

  5.   

    而我将两个按钮使用一个连接时,当点击OK按钮时出现以下错误:
    java.net.ProtocolException: Cannot write output after reading input.
      

  6.   

    你前台用SWING,用URLCONNECTION ,不能记录一些SESSION值到,你要用HTTPCLIENT类做你访问Internet的包。
    http://www.innovation.ch/java/HTTPClient/
      

  7.   

    skyyoung(路人甲) :可是我做的程序前台就是这种形式的。不知能否实现我要达到的结果呢?谢谢。