Which Java API is used for creating WebSocket applications in Java EE?

  • java.socket
  • java.web
  • javax.communication
  • javax.websocket
The javax.websocket API is used for creating WebSocket applications in Java EE, providing classes and interfaces to work with the WebSocket protocol.

In the context of servlets, what is a key difference between HTTP and WebSocket protocols?

  • Connectionless protocol
  • Full-duplex communication
  • Request-Response model
  • Stateless communication
A key difference between HTTP and WebSocket protocols is that WebSocket enables full-duplex communication, allowing both the client and server to send messages independently in real-time.

How does the WebSocket protocol handle message framing compared to HTTP?

  • Messages are framed using JSON format.
  • Messages are framed using XML format.
  • Messages are framed using headers and payload.
  • Messages are not framed; they are sent as a continuous stream.
The WebSocket protocol handles message framing by using headers and payload, unlike HTTP, where messages are not framed, and they are sent as a continuous stream.

What is the role of @ServerEndpoint annotation in Java WebSocket API?

  • It configures the server's endpoint settings.
  • It defines the client's endpoint in a WebSocket connection.
  • It handles exceptions during WebSocket communication.
  • It marks a class as a WebSocket endpoint.
The @ServerEndpoint annotation in Java WebSocket API is used to mark a class as a WebSocket endpoint, allowing the class to handle WebSocket communication on the server side.

How are WebSocket connections initiated in the context of a servlet-based web application?

  • They are automatically established when the web application starts.
  • They are initiated through a dedicated WebSocket servlet.
  • They are initiated using the openConnection() method.
  • They require manual configuration in the web.xml file.
WebSocket connections in a servlet-based web application are initiated through a dedicated WebSocket servlet, which is configured to handle WebSocket communication.

What mechanism does WebSocket use to achieve full-duplex communication?

  • Long Polling
  • Polling
  • Request-Response
  • WebSocket uses its own protocol
WebSocket achieves full-duplex communication by using its own protocol, which enables bidirectional communication between the client and the server in real-time without the need for constant polling.

A WebSocket connection is established with a _________ handshake upgraded from an HTTP connection.

  • SSL/TLS
  • TCP
  • UDP
  • WebSocket
A WebSocket connection is established with a "WebSocket" handshake upgraded from an HTTP connection.

In Java EE, the _________ method is used to send a message to the connected WebSocket client.

  • broadcastMessage()
  • sendMessage()
  • sendText()
  • writeMessage()
In Java EE, the sendMessage() method is used to send a message to the connected WebSocket client.

The annotation _________ is used to configure the endpoint of a WebSocket server.

  • @EndpointConfig
  • @ServerEndpoint
  • @WebSocketConfig
  • @WebSocketEndpoint
The annotation @ServerEndpoint is used to configure the endpoint of a WebSocket server.

A servlet receives a POST request with JSON data. What are the steps to correctly parse and use this data?

  • Use the doGet() method and manually parse the JSON data using a JSON library.
  • Use the doGet() method and rely on the servlet container to automatically parse the JSON data.
  • Use the doPost() method and manually parse the JSON data using a JSON library.
  • Use the doPost() method and rely on the servlet container to automatically parse the JSON data.
When handling a POST request with JSON data, it is appropriate to use the doPost() method, and manual parsing is often necessary using a JSON library. The servlet container does not automatically parse JSON data.