【问题标题】:Accessing JSP session from servlet从 servlet 访问 JSP 会话
【发布时间】:2012-04-01 09:59:13
【问题描述】:

我试图从我的 servlet 建立和访问会话,但我无法让它工作。

所有教程只需调用 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);
        }
    }
}

【问题讨论】:

  • 好的,我想通了。我不得不使用 HttpSession session = pageContext.getSession();并且页面指令应该包含 session="true"。
  • 页面指令不应该需要 session="true" 指令,因为这是默认行为。除非您明确指定 session="false",否则每个 jsp 页面都会为您创建一个会话。

标签: jsp taglib


【解决方案1】:

从 taglib 访问会话数据的正确示例是:

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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 2011-09-03
    • 2012-12-07
    • 2011-08-14
    • 1970-01-01
    • 2011-12-02
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多