根据文档查看发现使用@context的方式就可以注入HttpServletRequest,但我在实际使用中,始终无法注入,都获取的是null值
@Path("UserContext")
public class UserContext {
@GET
public String hi(@QueryParam("name") String yourName,
@Context HttpServletResponse response,
@Context HttpServletRequest request ){
if(yourName!=null)
request.getSession().setAttribute("name", yourName);
String username = (String) request.getSession().getAttribute("name");
if(username!=null){
System.out.println(request.getSession().getId() + ":" + username);
}
else{
System.out.println(request.getSession().getId() + "没有用户");
}
return null;
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
人生最曼妙的风景,竟是内心的淡定与从容!