下午好,我正在尝试从HttpSession对象中的servlet恢复实例化的对象。
当我尝试从JSP恢复时,我顺利地获得了对象。但是,当我尝试从另一个Servlet检索此数据时,虽然会话ID是相同的,但不是我获取对象,而是直接返回NULL。
下面是我实例化要传递的对象的代码:
request.getSession(true);
request.getSession().setAttribute("object1", object1);这是尝试检索对象的代码。
req.getSession().getAttribute("object1");你能想到什么吗?
谢谢并致以问候。
发布于 2013-12-03 20:29:38
在way中,像这样将数据保存在会话中。
servlet中的session.setAttribute("object1", object1);和检索,如下所示
HttpSession session=request.getSession();
session.getAttribute("object1");发布于 2013-12-03 20:34:27
尝尝这个
清单3:存储对象
公共类logonServlet扩展了HttpServlet{
公共无效服务(HttpServletRequest _req,HttpServletRe-
_res)抛出ServletException{
ServletContext thisContext = getServletContext();
//--假设某个方法创建了一个新的连接类
Connection newConnection = createConnection();
thisContext.setAttribute( "database.connection",newConnection );
//--向客户端返回一些输出
}}
清单4:检索对象
公共类logoffServlet扩展了HttpServlet{
公共无效服务(HttpServletRequest _req,HttpServletRequest _res)抛出ServletException{
ServletContext thisContext = getServletContext();
//--假设某个方法创建了一个新的连接类
Connection newConnection = thisContext.getAttribute( "database.connection");
if ( newConnection == null )
//-数据库尚未打开
//--向客户端返回一些输出
}}
来源:http://www2.sys-con.com/itsg/virtualcd/java/archives/0505/williamson2/index.html
https://stackoverflow.com/questions/20351177
复制相似问题