Servlet mapping refers to a servlet that is not defined - maven

I'm building a web project and have the following code in my web.xml:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>-1</load-on-startup>
<enabled>true</enabled>
<async-supported>false</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
In eclipse markers window I see following error twice: The servlet mapping "Faces Servlet" refers to a servlet that is not defined. Well, in my opinion it is defined right there. When I delete servlet-mapping nodes, error disappears, but the project is useless. I've tried to re-validate project and web.xml file, refresh dependencies from maven. I haven't succeeded in finding anything relevant in google search. Also, I used very similar web.xml file in another project(not maven) and there it worked fine.
How can I fix this error?

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

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>

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

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

JSF new website creation

I have a JSF project and I already have an index.xhtml page which is working fine. When I try to add another XHTML page, for some reason it is not connected to my session scoped managed bean. I add the code in my new page but it doesn't work like my index.xhtml. I even copy and pasted the code from index and it still does not work. Any thoughts?
Here is some of the code I have in my new page:
Amount: <h:inputText value="#{transactionBean.amount}" style="color: Yellow; background: Teal;"/>
Price <h:inputText value="#{transactionBean.pricePaid}" style="color: Yellow; background: Teal;"/
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
You've mapped the faces servlet on /faces/* instead of *.xhtml. This means that you need to include /faces path in the URL to get the faces servlet to run.
So, you should not open the page by
http://localhost:8080/SharePortfolioJSF/companies.xhtml
but instead by
http://localhost:8080/SharePortfolioJSF/faces/companies.xhtml
Much better, however, is to just use *.xhtml as URL pattern of the faces servlet, so that you don't need to fiddle with virtual paths.
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
(note that your <session-timeout> of 30 minutes is the default already, just remove it)
From the comments:
Your second page isn't processed by the faces servlet. Your url pattern for the faces servlet is /faces/*. So all requests must contain the prefix /faces in order to get processed by the servlet.
It should work if you call your page with the following URL:
http://localhost:8080/SharePortfolioJSF/faces/companies.xhtml

Resources