请教关于session的问题.

hzdzd 2011-06-13 01:29:24
现有一个过滤器和两个webservice, 一个webservice用于登录,另一个用于取得图书信息。当登录成功后,往session中写入用户名,
这样当访问图书信息的webservice时,如果在过滤器中发现用户名为空时,则提示还没登录.

现在的问题是, 登录时往session中写了用户名, 但当访问图书信息webserice时,在过滤器中发现用户名为空,而且sessionID也是新的.

具体如下:
过滤器:
public class bookFilter implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();

String userName = (String)session.getServletContext().getAttribute("userName");

if(req.getRequestURI().contains("booksdir/books") && userName == null)
{
System.out.println("请先登录");
return;
}

//...
chain.doFilter(request, response);
}

public void destroy()
{
}
}

有下面两个webservice:
1.登录的webservice
@Path("login")
public class LoginService {

@GET
@Path("login")
public boolean login(@Context HttpServletResponse response, @Context HttpServletRequest request) throws Exception
{
String username = request.getParameter("username");
String password = request.getParameter("password");
//...

HttpSession session = request.getSession();

session.setAttribute("userName", username);

//...

return true;
}
}

2. 获取图书信息的webservice:
@Produces("application/xml")
@Path("books")
public class Books
{
@GET
public List<book> getbooks(@Context HttpServletRequest request) {
List<book> books = new ArrayList<book>();

//...

return books;
}
}


现在用如下的方式调用LoginService,即登录 (LoginService会把参数username存到session中,见如上代码):
http://localhost:8080/booksdir/login/login?username=liming&password=88888

接着访问如下的URL:
http://localhost:8080/booksdir/books

但发现过滤器中的userName为空:
String userName = (String)session.getServletContext().getAttribute("userName");

请问是怎么回事?
...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzdzd 2011-06-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zdhcumt 的回复:]
String userName = (String)session.getServletContext().getAttribute("userName");
这里是错的
你放到session中了,并没有放到Application(即ServletContext)中
应该直接从session中取
String userName = (String)session.getAttribute(……
[/Quote]

我实际在程序中就是按下面这样写的,但session值得不到:
String userName = (String)session.getAttribute("userName");

发贴的时候不小心写在敲成了下面的代码:
String userName = (String)session.getServletContext().getAttribute("userName");
zdhcumt 2011-06-13
  • 打赏
  • 举报
回复

String userName = (String)session.getServletContext().getAttribute("userName");
这里是错的
你放到session中了,并没有放到Application(即ServletContext)中
应该直接从session中取
String userName = (String)session.getAttribute("userName");
hmily002 2011-06-13
  • 打赏
  • 举报
回复
String username = request.getParameter("username");
String password = request.getParameter("password");
//...

HttpSession session = request.getSession();
在这一步, session 取得的值是什么? username 还是password
session.setAttribute("userName", username);
这一步还不如直接写成
session.setAttribute("userName",request.getParameter("username"));

session 在一次会话中使用完后最好remove 一下。
hzdzd 2011-06-13
  • 打赏
  • 举报
回复
不好意思,发贴的时候写错了,实际应该是:
String userName = (String)session.getAttribute("userName");
风中叶 2011-06-13
  • 打赏
  • 举报
回复
String userName = (String)session.getServletContext().getAttribute("userName");
你这个的session 是HttpSession么? 信息放在session里了为什么还要session.getServletContext() 直接session.getAttribute("userName");

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧