cometd spring Request method 'POST' not supported for /cometd/handshake - spring

Am try to integrate cometd(spring-jquery-jetty7) with the appfuse spring MVC project.
my web.xml is
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
and did all other configuration like spring-jquery-jetty7 example, When i try cometd.handshake() from the script, it's failed and got error from the log like follows
WARN [http-8080-6] PageNotFound.handleHttpRequestMethodNotSupported(183) | Request method 'POST' not supported
115117 [http-8080-6] WARN org.springframework.web.servlet.PageNotFound - Request method POST' not supported
Anybody experience this? hope the dispatcher servlet process the request instead of cometd servlet, i dont know whats wrong in this, suggestion regarding this are welcome.
thank you

I resolve the issue by changing the servlet orders like cometd servlet first and dispatcher servlet second. The dispatcher servlet handle the cometd request first and throw the error always so i change the order like follows
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
and also add load-on-startup for initialize the comet servlet when application start. thank you

Related

The requested resource is not available on project launch spring mvc

Please why I am getting the requested resource is not found on project start up even though everything seems alright
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Please assist me!!!
You need to load the Spring context with org.springframework.web.context.ContextLoaderListener, not Log4jConfigListener (or try out Spring Boot)
See Loading context in Spring using web.xml

Difference between "dispatcherServlet" and "appServlet" in spring MVC

Difference between "dispatcherServlet" and "appServlet" in spring MVC. Can I get any samples or references?
Technically both are HttpServlet implementation to handle incoming requests. DispatcherServlet is Spring provided servlet implemenation having all essential features like exception handling ..
You have to just write your Request mappers ,it will handle all request.
AppServlet is nothing different, just your implementation for specific handling of requests.
Both will work in same way .If you dont have any specific handling than you can just go with Spring DispatcherServlet.
For example..
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- Custom Servlet -->
<servlet>
<servlet-name>CustomServlet</servlet-name>
<servlet-class>org.abc.CustomServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>any-other-Parameter</param-name>
<param-value>false</param-value>
</init-param>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>/myapp/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CustomServlet</servlet-name>
<url-pattern>/myapp2/*</url-pattern>
</servlet-mapping>
For reference of DispatcherServlet you can see http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example/
to understand this, you can have a look on below configuration :
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In above configuration DispatcherServlet is the servlet class provided by spring framework.
The job of the DispatcherServlet is to take an incoming URI and find the
right combination of handlers (generally methods on Controller classes)
and views (generally JSPs) that combine to form the page or resource
that's supposed to be found at that location.
while appServlet is the custom name given by you in your web.xml file.

Stomp.js and Spring WebSocket integration with Web MVC project

I am having one spring project which is running with '*.htm' extension , we have bind it in web.xml file.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Now I want to integrate the Spring WebSocket and stomp js for the chat application, but the problem here is stomp.js is sending the request to the server without '.htm' extenstion.
due to which I am getting 404 error in each request (info or other xhr).
Is there any way to enable the Spring WebSocket and stomp js with '.htm' extension ?
I can't remove this extension it will hault my current application.
add prefix in the websocket url,it works well.
js:
var socket = new SockJS("/websocket_demo/myapp/ws");
web.xml DispatchServlet mapping:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/myapp/*</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
You can add multiple servlet-mapping like this and keep the .htm working with "/url" serving for new functionality
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/url</url-pattern>
</servlet-mapping>
if you are using Servlet 2.5 you can directly use
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
<url-pattern>/url</url-pattern>
</servlet-mapping>

Single servlet mapped multiple times in web.xml

I have come accross some existing code where in web.xml a single servlet is mapped multiple times. I dont understand the need of doing so.
e.g.
<servlet>
<servlet-name>test1</servlet-name>
<servlet-class>
com.test.spring.MyDispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>test2</servlet-name>
<servlet-class>
com.test.spring.MyDispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>test3</servlet-name>
<servlet-class>
com.test.spring.MyDispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Assume if you have different url-pattern for each servlet, you can combine that into a single url-pattern.
If the url-patterns are same, then you can remove the redundant mappings.
As far I know, adding the same servlet mapping multiple times is not useful or not needed.
This post discussed in detail about servlet mapping. Hope this helps.
Thanks for the reply.
The url pattern are different.
e.g.
<servlet-mapping>
<servlet-name>test1</servlet-name>
<url-pattern>/test1/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>test2</servlet-name>
<url-pattern>/test2/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>test3</servlet-name>
<url-pattern>/test3/*</url-pattern>
</servlet-mapping>

How to make Spring MVC and plain JSP live together in one application

Say I have a Spring MVC application with JPA as backend. Now here we want to provide simple UI to user to perform simple configuration to some properties file. It would make sense to make it separate from the main Spring application because some configuration is related to Spring MVC so it will fail when start the main application by the main UI through Spring MVC.
But how to register both servlet(Spring and plain JSP)in the same web application?
<!-- Handles Spring requests -->
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mvc-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PlainJSPApplication</servlet-name> <!--Is it ok to separate request to different servlet like this?-->
<servlet-class>com.app.plainJSP</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PlainJSPApplication</servlet-name>
<url-pattern>/config</url-pattern> <!--How to handle mapping so not conflict to Spring main application-->
</servlet-mapping>
I think it is common to register another servlet class to in the SAME web.xml, is it OK? and also how to handle that request URL pattern, as "/" has been assign to Spring servlet?
Any advice would be appreciated.
You can separate Spring managed controllers and your own servlet by mapping both with different url patterns.
The requests for Spring controllers are managed by DispatcherServlet. Basically, it is just a Servlet that, when you map urls to it, it will automatically be seen by Spring, thus mapping it to the right controller, views etc.
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PlainJSPApplication</servlet-name> <!--Is it ok to separate request to different servlet like this?-->
<servlet-class>com.app.plainJSP</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PlainJSPApplication</servlet-name>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.bmk</url-pattern>
<!-- other url pattern ... -->
<!-- other url pattern ... -->
<!-- other url pattern ... -->
</servlet-mapping>
Here, all the requests end with .do will be seen by Spring. Others will then be seen by your servlets.
So, as long as you don't harm this mapping, Spring MVC & your normal servlets will integrate gracefully.

Resources