2012-04-01 240 views
0

即时通讯尝试从我的servlet建立和访问会话,但我无法使它工作。从servlet访问JSP会话

所有的教程只是调用request.getSession(true);为会话对象,但我得到“请求无法解析”。

我完全必须使用taglibs,在jsp页面中没有任何逻辑。

如何访问会话数据? 谢谢!

package controller; 

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.jsp.*; 
import javax.servlet.http.*; 
import javax.servlet.jsp.tagext.*; 

public class Initialize extends BodyTagSupport 
{ 
    public int doEndTag() throws JspTagException 
    { 
     try 
     { 
      // Implementation 
      JspWriter out = pageContext.getOut(); 
      HttpSession session = request.getSession(true); 
      out.println(session.getId()); 
      return SKIP_BODY; 
     } 
     catch(IOException error) 
     { 
      throw new JspTagException(error); 
     } 
    } 
} 
+0

好吧,我想通了。 我不得不使用HttpSession session = pageContext.getSession(); 页面指令应该包含session =“true”。 – 2012-04-01 10:37:46

+0

页面指令不应该需要session =“true”指令,因为这是默认行为。除非你明确指定session =“false”,否则每个jsp页面都会为你创建一个会话。 – reevesy 2012-04-01 10:52:32

回答

0

从标签库会话数据的访问权例如本来:

package controller; 

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.jsp.*; 
import javax.servlet.http.*; 
import javax.servlet.jsp.tagext.*; 

public class Initialize extends BodyTagSupport 
{ 
    public int doEndTag() throws JspTagException 
    { 
     try 
     { 
      // Implementation 
      JspWriter out = pageContext.getOut(); 
      HttpSession session = pageContext.getSession(true); 
      out.println(session.getId()); 
      return SKIP_BODY; 
     } 
     catch(IOException error) 
     { 
      throw new JspTagException(error); 
     } 
    } 
}