WebSocket and Struts2 conflict

Source: Internet
Author: User
Tags constant ssh
Need to use WebSocket in an old project.
Made a small example of websocket, no problem.
When this example is integrated into the STRUTS2 project, the WebSocket is not accessible. It is because the STRUTS2 filter has intercepted all requests, causing Websock to be unable to process requests to/websocket. Workaround: Use the Excludepattern property in the Struts configuration file to filter out the unhandled URLs.
WebSocket need Javaee-api-7.0.jar. STRUTS2 jar packages are not listed here, you can import them yourself.

index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" src= "/js/jquery-1.8.3.min.js" ></script>
<body>
<div>
<input type= "Submit" value= "Start" onclick= "Start ()"/>
</div>
<div id= "Messages" ></div>
<script type= "Text/javascript" >
var webSocket = new WebSocket (' Ws://localhost:80/websocket ');

Websocket.onerror = function (event) {
OnError (Event)
};

Websocket.onopen = function (event) {
OnOpen (Event)
};

Websocket.onmessage = function (event) {
OnMessage (Event)
};

function OnMessage (event) {
document.getElementById (' Messages '). InnerHTML + = ' <br/> '
+ Event.data;
}

function OnOpen (event) {
document.getElementById (' Messages '). InnerHTML = ' Connection established ';
}

function OnError (event) {
alert (event.data);
}

function Start () {
Websocket.send (' Hello ');
return false;
}
</script>
</body>

Websocketapp.java
Package com.ssh.action;

Import java.io.IOException;
Import Javax.websocket.OnClose;
Import Javax.websocket.OnMessage;
Import Javax.websocket.OnOpen;
Import javax.websocket.Session;
Import Javax.websocket.server.ServerEndpoint;

@ServerEndpoint ("/websocket")
public class Websocketapp {

@OnMessage
public void OnMessage (String message, Session session) throws IOException, Interruptedexception {

Print the client message for testing purposes
System.out.println ("Received:" + message);

Send the first message to the client
Session.getbasicremote (). SendText ("This is the first server message");

Send 3 messages to the client every 5 seconds
int sentmessages = 0;
while (Sentmessages < 3) {
Thread.Sleep (5000);
Session.getbasicremote (). SendText ("This was an intermediate server message. Count: "+ sentmessages);
sentmessages++;
}

Send a final message to the client
Session.getbasicremote (). SendText ("The Last Server Message");
}

@OnOpen
public void OnOpen () {
System.out.println ("Client connected");
}

@OnClose
public void OnClose () {
System.out.println ("Connection closed");
}
}
Struts.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.3//en"
"Http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<constant name= "Struts.action.excludePattern" value= "/websocket" ></constant>
<package name= "MyPackage" extends= "Struts-default" >
<action name= "useraction" class= "Com.ssh.action.UserAction"
Method= "Save" >

</action>
</package>
</struts>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.