Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Jetty WTP Plugin/Jetty WTP Websocket Wizard"
Line 1: | Line 1: | ||
{{ Jetty }} | {{ Jetty }} | ||
− | == Jetty Websocket Wizard == | + | == Using the Jetty Websocket Wizard == |
− | The | + | The plugin, org.eclipse.jst.server.jetty.ui.websocket, provides a wizard that you can use to create a Jetty WebSocket. |
+ | |||
+ | This document accomplishes the following: | ||
+ | |||
+ | * Creates an implementataion of the org.eclipse.jetty.websocket.WebSocketServlet, which use an implementation of org.eclipse.jetty.websocket.WebSocket | ||
+ | * Modifies the <tt>web.xml</tt> to declare the WebSocketServlet createdt-name + servlet-mapping). | ||
+ | * TODO : create an HTML page that calls the WebSocketServlet. | ||
+ | |||
+ | To create a WebSocketServlet, complete the following steps. | ||
+ | |||
+ | <ol> | ||
+ | <li>In the Eclipse menu, navigate to '''File->New>-Other'''. | ||
+ | </li>The ''Select a wizard'' dialog box opens. | ||
− | |||
− | |||
− | |||
− | |||
[[Image:Jetty-wtp-websocket1.jpg]] | [[Image:Jetty-wtp-websocket1.jpg]] | ||
− | The | + | <li> Select '''Web/Jetty WebSocket'''. |
+ | </li> The ''Create Jetty-WebSocket'' dialog box opens. | ||
+ | |||
+ | . | ||
[[Image:Jetty-wtp-websocket2.jpg]] | [[Image:Jetty-wtp-websocket2.jpg]] | ||
− | + | ||
+ | <li>In the Java package field, enter '''org.sample.websocket'''. | ||
+ | <li>In the Class name field, enter '''ChatWebSocketServlet'''. | ||
+ | </li>The Superclass field is automatically filled as org.eclipse.jetty.websocket.WebSocketServlet. | ||
+ | <li>Click '''Next'''. | ||
+ | </li>The ''Create Jetty-WebSocket Deployment Descriptor'' dialog box opens. | ||
Line 24: | Line 40: | ||
− | + | ||
+ | This is the same dialog box that the Servlet uses. | ||
+ | |||
+ | <li>Here you can define the servlet-mappings. | ||
+ | <li>Click '''Next'''. | ||
+ | </li> The ''Create Jetty-WebSocket Modifiers, Interfaces and Methods'' dialog box opens. | ||
[[Image:Jetty-wtp-websocket4.jpg]] | [[Image:Jetty-wtp-websocket4.jpg]] | ||
− | Click | + | |
+ | <li>Here you can generate some methods. The wizard generates doPost for WebSocket using ??? | ||
+ | <li>Click '''Finish'''. | ||
+ | |||
+ | </li>Your workspace looks like this : | ||
+ | |||
[[Image:Jetty-wtp-websocket5.jpg]] | [[Image:Jetty-wtp-websocket5.jpg]] | ||
− | The wizard | + | |
+ | |||
+ | The wizard generates : | ||
* a class ChatWebSocketServlet : | * a class ChatWebSocketServlet : |
Revision as of 18:28, 18 November 2010
Using the Jetty Websocket Wizard
The plugin, org.eclipse.jst.server.jetty.ui.websocket, provides a wizard that you can use to create a Jetty WebSocket.
This document accomplishes the following:
- Creates an implementataion of the org.eclipse.jetty.websocket.WebSocketServlet, which use an implementation of org.eclipse.jetty.websocket.WebSocket
- Modifies the web.xml to declare the WebSocketServlet createdt-name + servlet-mapping).
- TODO : create an HTML page that calls the WebSocketServlet.
To create a WebSocketServlet, complete the following steps.
- In the Eclipse menu, navigate to File->New>-Other. The Select a wizard dialog box opens.
- Select Web/Jetty WebSocket. The Create Jetty-WebSocket dialog box opens.
- In the Java package field, enter org.sample.websocket.
- In the Class name field, enter ChatWebSocketServlet. The Superclass field is automatically filled as org.eclipse.jetty.websocket.WebSocketServlet.
- Click Next. The Create Jetty-WebSocket Deployment Descriptor dialog box opens.
- Here you can define the servlet-mappings.
- Click Next. The Create Jetty-WebSocket Modifiers, Interfaces and Methods dialog box opens.
- Here you can generate some methods. The wizard generates doPost for WebSocket using ???
- Click Finish. Your workspace looks like this :
- a class ChatWebSocketServlet :
.
This is the same dialog box that the Servlet uses.
The wizard generates :
package org.sample.websocket; import java.io.IOException; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.websocket.WebSocket; import org.eclipse.jetty.websocket.WebSocketServlet; /** * Jetty WebSocketServlet implementation class ChatWebSocketServlet */ public class ChatWebSocketServlet extends WebSocketServlet { private static final long serialVersionUID = 1L; /** * @see WebSocketServlet#WebSocketServlet() */ public ChatWebSocketServlet() { super(); // TODO Auto-generated constructor stub } private final Set<ChatWebSocket> members = new CopyOnWriteArraySet<ChatWebSocket>(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getServletContext().getNamedDispatcher("default").forward(request, response); } /* * @see org.eclipse.jetty.websocket.WebSocketServlet#doWebSocketConnect(javax.servlet.http.HttpServletRequest, java.lang.String) */ protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) { return new ChatWebSocket(); } class ChatWebSocket implements WebSocket { private Outbound outbound; public void onConnect(Outbound outbound) { this.outbound = outbound; members.add(this); } /* * @see org.eclipse.jetty.websocket.WebSocket#onMessage(byte, byte[], * int, int) */ public void onMessage(byte frame, byte[] data, int offset, int length) { } /* * @see org.eclipse.jetty.websocket.WebSocket#onMessage(byte, * java.lang.String) */ public void onMessage(byte frame, String data) { for (ChatWebSocket member : members) { try { member.outbound.sendMessage(frame, data); } catch (IOException e) { // org.eclipse.jetty.util.log.Log.warn(e); e.printStackTrace(); } } } /* * @see org.eclipse.jetty.websocket.WebSocket#onDisconnect() */ public void onDisconnect() { members.remove(this); } } }
1.web.xml is modified like this :
<servlet> <description></description> <display-name>ChatWebSocketServlet</display-name> <servlet-name>ChatWebSocketServlet</servlet-name> <servlet-class>org.sample.websocket.ChatWebSocketServlet</servletclass> </servlet> <servlet-mapping> <servlet-name>ChatWebSocketServlet</servlet-name> <url-pattern>/ChatWebSocketServlet</url-pattern> </servlet-mapping>