从servlet到servlet,HttpSession Servlet无法正常工作

时间:2013-12-03 12:26:38

标签: java servlets httpsession

下午好,我正在尝试从HttpSession对象中的servlet恢复实例化对象。

当我尝试从JSP中恢复时,我可以顺利地获得对象。 但是当我尝试从另一个Servlet中检索这些数据时,不是我获取对象,它直接返回NULL,尽管会话ID是相同的。

这是我实例化要传递的对象的代码:

request.getSession(true);
request.getSession().setAttribute("object1", object1);

这是尝试检索对象的代码。

req.getSession().getAttribute("object1");

你能想到什么吗?

谢谢和问候。

2 个答案:

答案 0 :(得分:0)

在jspkeep会话中的数据就像这样

session.setAttribute("object1", object1);并以这种方式在servlet中检索

HttpSession session=request.getSession();
session.getAttribute("object1");

答案 1 :(得分:-1)

试试这个

清单3:存储对象

public class logonServlet扩展了HttpServlet {

public void service(HttpServletRequest _req,HttpServletRe-

sponse _res)抛出ServletException {

ServletContext thisContext = getServletContext();

// - 假设某个方法创建了一个新的连接类

Connection newConnection = createConnection();

thisContext.setAttribute(“database.connection”,newConnection);

// - 将一些输出返回给客户端

} }

清单4:检索对象

public class logoffServlet扩展了HttpServlet {

public void service(HttpServletRequest _req,HttpServletRe- sponse _res)抛出ServletException {

ServletContext thisContext = getServletContext();

// - 假设某个方法创建了一个新的连接类

连接newConnection = thisContext.getAttribute( “database.connection”);

if(newConnection == null)

// - 数据库尚未打开

// - 将一些输出返回给客户端

} }

来源:http://www2.sys-con.com/itsg/virtualcd/java/archives/0505/williamson2/index.html

相关问题