struts 中到底如何操作session?我在两个Action中通过ActionContext分别获得session对象 CreatLoginImageAction中向session中存入一个字符串(该Action的result是一个生成验证码图片的jsp)然后在LoginAction中获得该字符串的时候却为空在控制台输出两个Action中获得session对象的Id 发现时不一样的难道不同的Action中不能维持同一个会话吗???我是一个刚刚接触struts的学生 应经被这个问题困扰好几天了 希望高手能给我指点 谢谢!
HttpSession httpSession = request.getSession();
session.setAttribute("loginStr", bf.toString());
该属性可以在该CreatLoginImageAction所指定的result(也就是creatloginimage.jsp)中被访问而另外一个LoginAction中就无法访问它这是LoginAction中用来访问session的代码 String loginStr=(String)session.getAttribute("loginStr");结果为null天啊 不知道为生命
//得到显示的数字
int high=37;
int width=30;
String loginStr=(String)session.getAttribute("loginStr");
if(loginStr==null) loginStr="1111"; //数字的长度
int len=loginStr.length(); //创建一个位于缓存中的图像 长为30*len 高为37
BufferedImage image=new BufferedImage(width*len,high,BufferedImage.TYPE_INT_RGB); //获得Graphics画笔
Graphics g=image.getGraphics();
g.setColor(new Color(240,243,248)); //背景颜色 //画一个黑色的矩形,长为30*len 高为37
g.fillRect(0, 0, width*len, high); g.setFont(font);
char c;
for(int i=0;i <len;i++){
g.setColor(new Color(83,0,108)); //字体颜色
c=loginStr.charAt(i);
g.drawString(c+"",i*width+7,27); //画数字
g.setColor(Color.white); //线颜色
g.drawLine((i+1)*width-1,0,(i+1)*width-1,high); //画白线
} //输出JPEG格式的图片 用这两句会发生异常
//JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
//encoder.encode(image); //设定响应正文的类型
/********************************^_^**************************************/
ServletOutputStream os=response.getOutputStream();//jsp默认声明PrintWriter对象out
response.reset(); //在jsp中是不行是因为必須在.getOutputStream();之前沒有任何信息輸出可以
response.setContentType("image/jpeg");
/******************************半小时!!!*********************************/
/********************************^_^**************************************/
Cookie cookie=new Cookie("JSESSIONID",session.getId());
response.addCookie(cookie);
/*****************************花了三天!!!*********************************/ session.setAttribute("1","1");
ImageIO.write(image,"JPEG",os);
os.close();
%> 加上这两句话就行了 Cookie cookie=new Cookie("JSESSIONID",session.getId());
response.addCookie(cookie);