- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在@ServerEndpoint 中获取 HttpServletRequest?主要是我试图获取它以便我可以访问 HttpSession 对象。
最佳答案
Update (November 2016): The information provided in this answer is for the JSR356 spec, individual implementations of the spec may vary outside of this information. Other suggestions found in comments and other answers are all implementation specific behaviors outside of the JSR356 spec.
If the suggestions in here are causing you problems, upgrade your various installations of Jetty, Tomcat, Wildfly, or Glassfish/Tyrus. All current versions of those implementations have all been reported to work in the way outlined below.
modifyHandshake()
的调用之间您将无法访问正确的 HttpSession 对象。和
getEndpointInstance()
.
ServerEndpointConfig.getUserProperties()
的 Martin 代码的修改。 map 制作
HttpSession
在
@OnOpen
期间可用于您的套接字实例方法调用
package examples;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response)
{
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
GetHttpSessionSocket.java
package examples;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/example",
configurator = GetHttpSessionConfigurator.class)
public class GetHttpSessionSocket
{
private Session wsSession;
private HttpSession httpSession;
@OnOpen
public void open(Session session, EndpointConfig config) {
this.wsSession = session;
this.httpSession = (HttpSession) config.getUserProperties()
.get(HttpSession.class.getName());
}
@OnMessage
public void echo(String msg) throws IOException {
wsSession.getBasicRemote().sendText(msg);
}
}
奖励功能:无
instanceof
或需要类型转换。
EndpointConfig
每个“端点实例”确实存在对象。
javax.websocket.Session
它将对象端点实例及其配置与特定的逻辑连接联系在一起。 javax.websocket.Session
实例(这是
ServerEndpointConfig.Configurator
支持的功能之一)
javax.websocket.server.ServerContainer.addEndpoint(ServerEndpointConfig)
手动提供javax.servlet.ServletContextInitializer.contextInitialized(ServletContextEvent sce)
内完成调用javax.websocket.server.ServerApplicationConfig.getEndpointConfigs(Set)
称呼。 @ServerEndpoint
带注释的类。 ServerEndpointConfig
对象实例作为
javax.websocket.Session
时的默认值存在最终会被创建。
ServerEndpointConfig.Configurator
对象现在存在并准备执行其主要和唯一目的,以允许自定义 websocket 连接到最终
javax.websocket.Session
的升级过程。
ServerEndpointConfig
端点实例中的对象实例。您只能访问
EndpointConfig
实例。
ServerContainer.addEndpoint(new MyCustomServerEndpointConfig())
在部署期间,后来尝试通过注释访问它,它将不起作用。
@OnOpen
public void onOpen(Session session, EndpointConfig config)
{
MyCustomServerEndpointConfig myconfig = (MyCustomServerEndpointConfig) config;
/* this would fail as the config is cannot be cast around like that */
}
// --- or ---
@OnOpen
public void onOpen(Session session, ServerEndpointConfig config)
{
/* For @OnOpen, the websocket implementation would assume
that the ServerEndpointConfig to be a declared PathParam
*/
}
// --- or ---
@OnOpen
public void onOpen(Session session, MyCustomServerEndpointConfig config)
{
/* Again, for @OnOpen, the websocket implementation would assume
that the MyCustomServerEndpointConfig to be a declared PathParam
*/
}
您可以在 Endpoint 对象实例的生命周期内访问 EndpointConfig,但在有限的时间内。
javax.websocket.Endpoint.onOpen(Session,Endpoint)
, 注释
@OnOpen
方法,或通过使用 CDI。 EndpointConfig 不能以任何其他方式或在任何其他时间使用。
Session.getUserProperties()
访问 UserProperties。电话,随时可用。此用户属性映射始终可用,无论是通过带注释的技术(例如
@OnOpen
、
@OnClose
、
@OnError
或
@OnMessage
调用期间的 Session 参数),还是通过 session 的 CDI 注入(inject),甚至使用使用从
javax.websocket.Endpoint
扩展的非注释 websockets .
ServerEndpointConfig
与之相关。
ServerEndpointConfigs
是表示
EndpointConfig
的默认状态的单个实例最终可用于可能并最终创建的端点实例。
关于websocket - 从 Web Socket @ServerEndpoint 中的 HttpServletRequest 访问 HttpSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17936440/
这两个人似乎在做同样的事情。谁能解释两者之间的主要区别?你什么时候使用一个与另一个? HttpServletRequest.getRemoteUser() HttpServletRequest.get
我现在尝试了很多东西,但我似乎错过了一 block 拼图。这是故事:我有一个请求范围的 bean,它从 HttpServletRequest 读取一些 SessionContext。此属性在过滤器中设
使用HttpServletRequest.login(String, String)登录后,使用以下代码,在以下请求中,我仍然会收到基本身份验证提示。为什么login函数在我的配置中不起作用? 我的终
我为 HttpservletRequest 设置路径信息,如下所示。 request.setAttribute("javax.servlet.include.path_info", pathInfo)
我在表单和请求方面遇到了这个问题。我正在使用 sencha 和 javascript 创建一个网页,该网页将表单发布到 java Web 应用程序,该应用程序从数据库中提取数据并对其进行格式化,然后再
我曾经在 JSF 2 应用程序中基于 cookie 的对话过滤器中打开 session 。现在我想建立相同的机制,但与技术无关。重用一些代码,我将其编写在扩展 OncePerRequestFilter
我从我的客户端向我的服务器发送一个 ajax 请求。 这是我传递的数据结构: data = {"key1" : "value1" , "key2" : {"subkey1": "subvalue1"
如何使用 HttpServletRequest 中的信息识别集群中的节点? 每个节点唯一的任何信息都是合适的 - 我需要它来区分日志。 最佳答案 你可以尝试获取IP和hostname // Get c
我过滤我的 REST 服务的 BasicAuth 并将用户名存储在 HttpServletRequest 中 AuthFilter相关代码 public class AuthFilter implem
我想实现一个速度工具,它提供了一种方法来查明用户是否登录。我正在使用 VelocityLayoutServlet 以便在每个请求上呈现模板。 我的 velocity-tools.xml 看起来像这样:
我正在 Spring MVC 上 Swagger 玩,运行本地 jetty ,由于某种原因得到一个 null HttpServletRequest,至少可以说这很奇怪。 这是完整的堆栈跟踪: java
Class Permission implements ContainerRequestContext { @context HttpServletRequest servletReq
我在启用双向 SSL 的 Weblogic 10.3 中运行一个 servlet。当我尝试从 HTTPServletRequest 获取以下属性时,它们都是空的: - javax.servlet.re
我已经导入了以下内容导入 javax.servlet.http.*; 我想获取首选语言的浏览器 HttpServletRequest request = ServletActionContext.ge
我想拦截过滤器/servlet 中的请求并向其添加一些参数。但是,该请求不会公开“setParameter”方法,并且在操作参数映射时会抛出一个错误,说明它已被锁定。有没有我可以尝试的替代方案? 最佳
为了测试,我想从一些预定义的数据构建 HttpServletRequest 对象,例如: GET / HTTP/1.1 User-Agent: Opera/9.80 (Windows NT 6.1;
在我的 servlet 中,req.getQueryString() 在向其发送 ajax 请求时返回 null。这是因为 req.getQueryString() 只适用于 GET 而不是 POST
我使用打印编写器直接在 servlet 中打印列表,然后打印列表。 当我尝试放入 jsp 时,列表不会打印我使用的是 JSTL 还是 scriptlet。 我尝试在 JSTL 和 scriptlet
我有一个 jsp,其中包含一个 jquery post 到我的 tomcat 服务器上的一个 servlet,它创建了一个 HttpServletRequest。我想确保只处理我的 jsp 对我的 s
我对HttpServletRequest生命对象有疑问。 request对象进入controller后是否被销毁? 最佳答案 HttpServletRequest 对象的生命周期就是:为 HTTP S
我是一名优秀的程序员,十分优秀!