Session问题(在线等待)
前台用的是Application,与后台进行交互,后台传往前台的数据须在后台保留,现存在SESSION中,可下次再取SESSION时,里面的数据却是空的。哪位能给予帮助,或提出合理化建议。 问题点数:50、回复次数:10Top
1 楼chesterwoo()回复于 2002-05-16 09:42:43 得分 5
有可能你的session变量在其它地方进行了置空的操作。Top
2 楼broze()回复于 2002-05-16 09:50:39 得分 5
把session的时间设的长一点啊。Top
3 楼weidegong(weidegong)回复于 2002-05-16 09:50:42 得分 0
超时?Top
4 楼windyloft(大后天)回复于 2002-05-16 10:02:31 得分 0
gzTop
5 楼MagicJohn(默克)回复于 2002-05-16 10:05:13 得分 0
cookie中可以保存需要长期保存的数据。Top
6 楼skyyoung(路人甲)回复于 2002-05-16 10:22:50 得分 0
你的Session是什么类?前台的APP是什么类型的APP?
---------------
shmilu@sina.comTop
7 楼chenf_(晨晨)回复于 2002-05-16 10:48:39 得分 0
前台代码:
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中的值。Top
8 楼chenf_(晨晨)回复于 2002-05-16 10:56:45 得分 0
而我将两个按钮使用一个连接时,当点击OK按钮时出现以下错误:
java.net.ProtocolException: Cannot write output after reading input.Top
9 楼skyyoung(路人甲)回复于 2002-05-16 15:40:02 得分 40
你前台用SWING,用URLCONNECTION ,不能记录一些SESSION值到,你要用HTTPCLIENT类做你访问Internet的包。
http://www.innovation.ch/java/HTTPClient/Top
10 楼chenf_(晨晨)回复于 2002-05-17 09:15:44 得分 0
skyyoung(路人甲) :可是我做的程序前台就是这种形式的。不知能否实现我要达到的结果呢?谢谢。Top
11 楼chenf_(晨晨)回复于 2002-05-17 09:21:00 得分 0
另外,如果用你所说的类来实现的话,能否给点代码提示呢。先谢了。Top




